InputStream和OutputStream

字节输入流和字节输出流:


    /*
        *   字节输入流和字节输出流可以操作任何文件 但是是以字节读取文件
        *   read()读取的是一个字节,为什么返回int,而不是byte
        *
        *   因为读到中间可能遇到11111111(8个1),这11111111表示byte类型的-1,就不会读取后面的数据了,
        *   所以读取的时候用int接受将11111111在前面添16个0,补足24位,那么byte类型的-1就变成了int的255,
        *   就可以保证数据读完,而结束的标记-1就是int型
     */

    @Test
    public void demo() throws Exception{
        //"xx01.txt"表示当前项目路径下的文件,xxx.txt必须存在,要不就会报错
        FileInputStream file=new FileInputStream("xx01.txt");       //创建流对象

        int b;
        while ((b=file.read())!= -1){               //文件结束的标志是-1,,read()函数自动指向下一位
            System.out.println(b);
        }
        file.close();                      //关流释放资源
    }

    //上面输出97 98 99
    /*
     *   FileOutputStream 在new的时候,若文件不存在,就创建一个,
     *    若存在先将文件清空;如果想续写就 new FileOutputStream("yyy.txt",true);
     */

    @Test
    public void demo2() throws Exception{
        //保证路径存在即可,文件不存在就会创建一个
        FileOutputStream file = new FileOutputStream("yyy.txt");
        file.write(97);
        file.write(98);
        file.close();
    }
  1. 文件的拷贝(效率较慢):以下代码是一个字节一个字节读取,再一个字节一个字节写入,效率较慢,
        /*
         *  IO流的核心代码,读取一个文件内容,写入另一个文件
         */
        @Test
        public void demo3() throws Exception{
            FileInputStream fis=new FileInputStream("xx01.txt");    //文件可以是txt,照片.jpg  或者其他
            FileOutputStream fos = new FileOutputStream("yyy.txt");
    
            int b;
            while ((b=fis.read())!= -1){
                fos.write(b);
            }
    
            fis.close();
            fos.close();
    }
        
  2. 文件的拷贝(效率你上一个快,但不推荐使用,容易内存溢出):将文件一次性读取到字节数组中,在一次性写入按时
        /*
         *  IO流的核心代码,一次性读取一个文件内容,再全部写入另一个文件
         */
        @Test
        public void demo4() throws Exception {
            FileInputStream fis = new FileInputStream("xx01.txt");    //文件可以是txt,照片.jpg  或者其他
            FileOutputStream fos = new FileOutputStream("yyy.txt");
    
            byte[] file = new byte[fis.available()];          //创建一个与文件一样大小的字节数组
            fis.read(file);                                  //将文件的内容全部读取到字节数组中
            fos.write(file);                                 //将字节数组的内容写入到另一个文件中
    
            fis.close();
            fos.close();
    
    
        }
  3. 标准的拷贝格式(定义小数组):
    
        /*
         *  定义小数组的标准格式,一般长度是1024的整数倍
         */
        @Test
        public void demo5() throws Exception {
            FileInputStream fis = new FileInputStream("xx01.txt");    //文件可以是txt,照片.jpg  或者其他
            FileOutputStream fos = new FileOutputStream("yyy.txt");
    
            byte[] file = new byte[1024 * 8];          //防止内存溢出
            int len;                                    //表示新读取的字节长度,保证不会重复
            while ((len=fis.read(file))!=-1){           //忘记加file,返回的不是字节个数,而是字节的码表值
                fos.write(file,0,len);
            }
    
            fis.close();
            fos.close();
        }
  4.  缓冲区实现

    
        /*
         *  BufferedInputStream和BufferedOutputStream
         *  字节流缓冲区
         */
        @Test
        public void demo6() throws Exception {
            FileInputStream fis = new FileInputStream("xx01.txt");      //穿件文件输入流对象
            BufferedInputStream bfis=new BufferedInputStream(fis);               //创建缓冲区对fis修饰,默认创建byte[8192]数组
            FileOutputStream fos = new FileOutputStream("yyy.txt");
            BufferedOutputStream bfos=new BufferedOutputStream(fos);
    
            int b;
            while ((b=bfis.read())!=-1){
                bfos.write(b);
            }
    
            bfis.close();
            bfos.close();
        }

    JDK1.7新特性

        /*
         *  异常处理,JDK1.7版本的新特性
         */
        @Test
        public void demo7()throws Exception{
            try(
                    FileInputStream fis = new FileInputStream("xx01.txt");      //具有自动关闭的功能(继承了Closeable),{}运行完之后,会自动将()里的流关闭
                    FileOutputStream fos = new FileOutputStream("yyy.txt");
                    ){
                int b;
                while ((b = fis.read()) != -1) {
                    fos.write(b);
                }
            }
        }
    

猜你喜欢

转载自blog.csdn.net/weixin_42547717/article/details/83964153