Java基础-IO流-字节输出流

*IO流概述
* I表示input,是数据从硬盘文件读入到内存的过程,称之输入,负责读(磁盘————>内存)
* O表示output,是内存程序的数据从内存到写出到硬盘文件的过程,称之为输出,负责写(磁盘<————内存)
*IO流的分类
*    按流的方向分
*            IO流——输入流|输出流
*     按流中的数据最小单位分为
*                       (抽象类)            IO流——字节流|字符流
*                                         |                    |
*  操作所有类型的文件(抽象类)(InputStream,OutputStream)      只能操作纯本文件(Reader Writer)
*   (实现类)    FileInputStream     FileOutputStream        FileReader    FileWriter
*                   字节输入流     字节输出流                    字符输入流  字符输出流
*
* 总结流的四大类:
*    字节输入流:以内存位基础,来自磁盘文件/网络中的数据以字节的形式读入到内存中去的流称为字节输入流
*    字节输出流:以内存位基础,把内存中的数据以字节写出到磁盘文件或者网络中去的流称之为字节输出流
*    字符输入流:以内存位基础,来自磁盘文件/网络中的数据以字符的形式读入到内存中去的流称为字符输入流
*    字节输出流:以内存位基础,把内存中的数据以字符写出到磁盘文件或者网络中去的流称之为字符输出流
*
*
* IO流的作用?
*    读写文件数据的;
* IO流怎么划分的,大体分为几类?各自的作用
*
*
* 文件字节输入流(read)存在问题:(读字节)
* 1.性能慢(一个一个读).
* 2.读取中文无法避免乱码)(每次只读一个字节,中文一个字有3个字节在UTF-8)
*
* (读字节数组)read(byte[]buffer)
* 1.读取性能得到提升
* 2.读取中文字符输出还是无法避免乱码的问题
******************************************
* 如何避免中文乱爬:
* 定义一个与文件一样大的字节数组,一次读取完文件的全部字符(byte[]buffer=new byte[1024])
* 官方为字节输入流InputStream提供了如下API可以把文件的全部数据读取到一个字节数组中(jdk9)(readAllByte)
*还是存在问题:
* 如果文件过大,字节数组可能引起内存溢出
* **********************************************************
*
*
*
*
*
*
*
*
*
* */
public class IOKnowledge {
    public static void main(String[] args) throws Exception {
test4();
    }
    public static void test1() throws Exception  {
//创建一个文件输入流管道与源文件接通
        InputStream is=new FileInputStream(new File("d:/IO流.txt"));
        //读取一个字节返回(每次读1个)
     /*   int b1= is.read();
        System.out.println((char)b1);
        int b2= is.read();
        System.out.println((char)b2);*/

        //循环改进
        //定义一个变量记录每次读取的字节
        //容易乱码,不能读中文
        int b;
        while ((b=is.read())!=-1){
            System.out.print((char)b);
        }
    }
    public static  void test2() throws Exception {
        //使用文件输入流 每次读取一个字节数组的数据
        //创建一个文件输入流管道与源文件接通(管道is)
        InputStream is=new FileInputStream(new File("d:/IO流.txt"));
        //定义一个字节数组,用来读取字节数组
        byte[]buffer=new byte[3];
        int len = is.read(buffer);
        System.out.println("读取了几个字节:"+len);
        String rs=new String(buffer);
        System.out.println(rs);
        //buffer=[ab3]
        int len1= is.read(buffer);
        System.out.println("读取了几个字节:"+len1);
        String rs1=new String(buffer);
        System.out.println(rs1);
         //buffer=[abc]


        //buffer=[abc](只有2个。所以cd,第三个位置)
          //     cd+第二个里的a
        //所以是bug
        int len2 = is.read(buffer);
        System.out.println("读取了几个字节:"+len2);
        //读取多少,出来多少;0下标起始,length,读取长度
        String rs2=new String(buffer,0,2);
        System.out.println(rs2);

        int len3 = is.read(buffer);
        System.out.println("读取了几个字节:"+len3);
      /*  String rs3=new String(buffer);
        System.out.println(rs3);*/

    }
    public static  void test3() throws Exception {
        InputStream is=new FileInputStream(new File("d:/IO流.txt"));
        //改进使用循环,每次读取一个字节数组(还是避免不了中文乱码)
        byte[]buffer=new byte[3];
        int len;//记录每次读取的字节数
        while ((len=is.read(buffer))!=-1){
            //都多少取多少
            System.out.print(new String(buffer,0,len));
        }

    }
    public static  void test4() throws Exception {
        //  定义一个与文件一样大的字节数组,一次读取完文件的全部字符
        //拿文件大小 用File
        File file=new File("d:/IO流.txt");
        InputStream is=new FileInputStream(file);
        byte[]buffer=new byte[(int)file.length()];
       int len= is.read(buffer);
        System.out.println("读取字节:"+len);
        System.out.println("文件大小:"+file.length());
        System.out.println(new String(buffer));
      //byte[]buffer= is.readAllBytes();//JDK9出现,可以直接读取全部字节

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值