Understanding the basic problems of Java IO flow

 

catalog

Q: the bottom two bytes of char. Why use Java byte stream to read the English letters in the file. txt to output directly instead of half an English letter?

Q: why is input and output not literal meaning?

Q: understanding of flush() and close() in io flow

Why do we put the close() method and flush() method together to understand the difference in basic functions,

Q: Operation problems of read() and write() methods

Q: analysis of the method of reading data

Q: the bottom two bytes of char. Why use Java byte stream to read the English letters in the file. txt to output directly instead of half an English letter?

A: because I read Windows files, the letters in windows are one byte, and the letters in Java take up two bytes. So what's the relationship between reading Windows files and java files

Q: why is input and output not literal meaning?

A: because we are based on memory:

  • Come out of memory to get the output, that is, read the data from memory, and read corresponds to Input
  • Enter into memory, input data into memory, i.e. write data to memory, write corresponding to Output write

Understand that Input and output are all for memory, Input and Input to memory. Take data from other data sources and enter it into memory. Output is the same. Not according to the literal meaning

I feel depressed, but it's much clearer after I understand the memory

Q: understanding of flush() and close() in io flow

A:flush()  :

Why use the flush () method to clear the buffer data? When the data is read, it doesn't mean that your data has been written. There may be some left in the buffer, If we call the close() method directly to close the output bytes, this part of the data may be lost, so we should use flush() before closing the read / write stream, and then force the input of all the remaining data to end and empty the data.

API document explanation:

 close() :

Close the input and output flow, then why use the close() method? Because we can understand the flow as a pipe. If we don't close it, the IO resources will be occupied by it all the time. The channel will always be open. If we don't block the water in the straw, it will not always flow out. Moreover, in Java, the garbage collector will not actively recycle this resource, In this way, there is no way for others to use it, which will cause waste of resources.

API document explanation:

There's nothing to look at:

Why do we put the close() method and flush() method together to understand the difference in basic functions,

  • There is a point, the close() method. As long as the input or output stream is a stream, the close() method must be added finally, and it is generally in finally to ensure that it will be executed
  • At present, flush() is only used in IO read operation, and it is used in Output to ensure that the data will be Output, especially the very small data. However, it has no effect in Input, and the tool has not provided (laughter ~)

Q: Operation problems of read() and write() methods

A: Controls whether writes are overwritten

This depends on the fact that "true" is not overwritten when you read it, and "false" is appended after it. Overwrite the file

output2=new FileOutputStream("myfile",true); 

 

The control of reading and writing depends on your choice

System.out.print(new String(bytes,0,readCount));//read

output2.write(bytes,0,2);//write in

API document explanation:

read(): write the same

 

 

Q: analysis of the method of reading data

A: see the code (in the learning after IO flow, the functions of sub implementation classes are basically the same, and understanding FileInputStream and FileOutputStream can better understand the following input and output classes)

Attachment: flow to be mastered in basic learning stage:

public class Test4 {//How to read data

	public static void main(String[] args) {
		FileInputStream file = null;
		try {
     /*
    *(1) The most basic method, loop judgment, byte by byte output until - 1 is encountered
    *(2) The second method is to use byte array to store, but because array is used, so in the end
    *    If all elements in the array length are not covered in one output, the output
    *    It will add the value obtained last time but not covered
    */

			file = new FileInputStream("temp.txt");
			//byte[] bytes=new byte[4]; / / use byte [] array to read one by one, which will interact with memory many times and affect efficiency
			/*while(true) {//Mode 1
				int readCount=file.read(bytes);
				if(readCount==-1)
					break;
				System.out.print(new String(bytes,0,readCount));*/
			
/*			int readCount=0;//Mode 1 optimized version
			while((readCount=file.read(bytes))!=-1) {
				System.out.print(new String(bytes,0,readCount));
			}*/		
			
			
/*			byte[] bytes=new byte[file.available()];//Mode 2: available means to return the remaining bytes
			int readCount=file.read(bytes);
			System.out.println(new String(bytes));*/
			
		byte[] bytes=new byte[file.available()];
			file.skip(3);//Skip method, how many bytes to skip reading
			int readCount=0;
			while((readCount=file.read(bytes))!=-1) {
				System.out.print(new String(bytes,0,readCount));
			}	
			
	





			
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {//Because finally will be executed, and the flow needs to close the channel, so add the close method here
			if(file!=null) {
				try {
					file.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
			
		}
}

Put forward a set of children to understand, the flow behind slowly understand

public class Test7 {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
/*		FileInputStream file=new FileInputStream("Test4_1.java");File flow
		InputStreamReader reader=new InputStreamReader(file);//Conversion flow
		BufferedReader br =new BufferedReader(reader);*/ //Buffer stream
		BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("Test4_1.java")));//After a baby set, it's done with a line of code
		String line=null;
		while((line=br.readLine())!=null)
		{
			System.out.println(line);
		}
		br.close();
	}

}

 

That's all for now. There are new editors in the back

Tags: Java Windows

Posted on Fri, 26 Jun 2020 02:18:11 -0400 by springo