import java.io.*;
public class RandomAccess {
public static void main(String[] args) {
new RandomAccess().run();
}
private void run() {
RandomAccessFile f;
try {
f = new RandomAccessFile("data.dat", "rw");
f.writeChar('a');
f.writeByte(10);
f.writeInt(1234567);
f.writeBoolean(true);
f.writeFloat(1.2345f);
f.writeDouble(1.23456789);
f.close();
f = new RandomAccessFile("data.dat", "r");
System.out.println("file length = " + f.length());
System.out.println(f.readChar());
System.out.println(f.readByte());
System.out.println(f.readInt());
System.out.println(f.readBoolean());
System.out.println(f.readFloat());
System.out.println(f.readDouble());
f.close();
}
catch(IOException e) {
System.out.println("got error. Aborted.");
System.exit(1);
}
}
}
RandomAccess.java output
file length = 20
a
10
1234567
true
1.2345
1.23456789
Tag: Study Code Program Java
No comments:
Post a Comment