概述
本例来源于java核心技术卷二
文本存储
- 一、效果
- 二、编写代码
- 1.Employee类
- 2.TextFileTest类
- 2.完整代码
- 总结
一、效果
将数据写入文本中,采用统一格式进行存储例如:
二、编写代码
1.Employee类
读的readData方法:
public void readData(Scanner in){
String line =in.nextLine();
String[] tokens=line.split("|");
name=tokens[0];
salary=Double.parseDouble(tokens[1]);
int y=Integer.parseInt(tokens[2]);
int m=Integer.parseInt(tokens[3]);
int d=Integer.parseInt(tokens[4]);
GregorianCalendar calendar=new GregorianCalendar(y,m-1,d);
hireDay=calendar.getTime();
}
写的writeData方法:
public void writeData(PrintWriter out){
GregorianCalendar calendar=new GregorianCalendar();
calendar.setTime(hireDay);
out.println(name+"|"+salary+"|"+calendar.get(Calendar.YEAR)+"|"+(calendar.get(Calendar.MONTH)+1)+"|"+calendar.get(Calendar.DAY_OF_MONTH));
}
2.TextFileTest类
私有方法Employee[] readData:
private static Employee[] readData(Scanner in){
int n=in.nextInt();
in.nextInt();
Employee[] employee=new Employee[n];
for (int i=0;i<n;i++){
employee[i]=new Employee();
employee[i].readData(in);
}
return employee;
}
私有方法writeData:
private static void writeData(Employee[] employees,PrintWriter out)throws IOException{
out.println(employees.length);
for (Employee e:employees)
e.writeData(out);
}
2.完整代码
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class TextFileTest {
public static void main(String[] args) {
Employee[] staff=new Employee[2];
staff[0]=new Employee("shuxin",1000,2018,1,25);
staff[1]=new Employee("lin",23000,2017,13,50);
try {
PrintWriter out=new PrintWriter("E:\OK.txt");
writeData(staff,out);
out.close();
Scanner in=new Scanner(new FileReader("E:\OK.txt"));
Employee[] newstaff=readData(in);
in.close();
for (Employee e:newstaff)
System.out.println(e);
}catch (IOException exception){
exception.printStackTrace();
}
}
private static Employee[] readData(Scanner in){
int n=in.nextInt();
in.nextInt();
Employee[] employee=new Employee[n];
for (int i=0;i<n;i++){
employee[i]=new Employee();
employee[i].readData(in);
}
return employee;
}
private static void writeData(Employee[] employees,PrintWriter out)throws IOException{
out.println(employees.length);
for (Employee e:employees)
e.writeData(out);
}
// private static Employee[] readData(Scanner in){
// int n=in.nextInt();
// in.nextLine();
//
// Employee[] employees=new Employee[n];
// for (int i =0;i<n;i++){
// employees[i]=new Employee();
// employees[i].readData(in);
// }
// return employees;
// }
}
class Employee{
private String name;
private double salary;
private Date hireDay;
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 toString(){
return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
}
public void readData(Scanner in){
String line =in.nextLine();
String[] tokens=line.split("|");
name=tokens[0];
salary=Double.parseDouble(tokens[1]);
int y=Integer.parseInt(tokens[2]);
int m=Integer.parseInt(tokens[3]);
int d=Integer.parseInt(tokens[4]);
GregorianCalendar calendar=new GregorianCalendar(y,m-1,d);
hireDay=calendar.getTime();
}
public void writeData(PrintWriter out){
GregorianCalendar calendar=new GregorianCalendar();
calendar.setTime(hireDay);
out.println(name+"|"+salary+"|"+calendar.get(Calendar.YEAR)+"|"+(calendar.get(Calendar.MONTH)+1)+"|"+calendar.get(Calendar.DAY_OF_MONTH));
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
}
总结
写代码一定要写注释!写这个的时候我在中途停了一段时间,后来继续在写的时候,发现我忘了之前写的思路是什么了。
最后
以上就是傲娇电话为你收集整理的文本存储实例一、效果二、编写代码总结的全部内容,希望文章能够帮你解决文本存储实例一、效果二、编写代码总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复