Java的简单理解(21)---IO的字节流和字符流

Java

字节流: 可以处理一切文件,包括二进制,音频,视频,doc等等。
字符流: 只能处理纯文本,全部为可见字符。.txt .html

1. 字节流

1. 文件的读取
    public void Input() {
        // 1. 建立联系 File对象
        File src = new File("E:/xp/test/a.txt");
        // 2. 选择流
        InputStream is = null; // 提升作用域
        try {
            is = new FileInputStream(src);
            // 3. 操作 不断读取,缓冲数组
            byte[] car = new byte[1024];
            int len = 0; // 实际读取的长度

            // 循环读取,还回实际读取的长度,空的话还回-1
            while ((len = is.read(car)) != -1) {
                // 输出 字节数组转成字符串
                String info = new String(car,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件不存在!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败!");
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件流关闭失败!");
                }
            }
        }
    }
2. 文件的写出
    public void Output() {
        // 1. 建立联系 File对象 目的地
        File dest = new File("E:/xp/test/a.txt");
        // 2. 选择流,文件输出流 OutputStream,FileOutputStream
        OutputStream os = null;
        try {
            // true,以追加方式写出文件
            os = new FileOutputStream(dest,true);
            // 操作
            String str = "WM is a good man!";
            // 字符串转字节数组
            byte[] data = str.getBytes();
            os.write(data,0,data.length);

            os.flush(); // 强制刷新出去
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件未找到!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件写出失败!");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件写出失败!");
                }
            }
        }
    }
3. 文件的拷贝
    /**
     * 文件的拷贝
     * @throws IOException
     */
    public void copyFile() throws IOException {

        // 1. 建立联系 源文件+目的文件
        File src = new File("E:/xp/test/1.jpg");
        File dest = new File("E:/xp/test/2.jpg");

        // 2. 选择流
        InputStream is = new FileInputStream(src);
        OutputStream os = new FileOutputStream(dest);

        byte[] car = new byte[1024];
        int len = 0;

        // 读取
        while ((len = is.read(car)) != -1) {
            // 写出
            os.write(car,0,len);
        }
        os.flush(); // 强制刷出
        os.close(); // 后出现的先关闭
        is.close();

    }

2. 字符流

1. 纯文本读取
public void reader() {
    // 创建源
    File file = new File("E:/xp/test/a.txt");
    // 选择流
    Reader reader = null;

    try {
        reader = new FileReader(file);
        // 用于读取时存放的
        char[] car = new char[1024];
        int len = 0;
        while ((len = (reader.read(car))) != -1) {
            // 字符数组转换为字符串
            String string = new String(car,0,len);
            System.out.println(string);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("源文件不存在");
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
2. 纯文本的写入
public void write() {
    // 创建源
    File file = new File("E:/xp/test/b.txt");
    // 选择流
    Writer writer = null;

    try {
        // true: 追加文件而不是覆盖文件,默认是false
        writer = new FileWriter(file, true);
        // 写出
        // \r\n: 换行
        String msg = "锄禾日当午\r\n汗滴禾下土\r\n";
        writer.write(msg);
        writer.append("谁知盘中餐");

        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容