objectio.java.bak

来自「考试系统」· BAK 代码 · 共 99 行

BAK
99
字号
/*
 * Created on 2004-4-29
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package io;
import java.io.*;

/**
 * @author SpiritRain
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public abstract class ObjectIO {
	private Object obj;
	private String fileName;
	private static String path = "lib\\";
	private ObjectInputStream in;
	private ObjectOutputStream out;

	public void read() {
		try {
			in = new ObjectInputStream(new FileInputStream("lib\\" + fileName));
			obj = in.readObject();
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void write() {
		try {
			out =
				new ObjectOutputStream(
					new FileOutputStream("lib\\" + fileName));
			out.writeObject(obj);
			out.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setFileName(String fname) {
		this.fileName = fname;
	}

	public Object getObject() {
		return this.obj;
	}

	/**
	 * 
	 */
	public static Object read(String fileName) {
		Object obj = null;
		try {
			ObjectInputStream in =
				new ObjectInputStream(new FileInputStream(path + fileName));
			obj = in.readObject();
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			return obj;
		}
	}

	/**
	 * 
	 */
	public static void write(Object obj, String fileName) {
		try {
			ObjectOutputStream out =
				new ObjectOutputStream(
					new FileOutputStream(path + fileName));
			out.writeObject(obj);
			out.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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