首页 > 其他 > 详细

io-----字节流

时间:2021-05-24 09:26:59      阅读:23      评论:0      收藏:0      [点我收藏+]

一,字节流写数据

      1.步骤:

  1. 创建字符输出流对象
  2. 写数据
  3. 释放资源                     

                     注意事项:每次使用完必须收放资源,如果文件不存在就创建,但要保证父路径存在,如果文件年存在就清空,写int类型整数实际写出的是对应码表上的字母,写字符串数据,是字符串本身原样输出。

方法名 说明
void write(int b) 一次写一个字节数据
void write(byte [] b) 一次写一个字节数组数据
void write(byte [] b,int off,int len) 一次写一个字节数组的部分数据

 

package com.guancun.fileoutputstream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutputStreamDemo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(new File("a.txt"),true);
                 method2(fileOutputStream);
    }

    private static void method2(FileOutputStream fileOutputStream) throws IOException {
        byte[] bytes = "\r\n".getBytes();
        byte [] arr ={97,98,99,78};//off:为开始索引,len:为要写多长
        fileOutputStream.write(arr,1,2);
        fileOutputStream.write(bytes);
        fileOutputStream.write(arr,1,2);
        fileOutputStream.close();
    }

    private static void method1(FileOutputStream fileOutputStream) throws IOException {
        byte [] arr ={90,99,45,78};
        fileOutputStream.write(arr);
        fileOutputStream.close();
    }
}

 

2.字节流写数据的两个问题:

  1.   字节流写数据后加换行符 :windows:\r\n l                 inux:\n              mac:\r
  2. 自己写数据如何实现追加写入:FileOutputStream(String name,boolean oppend)            说明:创建文件输出流,以指定的名称写入文件,如果第二个参数为true,不会清空文件
package com.guancun.fileoutputstream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class InputStreamDemo2 {
    //数组copy提高效率
    public static void main(String[] args) throws IOException {
             //创建读取数据流
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\eg\\Day.class");
           //创建写数据的流
        FileOutputStream fileOutputStream = new FileOutputStream("Day.class");
        int b;
        while ((b=fileInputStream.read())!=-1){
            fileOutputStream.write(b);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    private static void method() throws IOException{
        FileInputStream fileInputStream = new FileInputStream("a.txt");//如果文件存在,那么不会报错
        int b ;
        while ((b=fileInputStream.read())!=-1){
            System.out.println(b);
        }
        //如果我们想要看到的是字符数据,那么一定要强转成char
        fileInputStream.close();//释放资源
    }
}

         练习:

package com.guancun.fileoutputstream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PracticeTest {
    public static void main(String[] args) throws IOException {

        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\Io.png");
        FileOutputStream fileOutputStream = new FileOutputStream("Io.png");
        byte[] bytes = new byte[1024];
        int read = fileInputStream.read(bytes);
        System.out.println(read);
        int len;//本次读的有效字节
        while ((len=fileInputStream.read(bytes))!=-1){
                  fileOutputStream.write(bytes,0,len);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}

 

 

二, 字节缓冲流

  1. bufferedOutputStream 字节缓冲输出流
  2. bufferedInputStream 字节缓冲输入流

    构造方法:

           .字节缓冲输出流:bufferedoutputStream(outPutStream out)

           .字节缓输入冲流:bufferedInputStream(inputStream in)

    为什么构造方法需要的是字节流,而不是具体的文件路径呢?

           .字节缓冲流仅仅提供缓冲区,而真证的读写数据还得依靠基本的字节流对象进行操作。

 

 

        字节缓冲流提高效率图解

技术分享图片

 

 

 

 

     

package com.guancun.fileoutputstream.BuffterStream;

import java.io.*;

public class OutputDemon1 {
    //利用缓冲流去copy文件
    public static void main(String[] args) throws IOException {
        //创建字节缓冲输入流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\an_an_wang-waagit-master.rar"));
        //创建字节缓冲输出流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("an_an_wang-waagit-master.rar"));
        int b;
        while ((b=bufferedInputStream.read())!=-1){
                   bufferedOutputStream.write(b);
        }
         bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}

 

 

 

 

  字节缓冲流结合数组提高效率图解

技术分享图片

 

 

 

 

  

package com.guancun.fileoutputstream.BuffterStream;

import java.io.*;

public class OutputDemon2 {
    //利用缓冲流去copy文件
    public static void main(String[] args) throws IOException {
        //创建字节缓冲输入流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\an_an_wang-waagit-master.rar"));
        //创建字节缓冲输出流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("an_an_wang-waagit-master.rar"));
        byte [] bytes = new byte[1024];
        int len;
        while ((len=bufferedInputStream.read(bytes))!=-1){
                   bufferedOutputStream.write(bytes,0,len);
            System.out.println(new String(bytes, 0, len));
        }

        bufferedOutputStream.close();
        new String(bytes,0,len);
    }
}

 

 

 

 

    字节流小结:

               字节流操作(拷贝所有类型的文件)

               字节缓冲流:可以提高效率(不能直接操作文件,需要传递字节流)

 

 

    拷贝文件的四种方式:

                1.字节流一次读写一个字节    2.字节流一次读写一个数组   3.字节缓冲流一次操作一个字节(在内存中从 输入缓>>>>>>输出缓)  4..字节缓冲流一次操作一个数组(在内存中进行)

               

io-----字节流

原文:https://www.cnblogs.com/waacode/p/14802924.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!