21-I / O (input and output stream)-characters and bytes

Hello everyone, I am a pig arched by cabbage.

1. Introduction to input and output streams

The code we knock at this stage is generally based on the console.
We output the data by System.out — System.out.print (“the output target is the screen”).
The input is System.in. It first defines a scanner object, scans the data entered by the user on the console
Scanner scan = new Scanner ( System.in ); the source of the input comes from the user input
(1) in the Console Judgment output and output stream
The judgment of input and output is based on the program.
Reading a file—input (in the program)
writing a file—output (information comes from the program)
(2) read is input, write (write) ) Is the output.
We imagine the program code as our own brain. When we read a book, is it a process of inputting knowledge into the brain. Then we had some ideas and wrote what we thought in our heads on paper. Is this an output process? Everything is based on our brain, that is, the program.
(3) JDBC, the input and output between the program and the database The input
Insert picture description here
stream and output stream are two data transmission channels, just like the straw when we drink a drink. We judge the direction of the input and output flow is very simple, that is, using ourselves as a reference, like when we and milk tea, is it hard to insert the tube and poke a hole. In the specific program, we just use the program as the reference object, insert the tube into the database, and then we get the data through this tube, so the direction of the input and output streams are all directed to the database. When we drink pearl milk tea, does it come up as soon as we suck the pearl in our mouth? The data is the same. The flow of the green arrow in the figure is the flow of data.
(4) File and file input and output
What we talked about earlier is the data trend between the program and the database. We want to copy the data in one file to another file, we want to see if there is a pipe between this file and the file, yes, the previous file is called the source file, and the file to be written into the data is called the target file. There is a small hole between them, which allows data to go out or in. The source file is inserted into the output stream pipeline, and the target file is inserted into the input stream pipeline. The two pipes have different names and are two independent pipes. It cannot be directly connected. Instead, it uses programs to implement data interaction. As shown in the figure, the direction of the input stream and output stream is based on the program. Think about how the straw is inserted.
(5) The data in the two common input and output streams
Reader / Writer is transmitted in the form of characters , mainly text . The data in
InputStream / OutStream is transmitted in the form of bytes , mainly hypertext (like pictures, audio, etc.) )

2. Let's talk about how to write the data in the file to another file

(1) First, we have to create two files , one is the source file and the other is the target file.

	//源
			File 源文件 = new File("d:/test0807/a.txt");
			
			//目标
			File 目标文件 = new File("d:/test0807/x.txt");

( 2) Create input streams, output streams (two pipes), and tell them where they are inserted.

			FileReader 输入流 = new FileReader(源文件);
			
			FileWriter 输出流 = new FileWriter(目标文件);

The FIleReader here is an input stream, and the parameter is to tell him where to insert, that is, which file to connect.
(3) Create a buffer area , save the data to the program first, and then write to the target file, where Reader / Wrieter is in the form of characters, so we define a character array to cache these data first. How long should the character array be? Like in real life, we have to transport a lot of sand from the production site to the construction site. We are not building a taxi that can be completely installed, but a lot of small cars are transported in multiple batches. This is also the reason for buffering. We think of the character array as a cart and transport it back and forth until it is finished, so it should be a loop here.
There is another situation that we need to consider. If we define the length of the character array to be 5 and the length of the source file to be 13, so it needs to be transported three times, so the length of the target file is 15. Is there space for 2 characters wasted? Kind of waste, write () can help us achieve

			char[] 临时缓冲区 = new char[5];
			
			int len = 0;
			while ((len = 输入流.read(临时缓冲区)) > 0) {
				输出流.write(临时缓冲区, 0, len);
			}
			

First define len = 0, use it to equal the length of the data read from the source file, if it is greater than 0, and then write it into the target file, equal to 0 means that the reading is finished. Output stream.write (temporary buffer, 0, len); means that the data in the buffer is followed by the data in the target file, and 0 represents the offset, for example, to say "Hello everyone, I am a handsome guy" , The first loop is written into "Hello everyone, I am" a total of 5 characters, if the offset is -1, then the following content is "Hello everyone, my handsome guy" will overwrite "Yes", len represents Write the length. In the explanation, the output stream.write (temporary buffer, 0, len); is to write the data of the temporary buffer to the length of len according to the offset of 0, according to the 13 mentioned before, and the final length is 3. So avoid Wasted.
(4) Don't forget to close the input stream and output stream after opening, and put the lid on when the drink is gone.

			输入流.close();
			输出流.close();

Below is the complete code

public static void main(String[] args) {
		try {

			//源
			File 源文件 = new File("d:/test0807/a.txt");
			
			//目标
			File 目标文件 = new File("d:/test0807/x.txt");
			
			FileReader 输入流 = new FileReader(源文件);
			
			FileWriter 输出流 = new FileWriter(目标文件);
			
			/*
			 * 输入流:可以读取源文件中的数据到【程序】中
			 * 输出流:可以把【程序】中的数据,写入到目标文件
			 * */
			
			//创建一个临时存储区域,buffer(缓冲)
			char[] 临时缓冲区 = new char[5];
			
			int len = 0;
			while ((len = 输入流.read(临时缓冲区)) > 0) {
				输出流.write(临时缓冲区, 0, len);
			}
			
			输入流.close();
			输出流.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

The following is an example of FIleInputStream / FileOutputStream The
difference is that one is a char array and the other is a byte array

public static void main(String[] args) {
		try {
			File 源文件 = new File("d:/test0807/normal.png");
			
			File 目标 = new File("d:/test0807/target.png");
			
			InputStream 输入流 = new FileInputStream(源文件);
			
			OutputStream 输出流 = new FileOutputStream(目标);
			
			byte[] buffer = new byte[1024];
			
			int len = 0;
			while ((len = 输入流.read(buffer)) > 0) {
				输出流.write(buffer, 0, len);
			}
			
			/*输入流.read(buffer);
			输出流.write(buffer);*/
			
			输入流.close();
			输出流.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
Published 24 original articles · praised 4 · visits 2038

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/98881555