I remember that when I first started learning Java, I didn’t understand Java IO streaming, so I wrote this essay and hoped that it would be helpful to those who just started learning Java and facilitate their query in the future. Java IO streams are divided into character streams (Reader, Writer) and byte streams (InputStream, OutputStream). Byte streams, as the name suggests, byte streams are to read the contents of files into byte arrays and then output them to another file. The smallest unit of character streaming operations is characters. Let’s take a look at the overview of IO streams:
The following is the first thing to read and write the file through a character stream:
package lib;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class Test { // Define the file path File f = new File("F://test.txt"); // Method for writing character stream public String writeInFile() throws IOException{ String str = ""; String count = ""; try { // Use character stream to read the file BufferedReader bf = new BufferedReader(new FileReader(f)); while (true) { //Read each line of data and assign it to str if ((count = bf.readLine()) != null) { str += count; } else { break; } } // Close the stream bf.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } return str; } // Method for character stream reading public void getReader(){ try { // where true means to add at the end of the original file content. If not written, it means to empty the file and add the content. PrintWriter pw=new PrintWriter(new FileWriter(f,true)); pw.write("Test input string to file 2"); pw.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { Test test=new Test(); //Enter the string into the file test.getReader(); //Read the corresponding string String str=test.writeInFile(); //Open System.out.println("File content: "+str); }}There are comments in the key points of the above code, so I won't go into details one by one. The main thing is that you don't forget to close after using the stream.
Then operate on the file through a byte stream, copying the contents of one file to another:
package com.file.test2;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class TestFile2 { //Read and write files using byte streams, copy one file to another public static void main(String[] args) throws IOException { //The source file to be copied f=new File("D://test.txt"); //Target file File f2=new File("D://test2.txt"); //Define an array of byte type to store the read content byte [] b=new byte[1024]; int length; try { //Define the read stream FileInputStream in=new FileInputStream(f); //Define the stream output to the file FileOutputStream out=new FileOutputStream(f2); //Output the file content to another file while((length=in.read(b))!=-1){ out.write(b, 0, length); } out.close(); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }}In the operation of byte stream, the source file on line 13 must exist. You can change the file path yourself as needed. You only need to exist. Otherwise, an error that the file cannot be found will be reported. At the same time, if you want to output the content of the read byte stream on the console, you can add two codes between lines 27 and 28: in.read(b, 0, b.length);System.out.println(new String(b));
The above are related operations of character streams and byte streams. In fact, the code is not difficult, it is mainly about one's own understanding. Everyone will have different ways of understanding the same problems. Of course, for us programmers, in addition to thinking more, we also need to do more. Finally, I hope the above content will be helpful to everyone, and please continue to support this site.