Skip to content

Commit daa723d

Browse files
committed
Absolute & Relative Read , Chained Put-Method
1 parent 6c070de commit daa723d

File tree

15 files changed

+423
-0
lines changed

15 files changed

+423
-0
lines changed

Input & Output/005_Absolute&RelativeRead/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Input & Output/005_Absolute&RelativeRead/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Input & Output/005_Absolute&RelativeRead/.idea/workspace.xml

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
40 Bytes
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Line 1
2+
Line 2
3+
Line 3
4+
Line 4
5+
Line 5
1.57 KB
Binary file not shown.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.io.FileOutputStream;
2+
import java.io.IOException;
3+
import java.nio.ByteBuffer;
4+
import java.nio.channels.FileChannel;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
try (FileOutputStream binFile = new FileOutputStream("data.dat");
11+
FileChannel binChannel = binFile.getChannel()) {
12+
13+
ByteBuffer buffer = ByteBuffer.allocate(100);
14+
byte[] outputBytes = "Hello World!".getBytes();
15+
buffer.put(outputBytes);
16+
buffer.putInt(245);
17+
buffer.putInt(-98765);
18+
byte[] outputBytes2 = "Nice to meet you".getBytes();
19+
buffer.put(outputBytes2);
20+
buffer.putInt(1000);
21+
buffer.flip();
22+
binChannel.write(buffer);
23+
24+
// ByteBuffer buffer = ByteBuffer.allocate(outputBytes.length);
25+
// buffer.put(outputBytes);
26+
//
27+
// buffer.flip();
28+
// int numBytes = binChannel.write(buffer);
29+
// System.out.println("numBytes written was: " + numBytes);
30+
//
31+
// ByteBuffer intBuffer = ByteBuffer.allocate(Integer.BYTES);
32+
// intBuffer.putInt(245);
33+
// intBuffer.flip();
34+
// numBytes = binChannel.write(intBuffer);
35+
// System.out.println("numBytes written was: " + numBytes);
36+
//
37+
// intBuffer.flip();
38+
// intBuffer.putInt(-98765);
39+
// intBuffer.flip();
40+
// numBytes = binChannel.write(intBuffer);
41+
// System.out.println("numBytes written was: " + numBytes);
42+
//
43+
// RandomAccessFile ra = new RandomAccessFile("data.dat", "rwd");
44+
// FileChannel channel = ra.getChannel();
45+
// outputBytes[0] = 'a';
46+
// outputBytes[1] = 'b';
47+
// buffer.flip();
48+
// long numBytesRead = channel.read(buffer);
49+
// if (buffer.hasArray()) {
50+
// System.out.println("byte buffer = " + new String(buffer.array()));
51+
//// System.out.println("byte buffer = " + new String(outputBytes));
52+
// }
53+
//
54+
// // Absolute read
55+
// intBuffer.flip();
56+
// numBytesRead = channel.read(intBuffer);
57+
// System.out.println(intBuffer.getInt(0));
58+
// intBuffer.flip();
59+
// numBytesRead = channel.read(intBuffer);
60+
// intBuffer.flip();
61+
// System.out.println(intBuffer.getInt(0));
62+
// System.out.println(intBuffer.getInt());
63+
//
64+
//
65+
// // Relative read
66+
// // intBuffer.flip();
67+
// // numBytesRead = channel.read(intBuffer);
68+
// // intBuffer.flip();
69+
// // System.out.println(intBuffer.getInt());
70+
// // intBuffer.flip();
71+
// // numBytesRead = channel.read(intBuffer);
72+
// // intBuffer.flip();
73+
// // System.out.println(intBuffer.getInt());
74+
//
75+
// channel.close();
76+
// ra.close();
77+
78+
// System.out.println("outputBytes = " + new String(outputBytes));
79+
80+
// RandomAccessFile ra = new RandomAccessFile("data.dat", "rwd");
81+
// byte[] b = new byte[outputBytes.length];
82+
// ra.read(b);
83+
// System.out.println(new String(b));
84+
//
85+
// long int1 = ra.readInt();
86+
// long int2 = ra.readInt();
87+
// System.out.println(int1);
88+
// System.out.println(int2);
89+
90+
} catch (IOException e) {
91+
e.printStackTrace();
92+
}
93+
}
94+
}

Input & Output/006_ChainedPut_Method/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Input & Output/006_ChainedPut_Method/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)