testobjectfile.java

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

JAVA
59
字号
import dslib.file.ObjectFileUos;

public class TestObjectFile
{
	/**	Show how sequential access to objects in a file can be
		achieved by using the ObjectFileUos class */
	TestObjectFile()
	{

		ObjectFileUos of = new ObjectFileUos("testObjFile","rw");
		long pos1, pos2, pos3, pos4;
		/* write the "records" to disk sequentially */
		pos1 = of.position();
		of.writeObject(new Integer(999));
		pos2 = of.position();
       		of.writeObject(new Double(555));
		pos3 = of.position();
		of.writeObject(new Integer(23));
		pos4 = of.position();
		of.writeObject(new Integer(523));

		/* close the file */
		of.close();

		/* open the file again */
		of.openFile("testObjFile","rw");
		/* read in the records sequentially */
		Object last;
		while (!of.eof())
		{
			last = of.readObject();
			System.out.println("Last record read: " + last);
		}
		last = of.readObjectFrom(pos1);
		System.out.println("Trying to read 999: " + last);
		last = of.readObjectFrom(pos2);
		System.out.println("Trying to read 555: " + last);
		last = of.readObjectFrom(pos3);
		System.out.println("Trying to read 23: " + last);
		last = of.readObjectFrom(pos4);
		System.out.println("Trying to read 523: " + last);
		System.out.println("Rewriting 23 as 32");
		of.writeObjectAt(new Integer(32), pos3);
		last = of.readObjectFrom(pos3);
		System.out.println("Trying to read 32: " + last);
		/* close the file */
		of.close();
	}

	public static void main(String[] args)
	{
		TestObjectFile tof = new TestObjectFile();
	}
}




⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?