zoukankan      html  css  js  c++  java
  • 2019-5-29 Java学习日记之IO(字节流)

    IO流概述及其分类:

    A:概念

    IO流用来处理设备之间的数据传输

    java对数据的操作是通过流的方式

    java用于操作流的类都在IO包中

    流按流向分为两种:输入流和输出流

    流按照操作类型分为两种:

    字节流:字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的

    字符流:字符流只能操作纯字符数据,比较方便

    B:IO流常用父类

    字节流的抽象父类:

    InputStream

    OutputStream

    字符流的抽象父类:

    Reader

    Writer

    C:IO程序书写

    使用前,导入IO包中的类

    使用时,进行IO异常处理

    使用后,释放资源

    FileIputStream:

    read()一次读取一个字节

    package com.stream;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Demo1 {
    
        public static void main(String[] args) throws IOException {
            FileInputStream fis = new FileInputStream("xxx.txt");
            int b;
            while ((b = fis.read()) != -1) {
                System.out.println(b);
            }
            fis.close();
        }
    
    }

    read()方法返回值为什么是int?

    read()方法读取的是一个字节,为什么返回的是int,而不是byte

      因为字节输入流可以操作任意类型的文件,你如图片音频等,这些文件底层都是以二进制形式存储的,如果每次读取都返回byte,有可能在读到中间的时候遇到11111111

    那么这11111111是byte类型的-1,我们的程序是遇到-1就会停止不读了,后面的数据就读不到了,所以在读取的时候用int类型接收,如果11111111会在其前面补上24个0凑足4个字节,那么byte类型的-1就变成int类型的255了,这样可以保证整个数据读完,而结束标记的-1就是int类型

    FileOutputStream:

    write()方法一次写出一个字节

    package com.stream;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo2 {
        public static void main(String[] args) throws IOException {
            FileOutputStream fos = new FileOutputStream("yyy.txt");        //创建字节输出流对象,如果没有就自动创建一个
            
            fos.write(97);            //虽然写出的是一个int数,但是到文件上的是一个字节,会自动去除前三个8位    
            fos.write(98);
            fos.write(99);
            
            fos.close();
        }
    }

    FileOutputStream追加:

    FileOutputStream在创建对象的时候是如果没有这个文件帮你创建出来,

    如果有这个文件会先把文件清空

    public static void demo2() throws FileNotFoundException, IOException {
            FileOutputStream fos = new FileOutputStream("yyy.txt", true); // 如果想续写就在第二个参数传true
    
            fos.write(97);
            fos.write(98);
    
            fos.close();
        }

    拷贝图片:

    FileInputStream  读取

    FileOutputStream  写出

    public static void demo1() throws FileNotFoundException, IOException {
            FileInputStream fis = new FileInputStream("hh.jpg");        //创建输入流对象,关联hh.jpg
            FileOutputStream fos = new FileOutputStream("copy.jpg");    //创建输出流对象,关联copy.jpg
            
            int b;
            while ((b = fis.read()) != -1) {                            //在不断的读取每一个字节
                fos.write(b);                                            //将每一个字节写出
            }
            
            fis.close();                                                //关流释放资源
            fos.close();
        }

    字节数组拷贝之available()方法:

    弊端:有可能会内存溢出

    public static void demo3() throws FileNotFoundException, IOException {
            FileInputStream fis = new FileInputStream("于文文-稻香.mp3");        //创建输入流对象,关联于文文-稻香.mp3
            FileOutputStream fos = new FileOutputStream("copy.mp3");    //创建输出流对象,关联copy.mp3
            
            byte[] arr = new byte[fis.available()];                        //创建与文件一样大小的字节数组
            fis.read(arr);                                                //将文件上的字节读取到内存中
            fos.write(arr);                                                //将字节数组中的字节数写到文件上
            
            fis.close();                                                //关流释放资源
            fos.close();
        }

    定义小数组:

    public static void demo() throws FileNotFoundException, IOException {
            FileInputStream fis = new FileInputStream("xxx.txt");
            byte[] arr = new byte[2];
            int a = fis.read(arr);                    //将文件上的字节读取到字节数组中
            
            System.out.println(a);                     //读到的有效字节个数
            for (byte b : arr) {                    //第一次获取到文件上的a和b
                System.out.println(b);
            }
            int c = fis.read(arr);
            System.out.println(c);
            for (byte b : arr) {
                System.out.println(b);
            }
            fis.close();
        }
    public static void demo2() throws FileNotFoundException, IOException {
            FileInputStream fis = new FileInputStream("xxx.txt");
            FileOutputStream fos = new FileOutputStream("yyy.txt");
            
            byte[] arr = new byte[2];
            int len;
            while ((len = fis.read(arr)) != -1) {
                fos.write(arr,0,len);
            }
            
            fos.close();
            fos.close();
        }

    定义小数组的标准格式:

    public static void demo3() throws FileNotFoundException, IOException {
            FileInputStream fis = new FileInputStream("于文文-稻香.mp3");
            FileOutputStream fos = new FileOutputStream("copy.mp3");
            
            byte[] arr = new byte[1024 * 8];
            int len;
            while ((len = fis.read(arr)) != -1) {            //如果忘记加arr,返回的就不是读取的字节个数,而是字节的码表值
                fos.write(arr,0,len);
            }
            
            fos.close();
            fos.close();
        }

    BufferedInputStream和BufferOutputStream拷贝:

    A:缓冲思想

    字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多

    这是加入了数组这样的缓冲区效果,java本身在设计的时候,

    也考虑到了这样的设计思想,所以提供了字节缓冲区流

    B:BufferedInputStream

    BufferedInputStream内置了一个缓冲区(数组)

    从BufferedInputStream中读取一个字节时

    BufferedInputStream会一次性从文件中读取8192个

     直到缓冲区中所有的都被使用过,才重新从文件中读取8192个

    C:BufferOutputStream

    BufferOutputStream也内置了一个缓冲区(数组)

    程序向流中写出字节时,不会直接写到文件,先写到缓冲区中

    知道缓冲区写满时,BufferOutputStream才会把缓冲区中的数据一次性写到文件里

    D:Demo

    package com.stream;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo5 {
        public static void main(String[] args) throws IOException {
            FileInputStream fis = new FileInputStream("于文文-稻香.mp3");        //创建输入流对象,关联于文文-稻香.mp3
            FileOutputStream fos = new FileOutputStream("copy.mp3");        //创建输出流对象,关联copy.mp3
            BufferedInputStream bis = new BufferedInputStream(fis);            //创建缓冲区对象,对输入流进行包装让其变得更加强大
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            
            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            
            bis.close();
            bos.close();
        }
    }

    E:小数组的读写和带Buffered的读取哪个更快?

    定义小数组如果是8192个字节大小和Buffered比较的话

    定义小数组会略胜一筹,因为读和写操作的是同一个数组

    而Buffered操作的是两个数组

    flush和close方法的区别:

    flush()方法

    用来刷新缓冲区的,刷新后可以再次写出

    close()方法

    用来关闭流释放资源的,如果是带缓冲区的流对象的close()方法,不但会关闭流,还会在关闭流之前刷新缓冲区,关闭后不能再写出

    字节流读写中文:

    字节流读取中文的问题

    字节流在读中文的时候有可能会读到半个中文,造成乱码

    字节流写出中文的问题

    字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组

    写出回车换行 write(" ".getBytes());

    package com.stream;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo6 {
        public static void main(String[] args) throws IOException {
    //        demo1();
            FileOutputStream fos = new FileOutputStream("zzz.txt");
            fos.write("我读的书少,你不要骗我!".getBytes());
            fos.write("
    ".getBytes());
        }
    
        public static void demo1() throws FileNotFoundException, IOException {
            FileInputStream fis = new FileInputStream("yyy.txt");
            byte[] arr = new byte[4];
            int len;
            while ((len = fis.read(arr)) != -1) {
                System.out.println(new String(arr,0,len));
            }
            fis.close();
        }
    }

    流的标准处理异常代码1.6版本及其以前:

    public static void demo1() throws FileNotFoundException, IOException {
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream("xxx.txt");
                fos = new FileOutputStream("yyy.txt");
    
                int b;
                while ((b = fis.read()) != -1) {
                    fos.write(b);
                }
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                } finally {
                    if (fos != null) { // try finally的嵌套目的是能关一个算一个
                        fos.close();
                    }
                }
            }
        }

    流的标准处理异常代码1.7版本:

    public static void demo2() throws IOException, FileNotFoundException {
            try (FileInputStream fis = new FileInputStream("xxx.txt");
                    FileOutputStream fos = new FileOutputStream("yyy.txt");
                    myClose mc = new myClose();
            ) {
                int b;
                while ((b = fis.read()) != -1) {
                    fos.write(b);
                }
            }
        }
    class myClose implements AutoCloseable {
        public void close() {
            System.out.println("我关了");
        }
    }

    图片加密:

    package com.stream;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Test1 {
        /**
         * 将写出的字节异或上一个数,这个数就是密钥,解密的时候再次异或就可以了
         */
        public static void main(String[] args) throws IOException {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));
            
            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b ^ 123);
            }
            
            bis.close();
            bos.close();
        }
    }

    拷贝文件:

    package com.stream;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Test2 {
        /**
         * 在控制台录入文件的路径,将文件拷贝到当前项目下
         * 
         * 分析:
         * 1、创建键盘录入对象
         * 2、定义方法对键盘录入的路径进行判断,如果是文件就返回
         * 3、在主方法中接收该文件
         * 4、读和写该文件
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            File file = getFile();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
            
            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            
            bis.close();
            bos.close();
        }
        /**
         *定义一个方法获取键盘录入的文件路径,并封装成File对象返回
         *
         *分析:
         *1、返回值类型File
         *2、参数列表无
         */
        public static File getFile(){
            Scanner sc = new Scanner(System.in);                //创建键盘录入对象
            System.out.println("请输入文件的路径:");
            while (true) {
                String line = sc.nextLine();                    //接收键盘录入的路径
                File file = new File(line);                        //封装成File对象,并对其进行判断
                if (!file.exists()) {
                    System.out.println("您录入的文件路径不存在,请重新录入:");
                }else if (file.isDirectory()) {
                    System.out.println("您录入的是文件夹路径,请重新录入:");
                }else {
                    return file;
                }
            }
        }
    }

    录入数据拷贝到文件:

    package com.stream;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Test3 {
        /**
         * 将键盘录入的数据拷贝到当前目录下的text.txt文件中,键盘录入数据遇到quit时退出
         * 
         * 分析:
         * 1、创建键盘录入对象
         * 2、创建输出流对象,关联text.txt文件
         * 3、定义无限循环
         * 4、遇到quit退出循环
         * 5、如果不quit,就将内容写出
         * 6、关闭流
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            //1、创建键盘录入对象
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入数据:");
            //2、创建输出流对象,关联text.txt文件
            FileOutputStream fos = new FileOutputStream("text.txt");
            //3、定义无限循环
            while (true) {
                String line = sc.nextLine();                //将键盘录入的数据存储在line中
                //4、遇到quit退出循环
                if ("quit".equals(line)) {
                    break;
                }
                //5、如果不quit,就将内容写出
                fos.write(line.getBytes());                    //字符串写出必须转换成字节数组
                fos.write("
    ".getBytes());
            }
            //6、关闭流
            fos.close();
        }
    }
  • 相关阅读:
    防删没什么意思啊,直接写废你~
    绝大多数情况下,没有解决不了的问题,只有因为平时缺少练习而惧怕问题的复杂度,畏惧的心理让我们选择避让,采取并不那么好的方案去解决问题
    Java 模拟面试题
    Crossthread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
    一步步从数据库备份恢复SharePoint Portal Server 2003
    【转】理解 JavaScript 闭包
    Just For Fun
    The database schema is too old to perform this operation in this SharePoint cluster. Please upgrade the database and...
    Hello World!
    使用filter筛选刚体碰撞
  • 原文地址:https://www.cnblogs.com/clqbolg/p/10947341.html
Copyright © 2011-2022 走看看