-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUsingRandomAccessFile.java
More file actions
44 lines (35 loc) · 1005 Bytes
/
UsingRandomAccessFile.java
File metadata and controls
44 lines (35 loc) · 1005 Bytes
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
package io;
import java.io.*;
import java.util.*;
/**
* RUN:
* javac io/UsingRandomAccessFile.java && java io.UsingRandomAccessFile
*
* OUTPUT:
*
*/
public class UsingRandomAccessFile {
static String file = "rtest.dat";
static void display() throws IOException {
RandomAccessFile rf = new RandomAccessFile(file, "r");
for (int i = 0; i < 7; i++) {
System.out.println("Value " + i + " " + rf.readDouble());
}
System.out.println(rf.readUTF());
rf.close();
}
public static void main(String[] args) throws IOException {
RandomAccessFile rf = new RandomAccessFile(file, "rw");
for (int i = 0; i < 7; i++) {
rf.writeDouble(i*1.414);
}
rf.writeUTF("The end of the file");
rf.close();
display();
rf = new RandomAccessFile(file, "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
display();
}
}