2013-02-05 91 views
-2

我正在努力让我的头保存一个数组到一个文件,然后再读回到它保存的同一个数组。Java文件阅读器和写作

对人的代码是

import java.io.*; 

public class Person implements Serializable 
{ 
     protected String name; 
     protected char gender; 
     protected Date dateOfBirth; 
     protected String address; 
     protected String natInsceNo; 
     protected String phoneNo; 

     public Person() 
     { 
      this("", 'N' , new Date()); 
     } 

     public Person (Person other) 
     { 
      // Create a clone from Person other 
      //TO DO 
     } 

     public Person (String aName, char sex, Date dob) 
     { 
      name = aName; 
      gender = sex; 
      dateOfBirth = dob; 
      address = ""; 
      natInsceNo = "" ; 
      phoneNo = ""; 

     } 

     public void setName(String aName) 
     { 
       //Setter method to alter the Person aName string 
       name = aName; 
     } 

     public void setAddress(String addr) 
     { 
       //Setter method to alter the Person address string 
       address = addr; 
     } 
     public void setNatInsNo(String ins) 
     { 
       //Setter method to alter the Person national insurance number 
       natInsceNo = ins; 
     } 

     public void setPhone(String phone) 
     { 
       //Setter method to alter the Person phone number 
       phoneNo = phone; 
     } 

     public String getName() 
     { 
       //Getter method to return the Person name String 
      return name; 

     } 
     public String getAddress() 
     { 
       //Getter method to return the Person address String 
      return address; 

     } 
     public String getNatInsNo() 
     { 
       //Getter method to return the Person national insurance String 
      return natInsceNo; 

     } 
     public String getPhone() 
     { 
       //Getter method to return the Person phone number 
      return phoneNo; 
     } 

     public boolean equals(Person other) 
     { 
     // return true if name, dateOfBirth and national insurance number are same 

      return false; 
     } 

     public String toString() 
     { 
       //Override the toString() method of the Object class 
       String output= ""; 
       output = "Name: " + name + ", Gender: " + gender+ ", Date Of Birth: " + dateOfBirth +", National Insurance No "+natInsceNo+""; 
      return output;  
     } 

} 

为我的员工的代码是

import java.awt.List; 
import java.io.*; 
import java.lang.reflect.Array; 
import java.util.ArrayList; 

import javax.swing.JOptionPane; 
public class Store implements Serializable 
{ 
private static int MAXSIZE; // holds the size of the array 
private static int count; // keeps count of number of employees stored in array 
Person list[]; 
static String fileName = "Employee.txt"; 


     public void save(Person testStore) throws IOException{ 
      try{ 
       FileWriter fr = new FileWriter(fileName); 
       PrintWriter pr = new PrintWriter(fr); 
       for(int i=0; i<Store.getCount(); i++) 
       { 
        pr.println(list[i]); 
       } 
       pr.close(); 
      } 
      catch(IOException e){ 
       String message = "problem Writing to file"; 
       JOptionPane.showMessageDialog(null, message, null, JOptionPane.ERROR_MESSAGE); 
      } 
     } 

     public void readFile() throws IOException { 
      BufferedReader CSVFile = new BufferedReader(new FileReader(fileName)); 
      String dataRow = CSVFile.readLine(); 
      while (dataRow !=null){ 
       String[] dataArray = dataRow.split(","); 
       for (String item:dataArray){ 
        System.out.print(item); 
        //int i=0; 
        //String word = item; 
        //list[i]=item; 
       } 

       System.out.println(); 
       dataRow = CSVFile.readLine(); 
      } 
     } 






     public Store(int size) 
     { 
      //Constructor which allow the size of the store to be specified 
      list = new Person[size]; 
      MAXSIZE = size; 
      count=0; 
     } 

     public static void setFileName(String fileNameIn){ 
      fileName = fileNameIn;   
     } 

     public static int getCount() 
     { 
      //Getter method to return the number of elements currently in store 
      //To do 
      return count; 
     } 
     public static boolean isFull() 
     { 
      //Returns true if no more space in store 
      //To do 
      return count == MAXSIZE; 
     } 
     public Store() 
     { 
      //Default constructor 
      this(0); 
     } 

     public Store(Store s) 
     { 
      //Create a Store from another Store s 
      //To do 
     } 
     public void add(Employee e) 
     { 
      list[count++] = e; 
     } 


     public void displayAll() 
     { 
      //display the contents of store on the screen 
      for (int i = 0; i< count; i++) 
       System.out.println(list[i]); 
     }  


} 

我的代码保存为

人名单[]; static String fileName =“Employee.txt”;

public void save(Person testStore) throws IOException{ 
     try{ 
      FileWriter fr = new FileWriter(fileName); 
      PrintWriter pr = new PrintWriter(fr); 
      for(int i=0; i<Store.getCount(); i++) 
      { 
       pr.println(list[i]); 
      } 
      pr.close(); 
     } 
     catch(IOException e){ 
      String message = "problem Writing to file"; 
      JOptionPane.showMessageDialog(null, message, null, JOptionPane.ERROR_MESSAGE); 
     } 
    } 

对不起,如果代码是有点搞砸了,而不是非常结构化。

+1

你在哪里有问题? –

+0

和什么问题? – Dan

+1

如果你不使用它,为什么要给参数人员一个保存功能?而你是readfile方法,你应该使用字符串项目从Person创建一个对象。然后把这个对象列入 –

回答

1

使用ObjectOutputStreamObject state序列化为文本文件并使用ObjectInputStream将其还原。

ObjectOutputStream output = new ObjectOutputStream(
          new FileOutputStream(fileName)); 
output.writeObject(Store); 
output.close(); 

ObjectInputStream input = new ObjectInputStream(
          new FileInputStream(fileName)); 
ArrayList<Person> store = (ArrayList<Person>)input.readObject(); 
input.close(); 
+0

当我使用序列化我的文本文件,它保存到当我打开它在文件浏览器中读取 ¬ü您的\t [LPerson;™sOÈÄ-xp dsrEmployeeár|'ÞžçF salaryL idt Ljava/lang/String; L jobTitleq〜L startt LDate; xrPersonÏÒ§C genderL addressq〜L dateOfBirthq 〜L nameq〜L natInsceNoq〜L phoneNoq〜xp Mt srDateÖk®Å'u¶aI dayI monthI yearxpÝtNathant fct cCj t 1t dfvsq〜Ýpppppppppppppppppppppppppppppppppppppppppp 我不知道这是它应该是或不是,也当使用serilaztion读取文件中有一个错误 'code'[LPerson;不能转换为java.util.ArrayList'代码' – user2043723

+0

当你坚持对象时,它会写入字节数组。告诉我一件事是'Store' arraylist Object? –

+0

商店是一个类和列表是类型的人的arraylist也是aclass。 声明为人员列表[]; – user2043723