IO [Byte Stream, High Efficiency Stream]

Posted by SQHell on Fri, 21 Jun 2019 01:48:09 +0200

IO flow:
I:input, input, read to memory
O:Output, output, write to file
Stream: Data Stream (Characters, Bytes)
Classification:
Flow to:
Input: FileInput Stream, FileReader
Output: FileOutput Stream, FileWriter
Category:
Characters, bytes

java.io.OutputStream: Byte output stream, which is the parent class of all byte output streams

Public membership methods:
Abstract void write (int b) writes a byte
void write(byte[] b) writes to an array of bytes
void write(byte[] b, int off, int len) writes to the byte array, off is the initial index, len writes several
void close() closes the output stream and releases all system resources associated with the stream.

java.io.FileOutputStream: File byte output stream
Function: Write data in memory into files in bytes

Construction method:
FileOutputStream(File file) creates a file output stream that writes data to a file represented by a specified File object.
FileOutputStream(String name) creates an output file stream that writes data to a file with a specified name.
The parameters of the construction method:
file:
name: The path of the file
Are all destinations for writing data


Characteristic:

If the file specified in the constructor or the file pointed to by the path of the file does not exist, the constructor creates a file.
If the additional write switch is not turned on and the file already exists, it will be overwritten


Use steps:
1. Create a byte output stream object, FileOutputStream, to bind the destination of the data
2. Write the data into the file using the write method in FileOutputStream
3. Releasing resources

When a stream writes data, it finds the JVM, which calls the local method of the system to complete the write, and uses the system-related resources (releasing memory) when the stream is finished.

void write(byte[] b) writes to an array of bytes
void write(byte[] b, int off, int len) writes to the byte array, off is the initial index, len writes several

 1 public static void main(String[] args) throws IOException {
 2         File file = new File("b.txt");
 3         FileOutputStream fos = new FileOutputStream(file);
 4         //Write 100 to a file,100 It's three bytes.
 5         fos.write(49);
 6         fos.write(48);
 7         fos.write(48);
 8         
 9         /*
10          * void write(byte[] b) Write byte array
11          * When writing data, if you write more than one byte at a time
12          * Written bytes are positive: ASC | table is queried
13          * Write the byte is, the first byte is negative, the second byte can be positive or negative, query will be two bytes into a Chinese, query GBK encoding table.
14          */
15         byte[] bytes = {65,66,67,68,69};//ABCDE
16         //byte[] bytes = {-65,-66,-67,68,88};//Roast rice
17         
18         fos.write(bytes);
19         
20         //void write(byte[] b, int off, int len) Write byte array,off Is the starting index,len Write a few
21         fos.write(bytes, 1, 2);
22         
23         /*
24          * Fast Writing Method of Byte Array
25          * String There is a method in the class
26          * byte[] getBytes(String charsetName) :Converting strings to byte arrays 
27          */
28         byte[] bytes2 = "Hello".getBytes();
29         System.out.println(Arrays.toString(bytes2));//[-60, -29, -70, -61]
30         fos.write(bytes2);
31         
32         fos.close();
33     }

void write(byte[] b) writes to an array of bytes
When writing data, if you write more than one byte at a time
Written bytes are positive: ASC | table is queried
Write the byte is, the first byte is negative, the second byte can be positive or negative, query will be two bytes into a Chinese, query GBK encoding table.

Fast Writing Method of Byte Array
There is a method in the String class
byte[] getBytes(String charsetName): Converts strings to byte arrays

Continuation and line change of documents:
Line change:
windows:\r\n
linux:\n
mac:\r
Additional Writing: A Construction Method Using Two Parameters
FileOutputStream(File file, boolean append)
FileOutputStream(String name, boolean append)
Parameters:
File file,String name: destination for writing data
boolean append: Additional write switch, true: Additional write (previous file, continue to write content), fasle: No additional write (overwrite previous file)

java.io.InputStream: Byte input stream, which is the parent class of all byte input streams

Public membership methods:
int read(): Read a byte and return it. No byte returns - 1.
int read(byte []): Read a certain number of bytes and store them in the byte array, returning the number of bytes read.
void close() closes the file input stream and releases all system resources associated with the stream.

java.io.FileInputStream: File byte input stream
Function: Read data from files into memory in bytes

Construction method:
FileInputStream(String name)
FileInputStream(File file)
Parameter: Which file to read (data source)
String name: File path of string
File file: Read file

Use steps:
1. Create the byte input stream object FileInputStream and bind the data source
2. Read the file using the method read in the FileInputStream object
3. Releasing resources


int read(byte []): Read a certain number of bytes and store them in the byte array, returning the number of bytes read.
To make clear:
1. The role of byte arrays: play a buffer role, can buffer multiple bytes to the array at a time, can improve the efficiency of reading.
Length of byte arrays: Generally defined as 1024 (one kb byte) or an integer multiple of 1024
2. What is the return value int: the number of valid bytes read each time

File Copy: Read the file one byte at a time, write the file one byte at a time.

Data source: c:\1.jpg
Data destination: d:\1.jpg

Operation steps:
1. Create the byte input stream object FileInputStream and bind the data source
2. Create the byte output stream object FileOutputStream and bind the data destination
3. Read one byte at a time using the method read in FileInputStream
4. write one byte at a time using the method in FileOutputStream
5. Release resources (first write, then read)

 1 public static void main(String[] args) throws IOException {
 2         long s = System.currentTimeMillis();
 3         //1.Create byte input stream objects FileInputStream,And bind data sources
 4         FileInputStream fis = new FileInputStream("c:\\1.jpg");
 5         //2.Create byte output stream objects FileOutputStream,And bind data destinations
 6         FileOutputStream fos = new FileOutputStream("d:\\1.jpg");
 7         //3.Use FileInputStream Method in read,Read one byte at a time
 8         int len = 0;//Receive read bytes
 9         while((len = fis.read())!=-1){
10             //4.Use FileOutputStream Method in write,Write one byte at a time
11             fos.write(len);
12         }
13         //5.Releasing resources(First Pass Writing,Hou Guan Reading)
14         fos.close();
15         fis.close();
16         long e = System.currentTimeMillis();
17         System.out.println(e-s);
18     }    

File Replication: Read files using byte array buffer read, write data to write multiple bytes at a time
*
* Data source: c:\1.jpg
* Data destination: d:\1.jpg
*
* Operation steps:
* 1. Create the byte input stream object FileInputStream and bind the data source
* 2. Create the byte output stream object FileOutputStream and bind the data destination
* 3. Read multiple bytes at a time using the method read(byte []) in FileInputStream
* 4. Write multiple bytes at a time using the method write(byte[],0,len) in FileOutputStream
* 5. Releasing resources

 1 public static void main(String[] args) throws Exception {
 2         long s = System.currentTimeMillis();
 3         //1.Create byte input stream objects FileInputStream,And bind data sources
 4         FileInputStream fis =  new FileInputStream("c:\\z.zip");
 5         //2.Create byte output stream objects FileOutputStream,And bind data destinations
 6         FileOutputStream fos = new FileOutputStream("d:\\z.zip");
 7         //3.Use FileInputStream Method in read(byte[]),Read multiple bytes at a time
 8         byte[] bytes = new byte[1024*100];
 9         int len = 0;//Number of bytes read valid
10         while((len = fis.read(bytes))!=-1){
11             //4.Use FileOutputStream Method in write(byte[],0,len),Write multiple bytes at a time
12             fos.write(bytes, 0, len);
13         }
14         //5.Releasing resources
15         fos.close();
16         fis.close();
17         
18         long e = new Date().getTime();
19         System.out.println(e-s);
20     }

File replication: read files using buffer stream + array, write data using buffer stream write multiple at a time
*
* Data source: c:\1.jpg
* Data destination: d:\1.jpg
*
* Operation steps:
* 1. Create FileInputStream objects and bind data sources
* 2. Create BufferedInputStream objects and pass FileInputStream in the construction method to improve the reading efficiency of FileInputStream.
* 3. Create FileOutputStream objects and bind data destinations
* 4. Create BufferedOutputStream object, pass FileOutputStream in construction method, improve FileOutputStream efficiency
* 5. Use the method read(byte []) in BufferedInputStream to read files
* 6. Write data to the buffer using the method write(byte[],0,len) in Buffered OutputStream
* 7. Use the method flush in Buffered OutputStream to refresh the buffer data to the file
* 8. Releasing resources

 1 public static void main(String[] args) throws Exception {
 2         long s = System.currentTimeMillis();
 3         //1.Establish FileInputStream object,Binding data sources
 4         FileInputStream fis = new FileInputStream("c:\\z.zip");
 5         //2.Establish BufferedInputStream object,Input in construction method FileInputStream
 6         BufferedInputStream bis = new BufferedInputStream(fis);
 7         //3.Establish FileOutputStream object,Binding data destination
 8         FileOutputStream fos = new FileOutputStream("d:\\z.zip");
 9         //4.Establish BufferedOutputStream object,Transfer in Construction Method FileOutputStream,increase FileOutputStream efficiency
10         BufferedOutputStream bos = new BufferedOutputStream(fos);
11         //5.Use BufferedInputStream Method in read(byte[]),read file
12         /*int len = 0;
13         while((len = bis.read())!=-1){
14             //6.Write data to the buffer using the method write(byte[],0,len) in Buffered OutputStream
15             bos.write(len);
16         }*/
17         byte[] bytes = new byte[1024*100];
18         int len = 0;
19         while((len = bis.read(bytes))!=-1){
20             fos.write(bytes, 0, len);
21             fos.flush();
22         }
23         
24         //8.Releasing resources
25         bos.close();
26         bis.close();
27         
28         long e = new Date().getTime();
29         System.out.println(e-s);
30     }

java.io.BufferedOutputStream: Byte buffer output stream extends OutputStream
* Byte Buffer Output Stream: Adding a Buffer to the Basic Stream to Improve the Efficiency of the Basic Stream
*
* Common member methods inherited from the parent class
* Abstract void write (int b) writes a byte
* void write(byte[] b) writes to an array of bytes
* void write(byte[] b, int off, int len) writes to the byte array, off is the initial index, len writes several
* void close() closes the output stream and releases all system resources associated with the stream.
*
* Construction method:
* Buffered Output Stream (Output Stream out) creates a new buffered output stream to write data to the specified underlying output stream.
* Parameters:
* OutputStream out: Byte output stream, using FileOutputStream
* Which byte output stream will be added to which byte output stream to improve the efficiency of the stream when parameters are passed
* Use steps:
* 1. Create FileOutputStream objects and bind data destinations
* 2. Create BufferedOutputStream object, pass FileOutputStream in construction method, improve FileOutputStream efficiency
* 3. Write the data into the buffer using the method write in BufferedOutputStream
* 4. Use the method flush in Buffered OutputStream to refresh the data in the buffer to the file
* 5. Releasing resources

 1  public static void main(String[] args) throws IOException {
 2         //1.Establish FileOutputStream object,Binding data destination
 3         FileOutputStream fos = new FileOutputStream("buffered.txt");
 4         //2.Establish BufferedOutputStream object,Transfer in Construction Method FileOutputStream
 5         BufferedOutputStream bos = new BufferedOutputStream(fos);
 6         //3.Use BufferedOutputStream Method in write,Write the data into the buffer
 7         bos.write(97);
 8         
 9         bos.write("I am a buffer stream".getBytes());
10         //4.Use BufferedOutputStream Method in flush,Put the data in the buffer,Refresh to file
11         bos.flush();
12         //5.Releasing resources
13         bos.close();
14     }

java.io.BufferedInputStream: Byte buffer input stream extends InputStream
* Function: Adding a buffer to the basic byte input stream to improve the efficiency of the basic byte input stream
*
* The public member method inherited from the parent class:
* int read(): Read a byte and return it. No byte returns - 1.
* int read(byte []): Read a certain number of bytes and store them in the byte array, returning the number of bytes read.
* void close() closes the file input stream and releases all system resources associated with the stream.
*
* Construction method:
* Buffered Input Stream (Input Stream) creates a Buffered Input Stream and saves its parameters, i.e. input stream in, for future use.
* Parameters:
* InputStream in: Byte input stream that passes FileInputStream
* Which byte input stream object is passed by the parameter will add a buffer to which byte input stream object to improve the efficiency of the stream.
*
* Use steps:
* 1. Create FileInputStream objects and bind data sources
* 2. Create BufferedInputStream objects and pass FileInputStream in the construction method to improve the reading efficiency of FileInputStream.
* 3. Read files using the method read in BufferedInputStream
* 4. Releasing resources
*
* Summary:
* Byte stream: Operating files are non-text files, and files are copied
* Character Stream: All the files operated on are text files. One character at a time can be read in Chinese.
* Text File: Use Notepad to open and understand

 1 public static void main(String[] args) throws IOException {
 2         //1.Establish FileInputStream object,Binding data sources
 3         FileInputStream fis = new FileInputStream("buffered.txt");
 4         //2.Establish BufferedInputStream object,Input in construction method FileInputStream
 5         BufferedInputStream bis = new BufferedInputStream(fis);
 6         //3.Use BufferedInputStream Method in read,read file
 7         //int read():Read a byte and return, no byte return-1.
 8         /*int len = 0;
 9         while((len = bis.read())!=-1){
10             System.out.println((char)len);
11         }*/
12         
13         //int read(byte[]): Read a certain number of bytes, and store them in the byte array, return the number of bytes read.
14         byte[] bytes = new byte[1024];
15         int len = 0;
16         while((len = bis.read(bytes))!=-1){
17             System.out.println(new String(bytes,0,len));            
18         }
19         
20         //4.Releasing resources
21         bis.close();
22         System.out.println("-------------------");
23         BufferedReader br = new BufferedReader(new FileReader("buffered.txt"));
24         while((len = br.read())!=-1){
25             System.out.println((char)len);
26         }
27     }

Copy a single folder
*
* Data source: c: demo
* Data destination: d::\
*
* Operational procedures:
* 1. Determine whether the d-disk has demo folder or not, and create it if it does not.
* 2. Method of creating a copy file
* Return value type: void
* Method name: copyFile
* List of parameters: File src,File dest
* 3. Traverse the folders to be copied and get the path of each file in the folder (the data source of the file to be copied)
* 4. Use the method getName in the file class to stitch together the data purpose of the file to be copied
* 5. Call the copyFile replication method for replication

 1 public static void main(String[] args) throws IOException {
 2         //1.judge d Do you have any discs? demo Folder,If not, create
 3         File file = new File("d:\\demo");
 4         if(!file.exists()){
 5             file.mkdirs();
 6         }
 7         //3.Traverse the folders to be copied,Get the path of each file in the folder(Data source to copy files)
 8         File srcDirectory = new File("c:\\demo");
 9         File[] srcFiles = srcDirectory.listFiles();
10         for (File srcFile : srcFiles) {
11             //4.Use file Methods in classes getName Data purposes of splicing files to be copied
12             String srcName = srcFile.getName();
13             //Use File The third constructor of the class creates the destination
14             File destFile = new File(file, srcName);
15             //5.call copyFile Method of replication for replication
16             copyFile(srcFile, destFile);
17         }
18     }

Topics: Java encoding jvm Windows