[Java_19] Byte Stream, Character Stream

I. IO

1. Classification

1 By flow direction
    Input Flow, Output Flow
 (2) By type
    Byte Stream, Character Stream

2. Byte Stream

1. Everything is bytes

* All data is stored in a computer as binary numbers, one byte at a time, as it is when transmitted.
*Byte streams can transfer any file data.When working with streams, it is always clear that no matter what stream object is used, the underlying transport is always binary data.

2. OutputStream abstract class

① void close() 
    Close this output stream and release any system resources associated with it.  
② void flush() 
    Refresh this output stream and force any buffered output bytes to be written out.  
③ void write(byte[] b)
    Writes b.length bytes from the specified byte array to this output stream.  
④ void write(byte[] b, int off, int len) 
    Writes len bytes from the specified byte array, starting with offset off, to this output stream.  
⑤ void write(int b) 
    Output stream of bytes specified.

3.FileOutputStream

1. Construction methods
    * public FileOutputStream(File file)
        Creates a file output stream to write to the file represented by the specified File object. 
    * public FileOutputStream(String name)
        Creates a file output stream to write to the file with the specified name.  
    * public FileOutputStream(File file, boolean append)
        Creates a file output stream to write to the file represented by the specified File object.  
    * public FileOutputStream(String name, boolean append)
        Creates a file output stream to write to the file with the specified name. 
    Example:
        import java.io.File;
        import java.io.FileOutputStream;
        import java.io.IOException;

        public class OutputStream {
            public static void main(String[] args) throws IOException {
                //Append Write
                FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Demo_Null\\Desktop\\file.java"), true);;
                //Overwrite
                FileOutputStream fos1 = new FileOutputStream("C:\\Users\\Demo_Null\\Desktop\\HelloWorld.java");;
 
                //Convert string to bytes
                fos.write("97".getBytes());
                //Output ASCII No. 65
                fos.write(65);
                //Convert string to bytes
                fos1.write("abc|".getBytes());
                //Output ASCII No. 97
                fos1.write(97);
                
                //Refresh
                fos.flush();
                fos1.flush();
                
                //Release resources Release resources first refresh when releasing resources flush() can not write
                fos.close();
                fos1.close();
            }
        }

4. InputStream abstract class

① void close()
    Close this input stream and release any system resources associated with it.    
② int read()
    Reads the next byte of data from the input stream. 
③ int read(byte[] b)
    Reads the number of bytes from the input stream and stores them in the byte array b.

5. FileInputStream

① Construction method
    * FileInputStream(File file)
        //Create a FileInputStream by opening a connection to the actual file, named after the File object file in the file system. 
    * FileInputStream(String name)
        //Create a FileInputStream by opening a connection to the actual file named by the path name in the file system.  
② Example
    public class DemoInputStream {
        public static void main(String[] args) throws IOException{
            // Create Stream Object with File Name
            FileInputStream fis = new FileInputStream("read.txt");
            // Read the data and return a byte
            int read = fis.read();
            System.out.println((char) read);
            read = fis.read();
            System.out.println((char) read);
            read = fis.read();
            System.out.println((char) read);
            read = fis.read();
            System.out.println((char) read);
            read = fis.read();
            System.out.println((char) read);
            // Read to end, return -1
            read = fis.read();
            System.out.println( read);
            // close resource
            fis.close();
        }
    }

6. File Copy

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

public class InputStream {
    public static void main(String[] args) throws IOException {
        //Create Input Stream
        FileInputStream fis = new FileInputStream("C:\\Users\\Demo_Null\\Desktop\\Desktop.rar");
        //Create Output Stream
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Demo_Null\\Desktop\\copy.rar");
 
        //Read in a byte (read->assign->judge)
        /*int content;
        while ((content = fis.read()) != -1) {
            //Write out a byte
            fos.write(content);
        }*/
 
        //Define a 1M buffer byte array 
        byte[] bytes = new byte[1024 * 1024];
        //Number of bytes read
        int len;
 
        //Copy file start time
        Date dateStart = new Date();
        long t1 = dateStart.getTime();
        //Read to the end
        while ((len = fis.read(bytes)) != -1) {
            //Write out len byte arrays starting at 0
            fos.write(bytes,0,len);
        }
 
        //End time of copying files
        Date dateEnd = new Date();
        long t2 = dateEnd.getTime();
 
        //How many milliseconds does it take to copy a file
        System.out.println(t2 - t1);
 
        //Release Resources
        fos.close();
        fis.close();
    }
}

3. Character Flow

1. Reader abstract class

① void close()
    Close the stream and release any system resources associated with it.    
② int read()
    Reads a character from the input stream. 
③ int read(char[] cbuf)
    Reads some characters from the input stream and stores them in the character array cbuf.

2. FileReader

1. Construction methods    
    * FileReader(File file)
        Create a new FileReader, given the File object to read.   
    * FileReader(String fileName)
        Create a new FileReader, given the name of the file to read.

3. Writer

① void write(int c)
    Writes a single character.
② void write(char[] cbuf)
    Writes an array of characters. 
③ void write(char[] cbuf, int off, int len)
    Writes a part of the character array, the start index of the off array, and the number of characters len writes. 
④ void write(String str)
    Writes a string. 
⑤ void write(String str, int off, int len) 
    Writes a part of the string, the start index of the off string, and the number of characters len writes.
⑥ void flush()
    Refresh the buffer for the stream.  
⑦ void close() 
    Close the stream, but refresh it first. 

4. FileWriter

1. Construction methods
    * FileWriter(File file)
        Create a new FileWriter, given the File object to read.   
    * FileWriter(String fileName)
        Create a new FileWriter, given the name of the file to read.
        
# GBK Chinese is 2 bytes and UTF-8 Chinese is 3 bytes so different coded text cannot be manipulated directly

5. Examples

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Reader {
    public static void main(String[] args) throws IOException {
        //Create character output stream
        FileWriter fw = new FileWriter("C:\\Users\\Demo_Null\\Desktop\\chinese.txt");
        //Enter string to text
        fw.write("Work hard?\r\n\t↓\r\n Appreciation raise¥\r\n\t↓\r\n To the Peak of Life<( ̄︶ ̄)↗[GO!]");
        //refresh data
        fw.flush();
        //Release Resources
        fw.close();
 
        //Create character input stream
        FileReader fr = new FileReader("C:\\Users\\Demo_Null\\Desktop\\chinese.txt");
 
        //Create Character Buffer
        char[] buffer = new char[1024 * 1024];
        //Number of characters read successfully
        int len;
        //Determine if the end tag is read
        while ((len = fr.read(buffer)) != -1) {
            //Print
            System.out.print(new String(buffer,0,len));
        }
        //Release Resources
        fr.close();
    }
}

Tags: Programming Java ascii

Posted on Thu, 13 Feb 2020 14:05:22 -0800 by 3s2ng