IO流

1.1字节输出流

/**
     * 字节输出流:FileOutPutStream
     * 1. 如果文件不存在,输出流会自动创建该文件.
     * 2. 如果文件的父目录不存在,输出流不会创建文件夹,因此文件会创建失败!
     */
    //创建文件对象
    File file = new File("D:/test/test.txt");

    //判断文件对象的父目录是否存在
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs(); //父目录不存在,则创建

        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            String data = "HelloWord";
            outputStream.write(data.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace(); //输出流关闭异常
            }
            }
        }
    }

1.2字节输入流

/**
         * 字节输入流:
         */
        InputStream in = null;
        try {
            //创建字节输入流对象
            in = new FileInputStream("D:/test/test.txt");

            int line = -1;
            while ((line = in.read()) != -1) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

1.3前面两种字节流的高级用法(缓冲区用法)

/**
         * 缓冲区结合字节输出输入流的使用
         */
        BufferedInputStream bis = null;
        BufferedOutputStream bos =null;
        try {
            InputStream in = new FileInputStream("D:/test/test.txt");
            //创建缓冲字节输入流
            bis = new BufferedInputStream(in);

            //创建一个文件字节输出流对象,关联目标文件
            File file = new File("D:/test/demo.txt");
            //判断文件路径是否存在
            if (!file.getParentFile().exists())
                file.getParentFile().mkdirs();

            OutputStream out = new FileOutputStream(file);
            //创建缓冲字节输出流
            bos = new BufferedOutputStream(out);

            //读取与写入
            byte[] buf = new byte[1024]; //这里可以填写1024的整数倍
            int line = -1;
            while ((line = bis.read(buf)) != -1) {
                bos.write(buf, 0, line);
                bos.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

2.下面介绍字符输出输入流的使用,其实和字节流的使用差不多,我直接使用它们的高级用法(缓冲区)

/**
         * 使用缓冲区字符输入输出流把文件复制到另一个文件
         */
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //创建字符缓冲输入流,读数据(编码格式可以不写,一般使用默认的)
            br = new BufferedReader(new InputStreamReader(new FileInputStream("D:/test/test.txt"), "UTF-8"));

            //创建一个文件字节输出流对象,关联目标文件
            File file = new File("D:/test/demo.txt");
            //判断文件路径是否存在
            if (!file.getParentFile().exists())
                file.getParentFile().mkdirs();

            //创建字符缓冲输出流,写数据
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));

            //读取和写入
            String line = null;
            while ((line = br.readLine()) != null) {

                //写入文件
                bw.write(line);
                //刷新缓存
                bw.flush();
                //换行
                bw.newLine();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

3.关于一些字节流和字符流的总结,希望对大家有用

"流总结 :"

1. "字节流 :"

InputStream : 字节输入流

  |-- FileInputStream

  |-- BufferedInputStream

OutputStream : 字节输出流

  |-- FileOutputStream

  |-- BufferedOutputStream

2. "字符流 :"

Reader : 字符输入流

  |-- InputStreamReader : InputStream + 任意编码表

    |-- FileReader : InputStreamReader + 默认编码表 (便捷类) 字节流 + 编码表

  |-- BufferedReader

Writer : 字符输出流

  |-- OutputStreamWriter : OutputStream + 任意编码表

    |-- FileWriter : OutputStreamWriter + 默认编码表 (便捷类)字节流 + 编码表

  |-- BufferedWriter

3."便捷类 和 父类 之间的关系 :"

"父类 :"

1. OutputStreamWriter = OutputStream + 任意编码表

2. InputStreamReader = InputStream + 任意编码表

"子类 :"

1. FileWriter = FileOutputStream + 默认编码表

2. FileReader = FileInputStream + 默认编码表

"总结 : 一般我们都使用子类,便捷类,当需要指定编码表的时候,才会使用父类 (转换流)"

 

"请问 : 如何选择使用那种流呢?"

"问题1 : 读取字节数据还是字符数据?"

    字节 : BufferedInputStream / BufferedOutputStream

    字符 : BufferedReader / BufferedWriter

"问题2 : 我是读还是写?"

    读 : BufferedInputStream / BufferedReader

    写 : BufferedOutputStream / BufferedWriter

"问题3 : 是否需要高效缓冲呢 ?" 当然要.

    是 : 就用对应体系的缓冲流包起来. 缓冲流就是用来装饰其它流的.所以,要加缓冲,就用缓冲流包装其它流.

"问题4 : 是否需要指定编码表?"

 需要 , 转换流. OutputStreamWriter & InputStreamReader

不需要 , 便捷流. FileWriter & FileReader

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值