randomfiledemo.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 27 行

JAVA
27
字号
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 + =
减小字号Ctrl + -
显示快捷键?