Java文本类型输入与输出
2024-09-01 03:06:10
import java.io.*; import java.time.LocalDate; import java.util.Scanner; public class Test { public static void main(String[] args){ // TextFileTest.test(); // RandomAccessTest.test(); } } /* 2.2.3 以文本格式储存对象 */ class Employee { private String name; private double salary; private LocalDate hireDay; public static final int NAME_SIZE = 30; public static final int RECORD_SIZE = 50; public Employee(String name, double salary, LocalDate hireDay) { this.name = name; this.salary = salary; this.hireDay = hireDay; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public LocalDate getHireDay() { return hireDay; } public void setHireDay(LocalDate hireDay) { this.hireDay = hireDay; } @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", salary=" + salary + ", hireDay=" + hireDay + '}'; } } class TextFileTest { public static void test() { Employee[] staff = new Employee[]{ new Employee("A", 10, LocalDate.now()), new Employee("B", 20, LocalDate.now()), new Employee("C", 30, LocalDate.now()) }; try(PrintWriter out = new PrintWriter("employee.dat","UTF-8")) { writeData(staff,out); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try (Scanner in = new Scanner(new FileInputStream("employee.dat"))) { Employee[] newStaff = readData(in); for (Employee e : newStaff) { System.out.println(e); } } catch (FileNotFoundException e) { e.printStackTrace(); } } private static void writeData(Employee[] employees, PrintWriter out) { out.println(employees.length); for (Employee e : employees) { writeEmployee(out,e); } } private static void writeEmployee(PrintWriter out, Employee employee) { out.println(employee.getName()+"|"+employee.getSalary()+"|"+ employee.getHireDay()); } 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] = readEmployee(in); } return employees; } private static Employee readEmployee(Scanner in) { String line = in.nextLine(); String[] tokens = line.split("\\|"); String name = tokens[0]; double salary = Double.parseDouble(tokens[1]); //*****************有趣的写法**************** LocalDate hire = LocalDate.parse(tokens[2]); //****************************************** return new Employee(name, salary, hire); } }
《Java核心技术卷二》笔记
最新文章
- 关于C语言宏定义 使用do{ xxxx }while()
- android 数据存储Ⅱ
- HDU 1548 A strange lift (最短路/Dijkstra)
- Raising Modulo Numbers
- Python ~~~ 面向对象的利器
- 李洪强iOS开发支付集成之银联支付
- ";Money, Money, Money";
- java 连接sql server2008配置
- [置顶] 与小伙伴共勉的java有关jvm的知识(一),小鸟尽量写得详细哦,欢迎讨论,谢绝喷子
- 《JavaScript权威指南》拾遗(下)
- 火狐浏览器打开html文件,中文乱码
- PowerDesigner设置null约束
- python静态属性的理解
- Android 音视频深入 四 录视频MP4(附源码下载)
- linux设置系统时间和时区
- C内存开辟与平移
- [Android 动画]简要分析一下Animator 与 Animation
- 【HTML入门】Html中块状元素和内联元素解析
- SpringCloud 入门
- 安装webpack命令环境