工具类-Properties

这篇博客展示了如何使用Java将ArrayList中的学生姓名和年龄信息存储到properties文件中。通过创建Properties对象,遍历学生列表并利用setProperty方法,然后使用PrintWriter将属性列表写入文件,实现了数据持久化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将lisi写入文件

自动换行:setProperty可以连续set会

public class InputArrayObject {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("张三",18));
        list.add(new Student("李四",19));
        list.add(new Student("王五",20));
        list.add(new Student("赵六",21));
        Properties properties = new Properties();

        PrintWriter printWriter = new PrintWriter("project/Test/student.properties");
        for (Student student : list) {
            properties.setProperty(student.getName(),String.valueOf(student.getAge()));
        }
        
        properties.list(printWriter);
        printWriter.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值