import java.io.*;
import java.util.*;
public class TextFileTest
{
public static void main(String[] args) throws IOException
{
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(Scanner in=new Scanner(new FileInputStream("F:\\JAVA\\123.txt"),"UTF-8"))
{
PrintWriter out =new PrintWriter("F:\\JAVA\\123.txt");
writeData(staff,out);
}
try(Scanner in=new Scanner(new FileInputStream("F:\\JAVA\\123.txt"),"UTF-8"))
{
Employee[] newStaff=readData(in);
for(Employee e:newStaff)
System.out.println(e);
}
}
private static void writeData(Employee[] employees,PrintWriter out) throws IOException
{
out.println(employees.length);
for(Employee e: employees)
writeEmployee(out,e);
}
private static Employee[] readData(Scanner in)
{
int n=0;Employee[] employees=new Employee[n];
if(in.hasNextInt())
{ n=in.nextInt();
in.nextLine();
for(int i=0;i<n;i++)
employees[i]=readEmployee(in);}
else
in.close();
return employees;
}
public static void writeEmployee(PrintWriter out, Employee e)
{
GregorianCalendar calendar=new GregorianCalendar();
calendar.setTime(e.getHireDay());
out.println(e.getName()+"|"+e.getSalary()+"|"+calendar.get(Calendar.YEAR)+"|"+(calendar.get(Calendar.MONTH)+1)+"|"+calendar.get(Calendar.DAY_OF_MONTH));
}
public static Employee readEmployee(Scanner in){
String line=null;
if(in.hasNextLine()){
line=in.nextLine();
String[] tokens=line.split("\\|");
String name=tokens[0];
double salary=Double.parseDouble(tokens[1]);
int year=Integer.parseInt(tokens[2]);
int month=Integer.parseInt(tokens[3]);
int day=Integer.parseInt(tokens[4]);return new Employee(name,salary,year,month,day);}
else
{
in.close();
return null;
}
}
}
Eclipse环境下的,控制台是这样显示的:
No conaoles to display at this time.
烦请各位大神赐教。