This is the 18th day of my participation in Gwen Challenge

What is IO?

The IO name is the abbreviation of Input and Output, which is the Input stream and Output stream. The input stream is used to read data from the source and the output stream is used to write data to the destination.

Characters of the flow

The character stream has two abstract classes: Writer and Reader. The subclasses FileWriter and FileReader can be used to read and write files. BufferedWriter and BufferedReader can provide buffering capabilities to improve efficiency.

public static void main(String[] args)  {
    try {
        test();
    } catch(IOException e) { e.printStackTrace(); }}private static void test(a) throws IOException {
    String str;
    // Create BufferedReader with system. in
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the character 'quit'.");
    // Read characters
    do {
        str=br.readLine();
        System.out.println("The character you entered is :"+str);
    } while(! str.equals("quit"));
    br.close();
}
Copy the code

Then we enter Hello and quit. The results are as follows:

Enter the character 'quit' to exit. Hello you have entered the following character :quit Hello you have entered the following character :quitCopy the code

Using the above example, we can briefly understand the character stream. In general, we mostly use character streams to read and write files, mostly text files, such as.txt files. Here we also incidentally introduce how to use.

/** ** Write and read files *@throws IOException
 */
private static void test2(a) throws IOException {
    // Create the path and name of the file to operate on
    String path ="E:/test/hello.txt";
    String str="hello world";
    FileWriter fw = new FileWriter(path);  
    fw.write(str);  
    fw.close();  

    FileReader fr = new FileReader(path);  
    StringBuffer sb=new StringBuffer();
    while(fr.ready()){
        sb.append((char)fr.read());
    }
    System.out.println("Output."+sb.toString());
    fr.close();
}
Copy the code

In the above code examples, we use FileWriter and FileReader to read and write files. Although characters can be written and read, the efficiency is not high, because it is direct reading and writing to disk. Usually for reading and writing files, we use buffering. The advantage of using buffering is similar to dumping garbage, which is sorted and piled up and then discarded when it reaches a certain size, rather than just dumping a little garbage once.

Add BufferedWriter and BufferedReader classes to the above code for buffering.

Code examples:

/** * Write and read files *@throws IOException
 */
private static void test3(a) throws IOException {
    // Create the path and name of the file to operate on
    String path ="E:/test/hello.txt";
    String str="Hello!";
    FileWriter fw = new FileWriter(path);  
    BufferedWriter bw=new BufferedWriter(fw);
    bw.write(str);  
    bw.close();
    fw.close();  

    FileReader fr = new FileReader(path);  
    BufferedReader br=new BufferedReader(fr);
    StringBuffer sb=new StringBuffer();
    while(br.ready()){
        sb.append((char)br.read());
    }
    System.out.println("Output."+sb.toString());
    br.close();
    fr.close();
}
Copy the code

Byte stream

Byte streams also have two abstract classes: InputStream and OutputStream. Its corresponding subclasses include FileInputStream and FileOutputStream to implement file read and write operations. BufferedInputStream and BufferedOutputStream provide buffer functionality

Byte stream can also read text, but its main use is to read binary files that cannot directly access text information, such as music files, video files, image files, and so on. Here we’re still reading and writing to the file, but we’re adding ‘hello’ to the new file from what we wrote in hello.txt. Since Chinese is used here, the encoding needs to be set accordingly.

Code examples:

* @throws IOException */ private static void test4() throws IOException {String path="E:/test/hello.txt"; String path2="E:/test/ hello.txt "; String STR =" Hello!" ; InputStream input = new FileInputStream(path); InputStreamReader reader = new InputStreamReader(input, "UTF-8"); StringBuffer sb=new StringBuffer(); while(reader.ready()){ sb.append((char)reader.read()); } input.close(); reader.close(); OutputStream output = new FileOutputStream(path2); // Create a file and write data to it. OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8"); writer.write(sb+str); writer.close(); output.close(); InputStream input2 = new FileInputStream(path2); InputStreamReader reader2 = new InputStreamReader(input2, "UTF-8"); StringBuffer sb2=new StringBuffer(); while(reader2.ready()){ sb2.append((char)reader2.read()); } system.out. println(" output :"+sb2); input2.close(); reader2.close(); }Copy the code

Results:

Output: Hello world!Copy the code

You can see that the results are in line with our expectations.