Java FileReader和写作

时间:2013-02-05 15:50:49

标签: java filewriter

我努力想要将数组保存到文件然后将其读回到保存的相同数组中。

Person的代码是

    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 :(得分: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();