【Java读写文件】

Java学习笔记

1.输入输出流

输入流(InputStream)表示从一个源读取数据,输出流(OutputStream)表示向一个目标写数据

使用输入输出流读写数据
输出流写入文件
       public static void main(String[] args) **throws Exception**{    
       
        File f = new File("a.txt");
        FileOutputStream fop = new FileOutputStream(f);
        OutputStreamWriter bw = new OutputStreamWriter(fop, "UTF-8");
        
        // 写入字符
        bw.writer("this is a simaple test");
        
        // 追加字符
        bw.append("中文输入");
        bw.append("\r\n");
        bwr.append("English");
        
        bw.close();
        fop.close();
        }
输入流读取文件
        FileInputStream fip = new FileInputStream(f);

        InputStreamReader reader = new InputStreamReader(fip,"UTF-8");

        BufferedReader br = new BufferedReader(reader);

        StringBuffer sb = new StringBuffer();

        String str;
        while(( str = br.readLine())!= null){
            sb.append(str);
            System.out.println(str);
        }
        
        System.out.println(sb);
        br.close();
        fip.close();

  • FileInputStream,FileReader,BufferedReader是用来读取文件的类。

  • FileOutputStream,FileWriter,BufferedWriter是用来写入文件的类。

  • 使用BufferedReader和BufferedWriter可以提高读写文件的效率,因为它们使用了缓冲区

  • InputStreamReader的read()方法返回的是一个整数,该方法读取类型为字符类型,这个整数表示字符的Unicode值

2.读写yaml文件
引入包
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>2.10.0</version>
</dependency>
读写yaml文件
       ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        Map<String,Object> yamlData = mapper.readValue(new File("mydata.yaml"), Map.class);
        
        //输出数据
        System.out.println(yamlData);
        
        // 使用for循环遍历数据
         for(String str:yamlData.keySet()){
            System.out.print(str +  "   value="+ yamlData.get(str));
            System.out.println();
        }
        for(Object value: yamlData.values()){
            System.out.println(value + "---------");
        }
        
        //使用forEach()遍历
        yamlData.forEach((key,vlaue) -> {
            System.out.print("key " + key + "   value=" + vlaue);
            System.out.println();
            System.out.println("---------");
        });

        yamlData.put("newKey", "newValue");
        mapper.writeValue(new File("mydata.yaml"), yamlData);
3读写Json文件
引入包
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.0</version>
</dependency>
读写json文件
        // 读取json 文件中的数据
        ObjectMapper mapper = new ObjectMapper();
        Map<String,Object> jsonData = mapper.readValue(new File("mydata.json"), Map.class);
        System.out.println(jsonData);

        jsonData.put("newKey", "newValue");
        mapper.writeValue(new File("mydata.json"), jsonData);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值