2017-02-03 48 views
1

我想读取和写入一个文件的对象。这是我的尝试:在文件中写入和读取对象

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
import java.util.ArrayList; 

public class SaveOpen implements Serializable 
{ 
    private static String fileName; 
    private ArrayList<Person> list = new ArrayList<Person>(); 

    public SaveOpen() { 
     fileName = "file.txt"; 
    } 
    //Reader 
    public static Object deserialize() throws IOException, 
    ClassNotFoundException { 
     FileInputStream fis = new FileInputStream(fileName); 
     BufferedInputStream bis = new BufferedInputStream(fis); 
     ObjectInputStream ois = new ObjectInputStream(bis); 
     Object obj = ois.readObject(); 
     ois.close(); 
     return obj; 
    } 
    //Writer 
    public static void serialize(Object obj) 
      throws IOException { 
     FileOutputStream fos = new FileOutputStream(fileName); 
     BufferedOutputStream bos = new BufferedOutputStream(fos); 
     ObjectOutputStream oos = new ObjectOutputStream(bos); 
     oos.writeObject(obj); 
     oos.close(); 
    } 
    public void setFileName(String fileName) { 
     this.fileName = fileName; 
    } 
    public ArrayList<Person> getListPersons() { 
     return list; 
    } 

} 

但是,我不知道这是否是正确的方法,也不知道如何在类中实现这一点。该对象是Person,我想保存并从文件中读取该对象。它是否应该完成到.txt文件?任何人可以清理的东西?谢谢!

+1

如果你使用流序列化一个对象,那么你正在编写二进制数据,而不是文本,所以你不应该写入'.txt'文件。 – khelwood

+0

没有什么能够阻止你将它写入'.txt'文件,但是这表明内容是文本,而不是。 – Kayaman

+0

https://docs.oracle.com/javase/8/docs/technotes/guides/serialization/ <=有一些可用的例子。 – Fildor

回答

0

如果你想要文件是人类可读的,我会建议把它保存为xml。

例子:

  • 对象类

    import java.io.Serializable; 
    
    public class Person implements Serializable 
    { 
        private String username; 
        private int id; 
    
        public String UserName() { return username; } 
        public void setUserName(String str) { username = str;} 
    
        public int ID() { return id; } 
        public void setID(int ID) { id = ID; }  
    } 
    

-Serializer /解串器

import Settings.Person; 
import java.beans.XMLDecoder; 
import java.beans.XMLEncoder; 
import java.io.ByteArrayInputStream; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 

public class XmlSerializer 
{ 
    //File path to serialize to and deserialize from 
    private static final String SERIALIZED_FILE_NAME = "yourSavePath.xml"; 
    //Insert person object and save as xml file to chosen filepath 
    public static void Serialize(Person person) 
    { 
     try 
     { 
      FileOutputStream os = new FileOutputStream(SERIALIZED_FILE_NAME); 
      XMLEncoder   encoder = new XMLEncoder(os); 
           encoder.writeObject(person); 
           encoder.close(); 
     } 
     catch(FileNotFoundException ex) 
     { 
      System.out.println(ex.getMessage()); 
     } 
    } 
    //Deserialize xml file into person object 
    public static Person Deserialize() 
    { 
     try 
     { 
      FileInputStream os = new FileInputStream(SERIALIZED_FILE_NAME); 
      XMLDecoder decoder = new XMLDecoder(os); 
      Person p = (Person)decoder.readObject(); 
      decoder.close(); 
      return p; 
     } 
     catch(FileNotFoundException ex) 
     { 
      System.out.println(ex.getMessage()); 
     } 
     return null; 
    } 
    } 
0

你这样做是正确的了。你可以在txt文件中安全地使用对象,尽管它没有多大意义,但我宁愿使用二进制文件。

要将多个对象存储在单个文件中,只需将它们打包到一个集合中,然后序列化该集合对象。

从文件中读取对象时,通过instanceof检查其类,并将其转换为任何内容。