objectio.java
来自「考试系统」· Java 代码 · 共 54 行
JAVA
54 行
package io;
import java.io.*;
/**
* @author SpiritRain
*
* a simple io class to read and write object
*/
public abstract class ObjectIO {
private static String path = "lib\\";
/**
* read an object from a file
* @param fileName file name
* @return object
*/
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;
}
}
/**
* write an object to a file
* @param obj object to be writed
* @param fileName target file name
*/
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 + -
显示快捷键?