import java.io.*;
public class randFile {
final static int DoubelSize=8;
void randomFileTest(String fileName) throws IOException {
RandomAccessFile rf = new
RandomAccessFile(fileName, "rw");
for(int i = 0; i < 10; i++) rf.writeDouble(i*1.0);
rf.seek( 5 * DoubelSize );
rf.writeDouble(98.0001);
rf.close( );
rf = new RandomAccessFile(fileName, "r");
for(int i = 0; i < 10; i++)
System.out.println("Value " + i +
": " + rf.readDouble( ));
rf.close( );
}
public static void main(String args[ ]) {
BufferedReader stdin=new BufferedReader(
new InputStreamReader(System.in));
String fileName=null;
randFile obj=null;
try{ System.out.print("Enter a file name: ");
fileName=stdin.readLine( );
obj=new randFile( );
obj.randomFileTest(fileName);
}catch(IOException e) {
System.out.println("File not found : "+e);
e.printStackTrace( );
}
}
}
运行结果是:
Enter a file name : randfile.txt
Value 0: 0.0
Value 1: 1.0
Value 2: 2.0
Value 3: 3.0
Value 4: 4.0
Value 5: 98.0001
Value 6: 6.0
Value 7: 7.0
Value 8: 8.0
Value 9: 8.0