Java basic byte stream

The byte stream is divided into a byte input stream FileInputStream and a byte output stream FileOutputStream, where input and output are relative to memory; input means reading data from a file and inputting it into memory; output means outputting data from memory to a file .

Please add a picture description


1. Byte input stream

1.1 byte input stream reads a single byte

public static void main(String[] args) throws IOException {
    
    
        //创建字节输入流对象,并指明从a.txt读取数据(注意:此路径必须正确且文件必须存在)
        FileInputStream fileInputStream=new FileInputStream("D:\\Code\\ideaCode\\FileTest\\aa\\a.txt");

        //读取一个字节(读出的数据为ASCII码值)
        int read = fileInputStream.read();

        //将数据转换为char类型
        char data=(char)read;

        //打印输出
        System.out.println("使用字节输入流读取了一个数据:"+data);
        
        //关闭资源
        fileInputStream.close();
    }

a.txt file content:

insert image description here


operation result:
insert image description here


1.2 Byte input stream reads data byte by byte

public static void main(String[] args) throws IOException {
    
    
        //创建字节输入流对象,并指明从a.txt读取数据(注意:此路径必须正确且文件必须存在)
        FileInputStream fileInputStream=new FileInputStream("D:\\Code\\ideaCode\\FileTest\\aa\\a.txt");

        //设置有效字节,初始值为0
        int read=0;
        //一个字节一个字节读取数据,当读取至文件数据末尾空值时,fileInputStream.read()返回值为-1
        while((read=fileInputStream.read())!=-1){
    
    
            //打印输出
            System.out.print((char)read);
        }

        //关闭资源
        fileInputStream.close();
    }

a.txt file content:

insert image description here


operation result:
insert image description here


1.3 byte input stream a byte array a byte array to read data

public static void main(String[] args) throws IOException {
    
    
        //创建字节输入流对象,并指明从a.txt读取数据(注意:此路径必须正确且文件必须存在)
        FileInputStream fileInputStream=new FileInputStream("D:\\Code\\ideaCode\\FileTest\\aa\\a.txt");

        //定义一个字节数组,设置其初始长度为128
        byte[] bytes=new byte[128];
        //定义有效字节长度,初始值为0
        int length=0;
        //一个字节数组一个字节数组读取数据
        while((length=fileInputStream.read(bytes))!=-1){
    
    
            //将字节数组打印输出,输出其有效数据
            System.out.println(new String(bytes,0,length));
        }

        //关闭资源
        fileInputStream.close();
    }

a.txt file content:

insert image description here


operation result:
insert image description here


Analysis of running results

我们在a.txt中写入了522个字符,而我们通过一个长度为128的字节数组方式进行读取数据,128\*4=512<522,因此我们将会分5次打印输出,当读满一个字节数组,我们就会打印此字节数组,若读至文本数据末尾,我们也会打印字节数组,并结束循环。

2. The problem of reading data garbled characters from the byte input stream

When we write Chinese in a text file, and then read the data to the console through the byte input stream, garbled characters will appear.

a.txt file content: (A total of 12 Chinese characters
insert image description here


public static void main(String[] args) throws IOException {
    
    
        //创建字节输入流对象,并指明从a.txt读取数据(注意:此路径必须正确且文件必须存在)
        FileInputStream fileInputStream=new FileInputStream("D:\\Code\\ideaCode\\FileTest\\aa\\a.txt");
        //设置有效字节,初始值为0
        int read=0;
        //一个字节一个字节读取数据,当读取至文件数据末尾空值时,fileInputStream.read()返回值为-1
        while((read=fileInputStream.read())!=-1){
    
    
            //打印输出
            System.out.print((char)read);
        }
        //关闭资源
        fileInputStream.close();
    }

operation result:(Garbled characters appear
insert image description here


insert image description here


reason:

Since the encoding format of our notepad is UTF-8, and in UTF-8, a Chinese character occupies 3 bytes, when we read data through the byte input stream and print it to the console, due to the byte input The stream cannot specify the encoding format. The console will use ASCII encoding by default, and read data byte by byte, resulting in a Chinese character being split into 3 characters and read out, resulting in garbled characters.

Illustration:
insert image description here


3. Byte output stream

method name illustrate
void write(int b) Write one byte of data to the specified text at a time
void write(byte[] b) Write a byte array data to the specified text at a time
void write(byte[] b, int off, int len) Write part of the data of a byte array to the specified text at a time

3.1 Write one byte of data to the specified text at a time

public static void main(String[] args) throws IOException {
    
    
        //创建字节输出流对象,并指明数据要写入的文件(此文件可以不存在,代码运行时可自行创建)
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\Code\\ideaCode\\FileTest\\aa\\b.txt");

        //一次向指定文本写入一个字节数据(97=>a)
        fileOutputStream.write(97);
        
        //关闭资源
        fileOutputStream.close();
    }

operation result:
insert image description here


b.txt file content:
insert image description here


3.2 Write a byte array data to the specified text at a time

public static void main(String[] args) throws IOException {
    
    
        //创建字节输出流对象,并指明数据要写入的文件(此文件可以不存在,代码运行时可自行创建)
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\Code\\ideaCode\\FileTest\\aa\\b.txt");

        //一次向指定文本写入一个字节数组数据(b,c,d,e,f,g,h)
        byte[] bytes={
    
    98,99,100,101,102,103,104};
        fileOutputStream.write(bytes);

        //关闭资源
        fileOutputStream.close();
    }

b.txt file content: (We find that our newly written data overwrites the previously written data
insert image description here


3.3 Write part of the data of a byte array to the specified text at a time

public static void main(String[] args) throws IOException {
    
    
        //创建字节输出流对象,并指明数据要写入的文件(此文件可以不存在,代码运行时可自行创建)
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\Code\\ideaCode\\FileTest\\aa\\b.txt");

        //一次向指定文本写入写一个字节数组的部分数据(b,c,d,e,f,g,h,i)
        byte[] bytes={
    
    98,99,100,101,102,103,104,105};
        //写入efgh数据,e的索引为3,efgh长度为4
        fileOutputStream.write(bytes,3,4);

        //关闭资源
        fileOutputStream.close();
    }

b.txt file content: (We find that our newly written data overwrites the previously written data
insert image description here


3.4 Appending data without overwriting

If we don't want to overwrite the previous data every time we write data, the byte output stream also provides us with a way to append data.

b.txt file content:
insert image description here


public static void main(String[] args) throws IOException {
    
    
        //创建字节输出流对象,并指明数据要写入的文件(此文件可以不存在,代码运行时可自行创建),并设置其为追加方式写入数据
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\Code\\ideaCode\\FileTest\\aa\\b.txt",true);

        //一次向指定文本写入一个字节数组数据(a,a,a,a,a,a,a,a)
        byte[] bytes={
    
    97,97,97,97,97,97,97,97};
        fileOutputStream.write(bytes);

        //关闭资源
        fileOutputStream.close();
    }

b.txt file content: (We see that the newly written data is appended to the previous data without overwriting the previous data
insert image description here


4. Copy data through byte stream

Copy the data of a text file to another text of the same type through the byte stream, and the data will not be garbled, because the same type of file will copy the data encoding to the new text together when copying.注意:我们不能将两个不同文件类型的数据进行拷贝,例如将txt数据拷贝至docx中。

4.1 Copy the txt file data in utf-8 encoding format to the txt file in ascii encoding format

a.txt text in utf-8 encoding format and b.txt text in ascii encoding format
Please add a picture description


public static void main(String[] args) throws IOException {
    
    
        //创建一个字节输入流,并指明数据要从a.txt文件中读取(注意:此路径必须正确且文件必须存在)
        FileInputStream fileInputStream=new FileInputStream("D:\\Code\\ideaCode\\FileTest\\aa\\a.txt");
        //创建字节输出流对象,并指明数据要写入b.txt(此文件可以不存在,代码运行时可自行创建)
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\Code\\ideaCode\\FileTest\\aa\\b.txt");

        //拷贝数据
        //定义一个字节数组,设置其初始长度为1024
        byte[] bytes=new byte[1024];
        //定义有效字节长度,初始值为0
        int len=0;
        //一个字节数组一个字节数组读取数据
        while((len=fileInputStream.read(bytes))!=-1){
    
    
            //向b.txt写入数据
            fileOutputStream.write(bytes,0,len);
        }

        //关闭资源
        fileInputStream.close();
        fileOutputStream.close();
    }

b.txt after the code runs: (No garbled characters, and the encoding format of b.txt is changed to UTF-8
insert image description here


4.2 Copy the txt file data in ascii encoding format to the txt file in UTF-8 encoding format

txt text in ascii encoding format and txt text in UTF-8 encoding format

Please add a picture description


public static void main(String[] args) throws IOException {
    
    
        //创建一个字节输入流,并指明数据要从a.txt文件中读取(注意:此路径必须正确且文件必须存在)
        FileInputStream fileInputStream=new FileInputStream("D:\\Code\\ideaCode\\FileTest\\aa\\a.txt");
        //创建字节输出流对象,并指明数据要写入b.txt(此文件可以不存在,代码运行时可自行创建)
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\Code\\ideaCode\\FileTest\\aa\\b.txt");

        //拷贝数据
        //定义一个字节数组,设置其初始长度为1024
        byte[] bytes=new byte[1024];
        //定义有效字节长度,初始值为0
        int len=0;
        //一个字节数组一个字节数组读取数据
        while((len=fileInputStream.read(bytes))!=-1){
    
    
            //向b.txt写入数据
            fileOutputStream.write(bytes,0,len);
        }

        //关闭资源
        fileInputStream.close();
        fileOutputStream.close();
    }

b.txt after the code runs: (No garbled characters, and the encoding format of b.txt is changed to ascii
insert image description here


4.3 Copy the txt file data to the docx file

Among them, we set a.txt as UTF-8 encoding, and 11.docx as UTF-8 encoding.

public static void main(String[] args) throws IOException {
    
    
        //创建一个字节输入流,并指明数据要从a.txt文件中读取(注意:此路径必须正确且文件必须存在)
        FileInputStream fileInputStream=new FileInputStream("D:\\Code\\ideaCode\\FileTest\\aa\\a.txt");
        //创建字节输出流对象,并指明数据要写入11.docx(此文件可以不存在,代码运行时可自行创建)
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\Code\\ideaCode\\FileTest\\aa\\11.docx");

        //拷贝数据
        //定义一个字节数组,设置其初始长度为1024
        byte[] bytes=new byte[1024];
        //定义有效字节长度,初始值为0
        int len=0;
        //一个字节数组一个字节数组读取数据
        while((len=fileInputStream.read(bytes))!=-1){
    
    
            //向b.txt写入数据
            fileOutputStream.write(bytes,0,len);
        }

        //关闭资源
        fileInputStream.close();
        fileOutputStream.close();
    }

11.docx after the code runs: (Something went wrong and data cannot be displayed
insert image description here


insert image description here


insert image description here


OK! The byte stream introduction is over! ! !

Guess you like

Origin blog.csdn.net/qq_45344586/article/details/130309473