zoukankan      html  css  js  c++  java
  • IO流(字节流)

    一、File类的构造方法:

    File(String pathname)     指定文件或者文件夹的路径创建一个File文件。
    File(File parent, String child)   根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。

    File(String parent, String child)

    注意:在windows机器上 的目录分隔符是  ,在linux机器上的目录分隔符是/ 。

    .   当前路径
    .. 上一级路径

    字节流: 字节流读取得都是文件中二进制数据,读取到二进制数据不会经过任何的处理。

    字符流: 字符流读取的数据是以字符为单位的 。 字符流也是读取文件中的二进制数据,不过会把这些二进制数据转换成我们能 识别的字符。

    字符流 = 字节流 + 解码

    二、字节流

    (一)输入字节流:
    --------| InputStream 所有输入字节流的基类 抽象类
    ---------------| FileInputStream 读取文件数据的输入字节流

    使用FileInputStream读取文件数据的步骤:
    1. 找到目标文件
    2. 建立数据的输入通道。
    3. 读取文件中的数据。
    4. 关闭 资源.

    public class Demo1 {
        
        public static void main(String[] args) throws IOException {
            readTest4();
        }
        
        //方式4:使用缓冲数组配合循环一起读取。28
        public static void readTest4() throws IOException{
            long startTime = System.currentTimeMillis();
            //找到目标文件
            File file = new File("F:\美女\1.jpg");
            //建立数据的输入通道
            FileInputStream fileInputStream = new FileInputStream(file);
            //建立缓冲数组配合循环读取文件的数据。
            int length = 0; //保存每次读取到的字节个数。
            byte[] buf = new byte[1024]; //存储读取到的数据    缓冲数组 的长度一般是1024的倍数,因为与计算机的处理单位。  理论上缓冲数组越大,效率越高
            
            while((length = fileInputStream.read(buf))!=-1){ // read方法如果读取到了文件的末尾,那么会返回-1表示。
                System.out.print(new String(buf,0,length));
            }
            
            //关闭资源
            fileInputStream.close();
            
    
            long endTime = System.currentTimeMillis();
            System.out.println("读取的时间是:"+ (endTime-startTime)); //446
        }
        
        
        //方式3:使用缓冲 数组 读取。    缺点: 无法读取完整一个文件的数据。     12G
        public static void readTest3() throws IOException{
            //找到目标文件
            File file = new File("F:\a.txt");
            //建立数据的输入通道
            FileInputStream fileInputStream = new FileInputStream(file);
            //建立缓冲字节数组,读取文件的数据。
            byte[] buf = new byte[1024];  //相当于超市里面的购物车。
            int length = fileInputStream.read(buf); // 如果使用read读取数据传入字节数组,那么数据是存储到字节数组中的,而这时候read方法的返回值是表示的是本次读取了几个字节数据到字节数组中。
            System.out.println("length:"+ length);
            
            //使用字节数组构建字符串
            String content = new String(buf,0,length);
            System.out.println("内容:"+ content);
            //关闭资源
            fileInputStream.close();
        }
        
        
        
        
        
        //方式2 : 使用循环读取文件的数据
        public static void readTest2() throws IOException{
            long startTime = System.currentTimeMillis();
            //找到目标文件
            File file = new File("F:\美女\1.jpg");
            //建立数据的输入通道
            FileInputStream fileInputStream = new FileInputStream(file);
            //读取文件的数据
            int content = 0; //声明该变量用于存储读取到的数据
            while((content = fileInputStream.read())!=-1){
                System.out.print((char)content);
            }
            //关闭资源
            fileInputStream.close();
            
            long endTime = System.currentTimeMillis();
            System.out.println("读取的时间是:"+ (endTime-startTime)); //446
        }
        
        
        
        //方式一缺陷: 无法读取完整一个文件的数据.
        public static void readTest1() throws IOException{
            //1. 找到目标文件
            File file = new File("F:\a.txt");
            //建立数据的输入通道。
            FileInputStream fileInputStream = new FileInputStream(file);
            //读取文件中的数据
            int content = fileInputStream.read(); // read() 读取一个字节的数据,把读取的数据返回。
            System.out.println("读到的内容是:"+ (char)content);
            //关闭资源    实际上就是释放资源。 
             fileInputStream.close();
        }
        
        
    }

    (二)输出字节流:

    --------| OutputStream 所有输出字节流的基类 抽象类
    ---------------| FileOutStream 向文件输出数据的输出字节流。

    FileOutputStream如何使用呢?
    1. 找到目标文件
    2. 建立数据的输出通道。
    3. 把数据转换成字节数组写出。
    4. 关闭资源FileOutputStream要注意的细节:

    1. 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。
    2. 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
    3.使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
    4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是把低八位的二进制数据写出,其他二十四位数据全部丢弃。

    00000000-000000000-00000001-11111111           511
    11111111---> -1

    public class Demo1 {
        
        public static void main(String[] args) throws IOException {
            writeTest3();
        }
        
        
        //使用字节数组把数据写出。
        public static void writeTest3() throws IOException{
            //找到目标文件
            File file = new File("F:\b.txt");
            //建立数据输出通道
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            //把数据写出。
            String data = "abc";
            byte[] buf = data.getBytes();

    fileOutputStream.write(buf,
    0, 3); // 0 从字节数组的指定索引值开始写, 2:写出两个字节。 //关闭资源 fileOutputStream.close(); } //使用字节数组把数据写出。 public static void writeTest2() throws IOException{ //找到目标文件 File file = new File("F:\b.txt"); //建立数据输出通道 FileOutputStream fileOutputStream = new FileOutputStream(file,true); //把数据写出。 String data = " hello world"; fileOutputStream.write(data.getBytes()); //关闭资源 fileOutputStream.close(); } //每次只能写一个字节的数据出去。 public static void writeTest1() throws IOException{ //找到目标文件 File file = new File("F:\b.txt"); //建立数据的输出通道 FileOutputStream fileOutputStream = new FileOutputStream(file); //把数据写出 fileOutputStream.write('h'); fileOutputStream.write('e'); fileOutputStream.write('l'); fileOutputStream.write('l'); fileOutputStream.write('o'); //关闭资源 fileOutputStream.close(); } }

    需求:copy一张图片

    public class CopyImage {
        
        
        public static void main(String[] args) throws IOException {
            //找到目标文件
            File inFile = new File("F:\美女\1.jpg");
            File destFile = new File("E:\1.jpg");
            //建立数据的输入输出通道
            FileInputStream fileInputStream = new  FileInputStream(inFile);
            FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....
            
            //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。
            //建立缓冲数据,边读边写
            byte[] buf = new byte[1024]; 
            int length = 0 ; 
            while((length = fileInputStream.read(buf))!=-1){ //最后一次只剩下了824个字节
                fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。
            }
            //关闭资源 原则: 先开后关,后开先关。
            fileOutputStream.close();
            fileInputStream.close();
        }
    
    }

    (三)字节缓冲流

    ----| InputStream 输入字节流的基类。 抽象
    ----------| FileInputStream 读取文件数据的输入字节流
    ----------| BufferedInputStream 缓冲输入字节流 缓冲输入字节流的出现主要是为了提高读取文件数据的效率。

    ----| OutputStream 输出字节流的基类。 抽象

    ----------| FileOutputStream 向文件 输出数据 的输出字节流
    ----------| Bufferedoutputstream 缓冲输出字节流 BufferedOutputStream出现的目的是为了提高写数据的效率。
    内部都是维护了一个8kb的字节数组而已

    注意: 凡是缓冲流都不具备读写文件的能力。

    使用BufferedInputStream的步骤 :
    1. 找到目标文件。
    2. 建立数据 的输入通道
    3. 建立缓冲 输入字节流流
    4. 关闭资源

    public class BufferIn {
    
        public static void main(String[] args) throws IOException {
            File file = new File("d:/test.txt");
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
            //读取文件数据
            int content = 0;
            while((content=bufferedInputStream.read())!=-1){
                System.out.print((char)content);
            }
                    bufferedInputStream.close();//调用BufferedInputStream的close方法实际上关闭的是FileinputStream.
    
        }
    
    }        

    使用BufferedOutputStream的步骤:
    1. 找到目标文件
    2. 建立数据的输出通道


    BufferedOutputStream 要注意的细节
    1. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中。
    2. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中,如果需要把数据真正的写到硬盘上面,需要
    调用flush方法或者是close方法、 或者是内部维护的字节数组已经填满数据的时候。

    public class Demo2 {
    
        public static void main(String[] args) throws IOException {
            //找到目标文件
            File file = new File("F:\a.txt");
            //建立数据的输出通道
            FileOutputStream  fileOutputStream = new FileOutputStream(file);
            //建立缓冲输出字节流对象
            BufferedOutputStream bufferedOutputStream  = new BufferedOutputStream(fileOutputStream);
            //把数据写出
            bufferedOutputStream.write("hello world".getBytes()); 
            //把缓冲数组中内部的数据写到硬盘上面。
            //bufferedOutputStream.flush();
            bufferedOutputStream.close();
        }
        
    }

     

  • 相关阅读:
    开源项目
    测试面试话题8:测试人员如何让开发少写bug?
    其他
    接口平台
    001接口概念
    python3PIL模块实现图片加文字/小图片水印
    python3实现url编码/解码
    python3实现读取Excel进行接口自动化测试
    常用正则表达式
    Python3实现简单的接口性能测试
  • 原文地址:https://www.cnblogs.com/stellar/p/5245961.html
Copyright © 2011-2022 走看看