Java IO Learning Summary

Reprinted from http://blog.csdn.net/du_minchao/article/details/49045421

    Java The streams can be classified from different perspectives.

According to the direction of flow: input stream and output stream.

According to the unit of processing data: byte stream and character stream.

It can be divided into node flow and processing flow according to the function of implementation.

Output stream:

 

Input stream:

Byte stream: A read-in or read-out is an 8-bit binary.

Character Stream: A single read-in or read-out is 16-bit binary.

Byte stream and character stream are the same in principle, but they are processed in different units. The suffix is Stream, the suffix is Reader, and Writer is a character stream.

Byte stream: A stream object that processes byte data. The data on the device, whether picture or dvd, text, are stored in binary. The binary system is ultimately represented by an 8-bit data unit, so the smallest data unit in a computer is the byte. This means that byte streams can handle all data on the device, so byte streams can handle character data as well.

 

Node flow: directly connected to the data source, read in or read out.

Direct use of node flow makes it inconvenient to read and write. In order to read and write files faster, processing flow is available.

Processing flow: It is used together with node flow. On the basis of node flow, another layer is socketed, and the process flow is socketed on the node flow.

The following are Java The structure diagram of IO flow:


Character stream:

Reader: An abstract class for reading character streams.

| BufferedReader: Read text from the character input stream, buffer each character, so as to achieve efficient reading of characters, arrays and rows. You can specify the size of the buffer, or you can use the default size. In most cases, the default value is large enough.

| LineNumberReader: A buffered character input stream that tracks line numbers. This class defines the method setLineNumber(int) And getLineNumbner() They can be used to set and get the current line number, respectively.

| InputStreamReader: A bridge between byte flow and character flow: It reads bytes and decodes them into characters using the specified charset. The character set it uses can be specified by name or explicitly given, or it can accept the platform default character set.

| FileReader: A convenient class for reading character files. This constructor assumes that the default character encoding and the default byte buffer size are appropriate. To specify these values yourself, you can first construct an InputStream Reader on FileInputStream.

***************************************************************************

Writer: An abstract class that writes character streams.

| Buffered Writer: Writes text to the character output stream, buffering each character, thus providing efficient writing of individual characters, arrays, and strings.

| Output Stream Writer: A bridge between character streams and byte streams: You can use the specified charset The characters to be written to the stream are encoded into bytes. The character set it uses can be specified by name or explicitly given, otherwise the platform default character set will be accepted.

| FileWriter: A convenient class for writing character files. This constructor assumes that the default character encoding and the default byte buffer size are acceptable. To specify these values yourself, you can first construct an OutputStream Writer on FileOutputStream.

****************************************************************************

Byte stream:

InputStream: A superclass representing all classes of byte input streams.

| FileInputStream: Get input bytes from a file in the file system. Which files are available depends on the host environment. FileInputStream is used to read raw byte streams such as image data. To read character streams, consider using FileReader.

| FilterInputStream: Contains other input streams that are used as its basic data source to transmit data directly or provide additional functionality.

| BufferedInputStream: This class implements buffered input streams.

**************************************************************************

OutputStream: This abstract class is a superclass representing all classes of the output byte stream.

| FileOutputStream: A file output stream is an output stream used to write data to a File or FileDescriptor.

| FilterOutputStream: This class is a superclass of all classes that filter the output stream.

| Buffered Output Stream: This class implements buffered output streams.


Rules of flow operation:

1. Identify the source and purpose.

Data Source: Just need to read, you can use two systems: InputStream, Reader;

Data sink: Just need to write, you can use two systems: OutputStream, Writer;

2. Is the operation data pure text data?

If yes: data source: Reader

Data sink: Writer

If not: Data Source: InputStream

Data sink: Output Stream

3. Although a system has been established, there are too many objects in the system, which one should be used in the end?

Data device with clear operation.

Data source corresponding devices: hard disk (File), memory (array), keyboard (System.in)

Data sink corresponding devices: hard disk (File), memory (array), console (System.out).

4. Do you need to add other functions to the basic operation? For example, buffer.

Decorate if necessary.

/ Remember, use this sentence as soon as you read the keyboard entry.

BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));



Here is an example of an application:

  1. <span style="color:#000000;">import java.io.;  
  2. import java.nio.CharBuffer;  
  3. import java.util.Scanner;  
  4.   
  5. import org.junit.Test;  
  6.   
  7. public class Test1 {  
  8.     @Test  
  9.     public void testFile() throws IOException {  
  10.   
  11.         File file1 = new File("D:/test");  
  12.         file1.mkdir(); //Create test directory under D disk  
  13.         File[] fileArr = file1.listFiles(); //Get all the files in the directory  
  14.         for (File f : fileArr) { //File names in the output directory  
  15.             System.out.println(f.getName());  
  16.         }  
  17.           
  18.         String str;  
  19.         System.out.println("Please enter the name of the file to be copied:");  
  20.         BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));  
  21.             str = buf.readLine();  
  22.         if(str.equals("1.txt")){  
  23.             new copyFile().copyTxt();  
  24.         }  
  25.         if(str.equals("1.jpg")){  
  26.             new copyFile().copyJpg();  
  27.         }  
  28.           
  29.         buf.close();  
  30.         File[] fileArr2 = file1.listFiles(); //Get all the files in the directory  
  31.         for (File f : fileArr2) { //File names in the output directory  
  32.             System.out.println(f.getName());  
  33.         }  
  34.           
  35.   
  36.     }  
  37.   
  38. }  
  39.   
  40. class copyFile {  
  41.     public void copyTxt() throws IOException {  
  42.         int number = 0;  
  43.         File file1 = new File("D:/test/1.txt");  
  44.         File file2 = new File("D:/test/2.txt");  
  45.         BufferedReader bfRead;  
  46.         BufferedWriter bfWrite;  
  47.         bfWrite = new BufferedWriter(new FileWriter(file2));  
  48.         char[] arry = new char[1024];  
  49.         FileReader fRead = new FileReader(file1);  
  50.         bfRead = new BufferedReader(fRead);  
  51.   
  52.         while ((number = bfRead.read(arry)) != -1) {  
  53.             bfWrite.write(arry, 0, number);  
  54.         }  
  55.   
  56.         bfWrite.close();  
  57.         bfRead.close();  
  58.   
  59.     }  
  60.   
  61.     public void copyJpg() throws IOException  {  
  62.               
  63.         File file1=  new File("D:/test/1.jpg");  
  64.         File file2= new File("D:/test/2.jpg");  
  65.         FileInputStream bfRead=new FileInputStream(file1);;  
  66.         FileOutputStream bfWrite=new FileOutputStream(file2);  
  67.         try {  
  68.             byte[] arry = new byte[1024]; //Define arrays to transfer data between streams  
  69.             int count = 0;  
  70.   
  71.             while((count = bfRead.read(arry)) != -1) {  
  72.                 bfWrite.write(arry, 0, count);  
  73.             }  
  74.   
  75.               
  76.         } catch (FileNotFoundException e) {  
  77.             System.out.println("filenotfound");  
  78.         } catch (IOException e) {  
  79.             System.out.println("IoException");  
  80.         }  
  81.         bfWrite.close();  
  82.         bfRead.close();  
  83.   
  84.     }  
  85. }  
  86. </span>  
<span style="color:#000000;">import java.io.; 
import java.nio.CharBuffer;
import java.util.Scanner;

import org.junit.Test;

public class Test1 {
@Test
public void testFile() throws IOException {

    File file1 = new File("D:/test");
    file1.mkdir(); // Create test directory under D disk
    File[] fileArr = file1.listFiles(); // Get all the files in the directory
    for (File f : fileArr) { // File name in output directory
        System.out.println(f.getName());
    }

    String str;
    System.out.println("Please enter the name of the file to be copied:");
    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
        str = buf.readLine();
    if(str.equals("1.txt")){
        new copyFile().copyTxt();
    }
    if(str.equals("1.jpg")){
        new copyFile().copyJpg();
    }

    buf.close();
    File[] fileArr2 = file1.listFiles(); // Get all the files in the directory
    for (File f : fileArr2) { // File name in output directory
        System.out.println(f.getName());
    }


}

}

class copyFile {
public void copyTxt() throws IOException {
int number = 0;
File file1 = new File("D:/test/1.txt");
File file2 = new File("D:/test/2.txt");
BufferedReader bfRead;
BufferedWriter bfWrite;
bfWrite = new BufferedWriter(new FileWriter(file2));
char[] arry = new char[1024];
FileReader fRead = new FileReader(file1);
bfRead = new BufferedReader(fRead);

    while ((number = bfRead.read(arry)) != -1) {
        bfWrite.write(arry, 0, number);
    }

    bfWrite.close();
    bfRead.close();

}

public void copyJpg() throws IOException  {

    File file1=  new File("D:/test/1.jpg");
    File file2= new File("D:/test/2.jpg");
    FileInputStream bfRead=new FileInputStream(file1);;
    FileOutputStream bfWrite=new FileOutputStream(file2);
    try {
        byte[] arry = new byte[1024]; // Define arrays to pass data between streams
        int count = 0;

        while((count = bfRead.read(arry)) != -1) {
            bfWrite.write(arry, 0, count);
        }


    } catch (FileNotFoundException e) {
        System.out.println("filenotfound");
    } catch (IOException e) {
        System.out.println("IoException");
    }
    bfWrite.close();
    bfRead.close();

}

}
</span>


Keywords: Java encoding Junit

Added by alfpalafox on Fri, 05 Jul 2019 22:00:42 +0300