📄 randomfiledemo.java
字号:
import java.io.*;
public class RandomFileDemo
{
public static void main(String[] args) throws IOException
{
RandomAccessFile raf = new RandomAccessFile("demo.data", "rw");
/* Write five 8-byte numbers to file. */
for(int i = 0; i < 5; i++)
raf.writeDouble(i * 3.1416);
raf.close();
/* Overwrite the third number in the file. */
raf = new RandomAccessFile("demo.data", "rw");
raf.seek(2 * 8); // move the file pointer to the third number
raf.writeDouble(1.7321); // overwrite third number
raf.close();
/* Read and print the contents of the file. */
raf = new RandomAccessFile("demo.data", "rw");
for(int i = 0; i < 5; i++)
System.out.println("Item " + i + ": " + raf.readDouble());
raf.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -