IO流_字节流

IO流_字节流

/**
 * 一、流的分类:
 * 1.操作的数据单位:字节流、字符流
 * 2.数据的流向:输入流、输出流
 * 3.流的角色:节点流、处理流
 *
 * 二、流的体系结构
 * 抽象基类             节点流(或文件流)            缓冲流(处理流的一种)
 * InputStream         FileInputStream           BufferedInputStream
 * OutputStream        FileOutputStream          BufferedOutputStream
 * Reader              FileReader                BufferedReader
 * Writer              FileWriter                BufferedWriter
 */

FileInputStreamAndOuputStream

  • 使用字节流FileInputStreamch处理文本文件,可能出现乱码

  • FileInputStreamAndOuputStream读写非文本文件

    示例代码:

/*
实现对图片的复制操作
 */
public class testIOStream {
    public static void main(String[] args){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //造文件
            File src = new File("基础语法\\沙滩.jpg");
            File dest = new File("基础语法\\沙滩2.jpg");
            //造流
            fileInputStream = new FileInputStream(src);
            fileOutputStream = new FileOutputStream(dest);
            //复制过程
            byte[] buffer = new byte[5];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1){
                fileOutputStream.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭操作
            try {
                if (fileInputStream != null)
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileOutputStream != null)
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

posted @ 2020-07-09 15:47  邱大将军  阅读(69)  评论(0编辑  收藏  举报