-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
206 lines (161 loc) · 8.2 KB
/
Copy pathMain.java
File metadata and controls
206 lines (161 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
//try with resources
//FileOutputStream is finding/creating a file 'data.dat', think of it as the file object
//the FileChannel is used to read/ write to it
try (FileOutputStream binFile = new FileOutputStream("data.dat");
FileChannel binChannel = binFile.getChannel()) {
//Writing the string in byte for to outputBytes
byte[] outputBytes = "Hello Word!".getBytes();
//wrapping the bytes in a ByteBuffer
ByteBuffer buffer = ByteBuffer.wrap(outputBytes);
int numBytes = binChannel.write(buffer);
System.out.println("Number of bytes written is = " + numBytes);
ByteBuffer intBuffer = ByteBuffer.allocate(Integer.BYTES);
intBuffer.putInt(245);
intBuffer.flip();
numBytes = binChannel.write(intBuffer);
System.out.println("Number of INT bytes written is = " + numBytes);
intBuffer.flip();
intBuffer.putInt(-2498);
intBuffer.flip();
numBytes = binChannel.write(intBuffer);
System.out.println("Number of INT bytes written is = " + numBytes);
// READING THE FILE **WITH RANDOM ACCESS FILE CLASS**
RandomAccessFile ra = new RandomAccessFile("data.dat", "rwd");
byte[] b = new byte[outputBytes.length];
ra.read(b);
System.out.println();
System.out.println(new String(b));
long int1 = ra.readInt();
System.out.println(int1);
long int2 = ra.readInt();
System.out.println(int2);
//*********************** Java NIO ***********************
// READING THE FILE **WITH JAVA.NIO ACCESS FILE CLASS**
RandomAccessFile raNIO = new RandomAccessFile("data.dat", "rwd");
FileChannel channel = raNIO.getChannel();
buffer.flip();
long numBytesRead = channel.read(buffer);
System.out.println("\noutput bytes = " + new String(outputBytes) + " Size is " + numBytesRead);
//THESE ARE RELATIVE READ'S AS WE ARE JUST FLIPPING THE BUFFER EVERY TIME
intBuffer.flip(); //FOR INT 1
numBytesRead = channel.read(intBuffer);
intBuffer.flip();
System.out.println(intBuffer.getInt());
//THIS IS ABSOLUTE READ, HERE WE SEND THE INDEX LOCATION OF THE INT TO THE READER
intBuffer.flip(); //FOR INT 2
numBytesRead = channel.read(intBuffer);
System.out.println(intBuffer.getInt(0));
//DIFFERENT WAY TO PRINT THE STRING IS
buffer.flip();
channel.read(buffer);
if (buffer.hasArray()) {
System.out.println("byte buffer = " + new String(buffer.array()));
}
channel.close();
raNIO.close();
} catch (IOException e) {
e.printStackTrace();
}
//RANDOM ACCESS NIO THOUGH CHANNEL
try (FileOutputStream binFile = new FileOutputStream("Random_data.dat");
FileChannel binChannel = binFile.getChannel()) {
ByteBuffer buffer = ByteBuffer.allocate(100);
byte[] outputBytes = "Hello Word!".getBytes();
buffer.put(outputBytes);
long position1 = outputBytes.length; //start of integer 1
buffer.putInt(245);
long position2 = position1 + Integer.BYTES; //start of integer 2
buffer.putInt(-91221);
byte[] outputBytes2 = "Nice to eet you".getBytes();
buffer.put(outputBytes2);
long position3 = position2 + Integer.BYTES + outputBytes2.length; //start of integer 3
buffer.putInt(1000);
long lastPosition = position3 +Integer.BYTES;
buffer.flip();
//YOU CAN ALSO CHAIN THE PUT METHORD
// buffer.put(outputBytes).putInt(245).putInt(-91221).put(outputBytes2).putInt(1000);
binChannel.write(buffer);
//READING IN LINEAR FOR
System.out.println("\n\nREADING IN LINEAR FOR");
RandomAccessFile ra = new RandomAccessFile("Random_data.dat","rwd");
FileChannel channel = ra.getChannel();
ByteBuffer readBuffer = ByteBuffer.allocate(100);
channel.read(readBuffer);
readBuffer.flip();
byte[] inputString = new byte[outputBytes.length];
readBuffer.get(inputString);
System.out.println("Input String = "+new String(inputString));
System.out.println("int 1 = "+readBuffer.getInt());
System.out.println("int 2 = "+readBuffer.getInt());
byte[] inputString2 = new byte[outputBytes2.length];
readBuffer.get(inputString2);
System.out.println("Input String = "+new String(inputString2));
System.out.println("int 3 = "+readBuffer.getInt());
channel.position(0);
System.out.println("\nRandomly reading with position");
ByteBuffer readRandomBuffer = ByteBuffer.allocate(Integer.BYTES);
channel.position(position3);
channel.read(readRandomBuffer);
readRandomBuffer.flip();
System.out.println("Int 3 = "+readRandomBuffer.getInt());
channel.position(position2);
readRandomBuffer.flip();
channel.read(readRandomBuffer);
readRandomBuffer.flip();
System.out.println("Int 2 = "+readRandomBuffer.getInt());
channel.position(position1);
readRandomBuffer.flip();
channel.read(readRandomBuffer);
readRandomBuffer.flip();
System.out.println("Int 1 = "+readRandomBuffer.getInt());
//WRITING RANDOMLY USING THE LOCATIONS
byte[] newOutputBytes = "***********New Hello Word********".getBytes();
long strPos1 = lastPosition; //LOCATION FOR STRING 1
long newPosition1 = strPos1 + newOutputBytes.length; //LOCATION FOR INT 1
long newPosition2 = newPosition1 + Integer.BYTES; //LOCATION FOR INT 2
long strPos2 = newPosition2 + Integer.BYTES; //LOCATION FOR STRING 2
byte[] newOutputBytes2 = "***************NEW Nice to meet you***********".getBytes();
long newPosition3 = strPos2 + newOutputBytes2.length;//LOCATION FOR STRING 2
ByteBuffer intBuffer = ByteBuffer.allocate(Integer.BYTES);
intBuffer.putInt(78867);
intBuffer.flip();
binChannel.position(newPosition1);
binChannel.write(intBuffer); //WRITING THE FIRST INT
intBuffer.flip();
intBuffer.putInt(-99999);
intBuffer.flip();
binChannel.position(newPosition2);
binChannel.write(intBuffer); //WRITING THE SECOND INT -9999
intBuffer.flip();
intBuffer.putInt(88888);
intBuffer.flip();
binChannel.position(newPosition3);
binChannel.write(intBuffer); //WRITING THE THIRD INT 8888
binChannel.position(strPos1);
binChannel.write(ByteBuffer.wrap(newOutputBytes));
binChannel.position(strPos2);
binChannel.write(ByteBuffer.wrap(newOutputBytes2));
//DATA HAS NOT BE OUTPUTED BUT CAN BE SEEN IN THE .dat FILE
//COPYING A FILE
RandomAccessFile copyFile = new RandomAccessFile("data_copy.dat", "rw");
FileChannel copyChannel = copyFile.getChannel();
channel.position(0);
//here you can use transferFrom(channel,0,channel.size()) or transferTo(0, channel.size(), copyChannel())
long numTransfered = copyChannel.transferFrom(channel,0,channel.size());
System.out.println("Num of bytes tranfered is = "+numTransfered);
channel.close();
ra.close();
copyChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}