序列化和反序列化的例子 程序中新建文件的操作
2014-07-23 07:43
225 查看
public class Serialization { Logger logger = LoggerFactory.getLogger(Serialization.class); String filePath = "serialization/"; File folder = new File(filePath); public static void main(String[] args) throws IOException, ClassNotFoundException { Serialization serialization = new Serialization(); serialization.save(); serialization.restore(); } private void restore() throws IOException, ClassNotFoundException { File file = new File(folder,"a.txt"); FileInputStream inputStream = new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); Person person = (Person) objectInputStream.readObject(); System.out.println(person.getName()); } private void save() { Person tom = new Person("jim", 34); if (!folder.exists()) { folder.mkdir(); } File file = new File(folder,"a.txt"); try { FileOutputStream fileOutputStream = new FileOutputStream(file); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(tom); objectOutputStream.flush(); objectOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- MFC文件的读写操作,类的序列化与反序列化,CFile,CFileDialog,CArchive,CStdioFile
- Java中List或者HashMap对象-序列化和反序列化并附读写文件操作
- Java序列化与反序列化,文件操作
- [Python进阶-7]文件和目录的IO操作,以及json序列化和反序列化
- C# 类用XmlSerializer进行序列化和反序列化进行文件读写的一个简单例子
- protocol buffers 序列化与反序列化 文件操作
- 文章标题 Java中io流的一些简单操作(包含文件复制,向硬盘中写入文本文件,以及io流高级应用序列化和反序列化)
- C数据结构(文件操作,随机数,排序,栈和队列,图和遍历,最小生成树,最短路径)程序例子
- IOS文件操作和自定义对象的归档(序列化)、反归档(反序列化)
- C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子
- 关于:以前的某个程序安装已在安装计算机上创建挂起的文件操作 解决办法
- 某个程序安装已在安装计算机上创建挂起的文件操作" 解决办法
- SQLServer2000的"以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机"错误
- 安装SQL SERVER遇到以前的某个程序安装已在安装计算机上创建挂起的文件操作
- "某个程序安装已在安装计算机上创建挂起的文件操作" 解决办法 另解
- 以程序的方式操作NTFS的文件权限
- 错误提示:以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机
- C#文件操作小例子
- SWT程序操作PDF文件的方法集锦
- [转]关于:以前的某个程序安装已在安装计算机上创建挂起的文件操作 解决办法