⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectfileuos.java

📁 国外的数据结构与算法分析用书
💻 JAVA
字号:
/* ObjectFileUos.java
 * ---------------------------------------------
 * Copyright (c) 2001 University of Saskatchewan
 * All Rights Reserved
 * -------------------------------------------- */

package dslib.file;

import java.io.*; 
import dslib.base.ContainerUos; 
import dslib.exception.*;
 
/**	Class that is used to store and retrieve objects in a file.  A client can specify the 
	file location where an object is to be written and from which an object can read. 
	@see java.io.RandomAccessFile
	@see java.io.ObjectInputStream
	@see java.io.ObjectOutputStream */
public class ObjectFileUos implements ContainerUos
{
	/**	The name of the underlying file. */
	protected String fileName; 

	/**	The underlying file that is used to store objects. */
	protected RandomAccessFile file;

	/**	The mode of the file. */
	public String fileMode; 

	/**	The file stream used to read objects from the file. */
	protected FileInputStream fileIn;

	/**	The file stream used to write objects to the file. */
	protected FileOutputStream fileOut;

	/**	Construct a file with the specified name. <br>
		Analysis: Time = O(1) 
		@param name The name of the file where objects are written and read */
	public ObjectFileUos(String name, String mode)
	{
		openFile(name, mode);
	}

	/**	Open the file with the specified name. <br> 
		Analysis: Time = O(1) file reads
		@param name The name of the file where objects are written and read*/
	public void openFile(String name, String mode)
	{
		try
		{
			fileName = name;
			fileMode = mode;
			file = new RandomAccessFile(name, fileMode);
			if (!fileMode.equals("r"))
			{
				fileOut = new FileOutputStream(file.getFD());
				/*	Create an initial ObjectOutputStream and use it to initialize
					the file with the stream header information. */
				ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
				/*	Move the file pointer to the beginning of the stream header
			   		so the ObjectInputStream can read it. */
				file.seek(0);
			}
			fileIn = new FileInputStream(file.getFD());
			ObjectInputStream objIn = new ObjectInputStream(fileIn);
			startPosition = position();
		} catch (IOException e)
		{
			System.err.println("Error while opening files");
			e.printStackTrace();
			throw new Error("Error while opening files");
		}
	}

	/**	The start position for objects in the file. */
	protected long startPosition;

	/**	Is the file empty?. <br>
		Analysis: Time = O(1) */
	public boolean isEmpty()
	{ 
		return (length() == startPosition);
	}

	/**	Is the file full?. <br>
		Analysis: Time = O(1) */
	public boolean isFull()
	{ 
		return false;
	}

	/**	The current position in the file. <br> 
		Analysis: Time = O(1) */
	public long position()
	{
		try
		{
			return file.getFilePointer();
		}catch(IOException e)
		{
			e.printStackTrace();
			throw new Error("Error while accessing the file pointer");
		}
	}

	/**	Move to the start of the file. <br>
		Analysis: Time = O(1)  */
	private void reset()
	{
		seek(startPosition);
	}

	/**	Move to a new position in the file. <br>
		Analysis: Time = O(1) 
		@param pos The position to move to */
	private void seek(long pos)
	{
		try
		{
			file.seek(pos);
		}catch(IOException e)
		{
			e.printStackTrace();
			throw new Error("Error while seeking");
		}
	}

	/**	The length of this file. <br> 
		Analysis: Time = O(1) */
	public long length()
	{
		try
		{
			return file.length();
		} catch(IOException e)
		{
			e.printStackTrace();
			throw new Error("Error while getting file length");
		}
	}

	/**	Is the current position at the end of the file?. <br> 
		Analysis: Time = O(1) */
	public boolean eof()
	{
		return (position() == length());
	}

	/**	Close the file. <br> 
		Analysis: Time = O(1) */
	public void close()
	{
		try
		{
			if (!fileMode.equals("r"))
			{
				fileOut.close();
			}
			fileIn.close();
			file.close();
		} catch(IOException e)
		{
			e.printStackTrace();
			throw new Error("Error while closing the files");
		}
	}

	/**	Write the specified object to the file at the current position. <br> 
		Analysis: Time = O(n) + 1 file write, n = size of the object <br>
		PRECONDITION:  <br>
		<ul>
			!fileMode.equals("r") 
		</ul> 
		@param x The object to be written to disk */
	public void writeObject(Object x)
	{
		if (fileMode.equals("r"))
			throw new InvalidModeUosException("Cannot write into a file opened in read mode.");
		
		try
		{
			ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
			objOut.writeObject(x);
		} catch(IOException e)
		{
			e.printStackTrace();
			throw new Error("Error in writeObject()");
		}
	}

	/**	Append the specified object to the end of the file. <br>
		Analysis: Time = O(n) + 1 file write, n = size of the object <br>
		PRECONDITION:  <br>
		<ul>
			!fileMode.equals("r") 
		</ul>  
		@param x The object to be appended to the end of the file */
	public void appendObject(Object x)
	{
		if (fileMode.equals("r"))
			throw new InvalidModeUosException("Cannot append onto a file opened in read mode.");
		
		seek(length());
		writeObject(x);
	}

	/**	Writes the specified object at the specified position. <br>
		Analysis: Time = O(n) + 1 file write, n = size of the object <br>
		PRECONDITION:  <br>
		<ul>
			!fileMode.equals("r") 
		</ul>  
		@param x The object to be written to disk
		@param pos The position where x will be written to disk */
	public void writeObjectAt(Object x, long pos)
	{
		if (fileMode.equals("r"))
			throw new InvalidModeUosException("Cannot write into a file opened in read mode.");
		
		seek(pos);
		writeObject(x);
	}

	/**	Read an object from the file at the current position. <br>
		Analysis: Time = O(n) + 1 file read, n = size of the object <br>
		PRECONDITION:  <br>
		<ul>
			!eof() 
		</ul>  */
	public Object readObject()
	{
		if (eof())
			throw new AfterTheEndUosException("Cannot read from a file when at eof.");
		
		try
		{
			ObjectInputStream objIn = new ObjectInputStream(fileIn);
			return objIn.readObject();
		} catch(Exception e)
		{
			e.printStackTrace();
			throw new Error("Error in the reading of an object from the file");
		}
	}

	/**	Read an object from a specified location on disk. <br>
		Analysis: Time = O(n) + 1 file read, n = size of the object <br>
		PRECONDITION:  <br>
		<ul>
			pos < length() 
		</ul> 
		@param pos The position on disk where the object will be read */
	public Object readObjectFrom(long pos)
	{
		if (pos >= length())
			throw new AfterTheEndUosException("Cannot read from after the end of a file.");
		
		seek(pos);
		return readObject();
	}

	/**	Remove the contents of the file. <br>
		Analysis: Time = O(1) <br>
		PRECONDITION:  <br>
		<ul>
			!fileMode.equals("r") 
		</ul>  */
	public void wipeOut()
	{
		if (fileMode.equals("r"))
			throw new InvalidModeUosException("Cannot wipeout a file opened in read mode.");
		
		try
		{
			file.setLength(startPosition);
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new Error("Error in setting the length of the file.");
		}
	}

	/**	String representation of the contents of the file. <br>
		Analysis: Time = O(s) file reads, s = number of objects in the file */
	public String toString()
	{
		String result = "";
		try
		{
			long initialPosition = position();
			reset();
			while (!eof())
				result += readObject() + "\n";
			seek(initialPosition);
		} catch(Exception e)
		{
			e.printStackTrace();
			throw new Error("Error in reading an object.");
		}
		return result;
	}
}

⌨️ 快捷键说明

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