各位大哥大姐来看看哪! 我这几个程序,有两个能写,但回读时就找不到文件,而另外两个却没有这个问题,小弟百思不得其解,请大哥大姐指导一下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*; import java.util.*;public class DataFileTest { public static void main(String[] args) { Employee[] staff=new Employee[3]; staff[0]=new Employee("Carl Cracker",75000,1987,12,15); staff[1]=new Employee("Harry Hacker",50000,1989,10,1); staff[2]=new Employee("Tony Tester",40000,1990,3,15); try { PrintWriter out=new PrintWriter(new FileWriter("employee.dat")); writeData(staff,out); out.close(); BufferedReader in=new BufferedReader(new FileReader("employee.dat")); Employee[] newStaff=readData(in); in.close(); for(int i=0;i<newStaff.length;i++) System.out.println(newStaff[i]); } catch(IOException exception) { exception.printStackTrace(); } } static void writeData(Employee[] e,PrintWriter out) throws IOException { out.println(e.length); for(int i=0;i<e.length;i++) e[i].writeData(out); } static Employee[] readData(BufferedReader in)throws IOException { int n=Integer.parseInt(in.readLine()); Employee[] e=new Employee[n]; for(int i=0;i<n;i++) { e[i]=new Employee(); e[i].readData(in); } return e; }}class Employee{ public Employee() { } public Employee(String n,double s,int year,int month,int day) { name=n; salary=s; GregorianCalendar calendar=new GregorianCalendar(year,month-1,day); hireDay=calendar.getTime(); } public String getName() { return name; } public double getSalary() { return salary; } public Date getHireDay() { return hireDay; } public void raiseSalary(double byPercent) { double raise=salary*byPercent/100; salary+=raise; } public String toString() { return getClass().getName()+"[name="+name+".salary="+salary+".hireDay="+hireDay+"]"; } public void writeData(PrintWriter out)throws IOException { GregorianCalendar calendar=new GregorianCalendar(); calendar.setTime(hireDay); out.println(name+"|"+salary+"|"+calendar.get(Calendar.YEAR)+"|"+calendar.get(Calendar.DAY_OF_MONTH)); } public void readData(BufferedReader in) throws IOException { String s=in.readLine(); StringTokenizer t=new StringTokenizer(s,"|"); name=t.nextToken(); salary=Double.parseDouble(t.nextToken()); int y=Integer.parseInt(t.nextToken()); int m=Integer.parseInt(t.nextToken()); int d=Integer.parseInt(t.nextToken()); GregorianCalendar calendar=new GregorianCalendar(y,m-1,d); hireDay=calendar.getTime(); } private String name; private double salary; private Date hireDay;} 这是第二个import java.io.*; import java.util.*; class Employee implements Serializable { public Employee() { } public Employee(String n,double s,int year,int month,int day) { name=n; salary=s; GregorianCalendar calendar=new GregorianCalendar(year,month-1,day); hireDay=calendar.getTime(); } public String getName() { return name; } public double getSalary() { return salary; } public Date getHireDay() { return hireDay; } public void raiseSalary(double byPercent) { double raise=salary*byPercent/100; salary+=raise; } public String toString() { return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]"; } private String name; private double salary; private Date hireDay; } class Manager extends Employee { public Manager(String n,double s,int year,int month,int day) { super(n,s,year,month,day); bonus=0; } public double getSalary() { double baseSalary=super.getSalary(); return baseSalary+bonus; } public void setBonus(double b) { bonus=b; } public String toString() { return super.toString()+"[bonus="+bonus+"]"; } private double bonus; }public class ObjectFileTest { public static void main(String[] args) { Manager boss=new Manager("Carl Cracker",80000,1987,12,15); boss.setBonus(5000); Employee[] staff=new Employee[3]; staff[0]=boss; staff[1]=new Employee("Harry Hacker",50000,1989,10,1); staff[2]=new Employee("Tony Tester",40000,1990,3,15); try { ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("employee.dat")); out.writeObject(staff); out.close(); ObjectInputStream in=new ObjectInputStream(new FileInputStream("employee.dat")); Employee[] newStaff=(Employee[])in.readObject(); in.close(); for(int i=0;i<newStaff.length;i++) System.out.println(newStaff[i]); } catch(Exception e) { e.printStackTrace(); } }} 三import java.io.*; import java.util.*; class Employee implements Serializable { public Employee() { } public Employee(String n,double s,int year,int month,int day) { name=n; salary=s; GregorianCalendar calendar=new GregorianCalendar(year,month-1,day); hireDay=calendar.getTime(); } public String getName() { return name; } public double getSalary() { return salary; } public Date getHireDay() { return hireDay; } public void raiseSalary(double byPercent) { double raise=salary*byPercent/100; salary+=raise; } public String toString() { return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]"; } private String name; private double salary; private Date hireDay; } class Manager extends Employee { public Manager(String n,double s,int year,int month,int day) { super(n,s,year,month,day); bonus=0; secretary=null; } public void setSecretary(Employee s) { secretary=s; } public double getSalary() { double baseSalary=super.getSalary(); return baseSalary+bonus; } public void setBonus(double b) { bonus=b; } public String toString() { return super.toString()+"[bonus="+bonus+"]"+"[secretary="+secretary+"]"; } private double bonus; private Employee secretary; }public class ObjectRefTest { public static void main(String[] args) { Employee harry=new Employee("Carl Cracker",50000,1989,10,1); Manager boss=new Manager("Carl Cracker",80000,1987,12,15); boss.setSecretary(harry); Employee[] staff=new Employee[3]; staff[0]=boss; staff[1]=harry; staff[2]=new Employee("Tony Tester",40000,1990,1,15); try { ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream("employee.dat")); out.writeObject(staff); out.close(); ObjectInputStream in=new ObjectInputStream(new FileInputStream("employee.dat")); Employee[] newStaff=(Employee[])in.readObject(); in.close(); newStaff[1].raiseSalary(10); for(int i=0;i<newStaff.length;i++) System.out.println(newStaff[i]); } catch (Exception e) { e.printStackTrace(); } }} utf-8判断 关于使用socket传输非文本文件的问题 关天JAVA中用CANVAS类画图 初学Java有些地方不明白,还望哥哥们指教....谢谢各位进来一阅..MM这厢有礼啦~ 请问个泛型通配符问题 谁有jspsmart包? 在applet中如何使用keypress事件. 包的错误在哪里? 初学者遇到的问题之二? linux中jvm内存不够怎么解决? MTring (小古典),参考一下!接你的问题 每隔一时间,进行一次操作,要怎么做?
import java.util.*;
public class DataFileTest
{
public static void main(String[] args)
{
Employee[] staff=new Employee[3];
staff[0]=new Employee("Carl Cracker",75000,1987,12,15);
staff[1]=new Employee("Harry Hacker",50000,1989,10,1);
staff[2]=new Employee("Tony Tester",40000,1990,3,15);
try
{
PrintWriter out=new PrintWriter(new FileWriter("employee.dat"));
writeData(staff,out);
out.close();
BufferedReader in=new BufferedReader(new FileReader("employee.dat"));
Employee[] newStaff=readData(in);
in.close();
for(int i=0;i<newStaff.length;i++)
System.out.println(newStaff[i]);
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
static void writeData(Employee[] e,PrintWriter out) throws IOException
{
out.println(e.length);
for(int i=0;i<e.length;i++)
e[i].writeData(out);
}
static Employee[] readData(BufferedReader in)throws IOException
{
int n=Integer.parseInt(in.readLine());
Employee[] e=new Employee[n];
for(int i=0;i<n;i++)
{
e[i]=new Employee();
e[i].readData(in);
}
return e;
}
}
class Employee
{
public Employee()
{
}
public Employee(String n,double s,int year,int month,int day)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireDay=calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise=salary*byPercent/100;
salary+=raise;
}
public String toString()
{
return getClass().getName()+"[name="+name+".salary="+salary+".hireDay="+hireDay+"]";
}
public void writeData(PrintWriter out)throws IOException
{
GregorianCalendar calendar=new GregorianCalendar();
calendar.setTime(hireDay);
out.println(name+"|"+salary+"|"+calendar.get(Calendar.YEAR)+"|"+calendar.get(Calendar.DAY_OF_MONTH));
}
public void readData(BufferedReader in) throws IOException
{
String s=in.readLine();
StringTokenizer t=new StringTokenizer(s,"|");
name=t.nextToken();
salary=Double.parseDouble(t.nextToken());
int y=Integer.parseInt(t.nextToken());
int m=Integer.parseInt(t.nextToken());
int d=Integer.parseInt(t.nextToken());
GregorianCalendar calendar=new GregorianCalendar(y,m-1,d);
hireDay=calendar.getTime();
}
private String name;
private double salary;
private Date hireDay;
}
import java.io.*;
import java.util.*;
class Employee implements Serializable
{
public Employee()
{
}
public Employee(String n,double s,int year,int month,int day)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireDay=calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise=salary*byPercent/100;
salary+=raise;
}
public String toString()
{
return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
}
private String name;
private double salary;
private Date hireDay;
}
class Manager extends Employee
{
public Manager(String n,double s,int year,int month,int day)
{
super(n,s,year,month,day);
bonus=0;
}
public double getSalary()
{
double baseSalary=super.getSalary();
return baseSalary+bonus;
}
public void setBonus(double b)
{
bonus=b;
}
public String toString()
{
return super.toString()+"[bonus="+bonus+"]";
}
private double bonus;
}
public class ObjectFileTest
{
public static void main(String[] args)
{
Manager boss=new Manager("Carl Cracker",80000,1987,12,15);
boss.setBonus(5000);
Employee[] staff=new Employee[3];
staff[0]=boss;
staff[1]=new Employee("Harry Hacker",50000,1989,10,1);
staff[2]=new Employee("Tony Tester",40000,1990,3,15);
try
{
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("employee.dat"));
out.writeObject(staff);
out.close();
ObjectInputStream in=new ObjectInputStream(new FileInputStream("employee.dat"));
Employee[] newStaff=(Employee[])in.readObject();
in.close();
for(int i=0;i<newStaff.length;i++)
System.out.println(newStaff[i]);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.io.*;
import java.util.*;
class Employee implements Serializable
{
public Employee()
{
}
public Employee(String n,double s,int year,int month,int day)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireDay=calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise=salary*byPercent/100;
salary+=raise;
}
public String toString()
{
return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
}
private String name;
private double salary;
private Date hireDay;
}
class Manager extends Employee
{
public Manager(String n,double s,int year,int month,int day)
{
super(n,s,year,month,day);
bonus=0;
secretary=null;
}
public void setSecretary(Employee s)
{
secretary=s;
}
public double getSalary()
{
double baseSalary=super.getSalary();
return baseSalary+bonus;
}
public void setBonus(double b)
{
bonus=b;
}
public String toString()
{
return super.toString()+"[bonus="+bonus+"]"+"[secretary="+secretary+"]";
}
private double bonus;
private Employee secretary;
}
public class ObjectRefTest
{
public static void main(String[] args)
{
Employee harry=new Employee("Carl Cracker",50000,1989,10,1);
Manager boss=new Manager("Carl Cracker",80000,1987,12,15);
boss.setSecretary(harry);
Employee[] staff=new Employee[3];
staff[0]=boss;
staff[1]=harry;
staff[2]=new Employee("Tony Tester",40000,1990,1,15);
try
{
ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream("employee.dat"));
out.writeObject(staff);
out.close();
ObjectInputStream in=new ObjectInputStream(new FileInputStream("employee.dat"));
Employee[] newStaff=(Employee[])in.readObject();
in.close();
newStaff[1].raiseSalary(10);
for(int i=0;i<newStaff.length;i++)
System.out.println(newStaff[i]);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}