我正在努力将数组保存到文件中,然后将其读回到保存它的同一数组中。
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]);
}
}
我保存的代码是
人员列表[]; 静态字符串文件名 = "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);
}
}
如果代码有点混乱并且不是很结构化,我很抱歉。