class SerializationUtils {
private SerializationUtils() {
throw new UnsupportedOperationException();
}
public static void writeObject(Serializable s, String fileNameAndPath) {
ObjectOutputStream os = null;
try {
os = new ObjectOutputStream(new FileOutputStream(fileNameAndPath));
os.writeObject(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Object readObject(String fileNameAndPath) {
ObjectInputStream oi = null;
try {
oi = new ObjectInputStream(new FileInputStream(fileNameAndPath));
return oi.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (oi != null) {
try {
oi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
throw new UnsupportedOperationException();
}
}
java序列化工具类
最新推荐文章于 2021-09-23 11:22:21 发布