java文件读写和properties配置文件

流的分类

image-20230322231812492

对于文本文件,选择带缓冲的字符流BufferedReader和BufferedWriter。

写文件的demo

private static void writeFile() {
        // 写文件
        File file = null;
        BufferedWriter bw = null;
        try {
            String filePath = "demo" + File.separator + "abc.txt";
            file = new File(filePath);
            bw = new BufferedWriter(new FileWriter(file));
            bw.write("zhangsan:24");
            bw.newLine();
            bw.write("lisi:26");
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

读文件的demo

private static void readFile() {
        // 读文件
        File file = null;
        BufferedReader br = null;
        try {
            String filePath = "demo" + File.separator + "abc.txt";
            file = new File(filePath);
            br = new BufferedReader(new FileReader(file));
            String line=null;
            while ((line=br.readLine())!=null){
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

properties文件

特别的,properties文件一般用于做配置文件。

image-20230322233334547

properties继承于Hashtable,但有其特有的方法。

private static void properties1() {
        //创建集合对象
        Properties prop = new Properties();

        //存储元素
        prop.setProperty("zhangsan", "男");
        prop.setProperty("lisi", "女");
        prop.setProperty("wangwu", "男");

        //遍历集合
        Set<String> keySet = prop.stringPropertyNames();
        for (String key : keySet) {
            String value = prop.getProperty(key);
            System.out.println(key + "," + value);
        }
    }

跟文件结合到一起。

写properties文件

private static void storeProperties() {
        Properties prop = new Properties();

        //存储元素
        prop.setProperty("zhangsan", "男");
        prop.setProperty("lisi", "女");
        prop.setProperty("wangwu", "男");
        String filePath = "demo" + File.separator + "abc.properties";
        FileWriter fw = null;
        try {
            fw = new FileWriter(filePath);
            prop.store(fw, null);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

读properties文件,用try-with-resource写

private static void loadProperties() {
        Properties prop = new Properties();
        String filePath = "demo" + File.separator + "abc.properties";
        try {
            FileReader fr = new FileReader(filePath); // 可能会抛FileNotFoundException
            prop.load(fr); // 可能会抛IOException
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(prop);
    }

properties文件

#Wed Mar 22 23:41:42 CST 2023
lisi=女
zhangsan=男
wangwu=男
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值