javaSE io_2 配置文件的读取

package LS_进阶02;

import java.io.*;
import java.util.Properties;

/**
 * 测试配置文件
 */
public class TestProperties {
    public static void main(String[] args) {
        File file = new File("test.properties");
        OutputStream out = null;
        InputStream in = null;
        try {
            out = new FileOutputStream(file);
            //设置配置文件流
            Properties prop = new Properties();
            //添加键值对
            prop.setProperty("name","张三");
            prop.setProperty("age","13");
            //写入配置文件:
            prop.store(out,"添加姓名的操作");

            System.out.println("写入成功");


            //读取
            in = new FileInputStream(file);
            prop.load(in);
            System.out.println(prop.getProperty("name"));


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 就是实现,将数据写入和读出到配置文件中

posted @ 2021-01-30 12:08  赚钱买个打印机  阅读(35)  评论(0编辑  收藏  举报