Java II:2.1.7、以文本格式存储对象

40 篇文章 0 订阅
17 篇文章 0 订阅
该博客介绍了如何使用Java将对象以文本格式存储到文件中,并从文件中读取。主要涉及Employee类实例的创建、序列化和反序列化过程,使用了SimpleDateFormat和Scanner/PrintWriter进行日期格式化和文件I/O操作。
摘要由CSDN通过智能技术生成

以文本格式存储对象

主程序:

package xyz.jangle.javabase.n2.b1;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

/**
 * 	2.1.7、以文本格式存储对象
 * @author jangle
 * @email jangle@jangle.xyz
 * @time 2021年2月13日 上午10:29:49
 * 
 */
public class M5 {

	public static void main(String[] args) throws IOException {
		Employee eArr[] = new Employee[3];
		eArr[0] = new Employee("名称", 11999, new Date());
		eArr[1] = new Employee("名称2", 11998, new Date());
		eArr[2] = new Employee("名称3", 11997, new Date());
		
		try(var out = new PrintWriter(Files.newOutputStream(Paths.get(M3.pathFileStr)))				)
		{
			writerDate(eArr, out);
		}
		try(var in = new Scanner(Files.newInputStream(Paths.get(M3.pathFileStr)))){
			Employee[] employees = readData(in);
			for (Employee employee : employees) {
				System.out.println(employee.getName()+"|"+employee.getSalary()+"|"+employee.getHireDay());
			}
		}
	}
	
	private static void writerDate(Employee[] employees, PrintWriter out) {
		for (Employee employee : employees) {
			writerEmployee(employee, out);
		}
	}
	
	private static void writerEmployee(Employee e, PrintWriter out) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = sdf.format(e.getHireDay());
		out.println(e.getName()+"|"+e.getSalary()+"|"+format);
	}
	
	private static Employee readEmployee(Scanner in) {
		String line = in.nextLine();
		String[] tokens = line.split("\\|");
		String name = tokens[0];
		Integer salary = Integer.valueOf(tokens[1]);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(tokens[2]);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Employee(name,salary,date);
	}
	
	private static Employee[] readData(Scanner in) {
		var list = new ArrayList<Employee>();
		int i = 0;
		for (;in.hasNext();i++) {
			list.add(readEmployee(in));
		}
		return list.toArray(new Employee[i]);
	}

}

雇员类:


package xyz.jangle.javabase.n2.b1;

import java.util.Date;

/**
 * @author jangle
 * @email jangle@jangle.xyz
 * @time 2021年2月13日 上午10:31:47
 * 
 */
public class Employee {
	
	private String name;
	
	private Integer salary;
	
	private Date hireDay;

	public Employee(String name2, Integer salary2, Date date) {
		this.name = name2;
		this.salary = salary2;
		this.hireDay = date;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getSalary() {
		return salary;
	}

	public void setSalary(Integer salary) {
		this.salary = salary;
	}

	public Date getHireDay() {
		return hireDay;
	}

	public void setHireDay(Date hireDay) {
		this.hireDay = hireDay;
	}
	
	
	
	

}

读写二进制数据:https://blog.csdn.net/Bof_jangle/article/details/113805493

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值