forked from deepdalsania/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomAccess.java
More file actions
40 lines (35 loc) · 1.05 KB
/
RandomAccess.java
File metadata and controls
40 lines (35 loc) · 1.05 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
package file;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
public class RandomAccess {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f = new File("D:/DEEP/nine.txt");
f.createNewFile();
RandomAccessFile rm = new RandomAccessFile(f,"rw");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your name : ");
rm.writeUTF(br.readLine());
rm.seek(0);
System.out.println("Name is : "+rm.readUTF());
// System.out.println("Pointer : "+rm.getFilePointer());
rm.seek(0);
byte[] b = new byte[(int) rm.length()];
rm.readFully(b, 0, b.length);
String s = new String(b);
System.out.println("Name is : "+s);
rm.seek(s.length());
rm.writeInt(2);
rm.seek(s.length());
System.out.println("readInt : "+rm.readInt());
System.out.println("Pointer : "+rm.getFilePointer());
rm.close();
}
}