java IO 流的学习(我们到底能走多远系列1)

我们到底能走多远系列(1)

“我们到底能走多远系列”:开始我的java之路。我要挖出这个地道,离开这里。

IO入门代码阅读:

字节流:

复制代码
    private void writeTxt(String path, String value) throws IOException{
        OutputStream fos = new FileOutputStream(path);//构造方法1
        fos.write(value.getBytes());
        fos.close();
    }
    private void readTxt(String path) throws IOException{
        File file = new File(path);
        InputStream fis = new FileInputStream(path);//构造方法2
        byte b[] = new byte[(int)file.length()] ;
        fis.read(b);
        System.out.print(new String(b));
        fis.close();
    }
复制代码

字符流:

复制代码
    private void writeTxt(String path, String value) throws IOException{
        Writer writer = new FileWriter(path);
        writer.write(value);
        writer.close();
    }
    
    private void readTxt(String path) throws IOException{
        Reader reader = new FileReader(path);
        char c[] = new char[1024] ;
        int len = reader.read(c);
        System.out.print(new String(c, 0, len));
        reader.close();
    }
复制代码

posted on   每当变幻时  阅读(1015)  评论(1编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2012年8月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示