Copy of IO Stream Implementation Files

Keywords: Big Data ascii network Java encoding

Flow (IO stream): input-output-stream.

  • Function: To achieve communication between two devices.

  • Devices: Memory, Hard Disk, Console, Keyboard, File, Network

  • Network: On the network, outside of your host environment.

  • Classification of flows:

    • Classification by operation: input stream and output stream

    • Classification by data type: byte stream and character stream

      • Byte streams: bytes that are transmitted and can transmit any type of data. - - Video, audio, files, pictures, etc.
      • Character Stream: The transmission is also bytes. The difference is that the encoding operation is added in the process of transmission, which makes it more convenient for us to transfer characters - --- files.
  • Reference to memory

    • Byte stream: - corresponding parent class
    • Byte input stream: InputStream
    • Byte output stream: OutputStream
  • Character stream: - corresponding parent class

  • Character Read-in Flow (Input Data into Memory): Reader

  • Writer: Writer

  • Processing the character stream first:

  • Example: Take disk storage as an example, write content to a file

    • Analysis:
    • Because the transmission is a character-character stream
    • Because it's out of memory - out of write - FileWriter

Note:

  • Note 1: The characteristics of the associated file: If it does not exist before, the program will automatically create one, if it already exists, use it directly. But it will overwrite the content of the original file.
  • Note 2: If you only write the name, not the specific path, the default path is the current project.
  • Note 3: We can specify our own path, but we must ensure that the path exists. Otherwise, an exception is reported: FileNotFoundException: W: temp1. TXT (the system can not find the specified path). )
  • Note 4: When the write method is executed, the data is temporarily written to the internal array of the stream object, which is a byte array, or the default code table is fetched.
  • Note 5: The stream must be closed after use
  • Note 6: When the stream object is closed, no more operations can be performed, otherwise an exception is reported: Stream closed
public static void main(String[] args) throws IOException {
	//Create files that write out stream objects and associate content written out
	/*
	 * Note 1: The characteristics of the associated file: If it does not exist before, the program will automatically create one, if it already exists, use it directly. But it will overwrite the content of the original file.
	 * Note 2: If you only write the name, not the specific path, the default path is the current project.
	 * Note 3: We can specify our own path, but we must ensure that the path exists. Otherwise, an exception is reported: FileNotFoundException: W: temp1. TXT (the system can not find the specified path). )
	 */
	FileWriter fileWriter = new FileWriter("temp1.txt");
	
	//Call write-out method
	/*
	 * Note 4: When the write method is executed, the data is temporarily written to the internal array of the stream object, which is a byte array, or the default code table is fetched.
	 */
	fileWriter.write("bingbingAndChenchen");
	
	//Call the refresh method -- it actually writes the content to the file
	//fileWriter.flush();
	
	//Close the flow - a: Close the flow b: refresh
	//Note 5: The stream must be closed after use
	fileWriter.close();
	//Note 6: When the stream object is closed, no more operations can be performed, otherwise an exception is reported: Stream closed
	//fileWriter.write("zhangsan");
}

File Writer (String file, Boolean value)

  • When value is true, the original content is not deleted

  • It's an escape character\t:tabnormal slash

  • Implementing newline:r\n-linux:\n under windows

public static void main(String[] args) {
	//Create objects and associate files
	FileWriter fileWriter = null;
	try {
		fileWriter = new FileWriter("temp2.txt",true);
		
		//write
		fileWriter.write("bingbing");
		fileWriter.write("\r\n");
		fileWriter.write("zhansan");
		
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		//Close
		if (fileWriter != null) {
			try {
				fileWriter.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

There are two ways to read a document:

  • First: read() - A character-by-character reading
  • The second is read (character array) - multiple characters can be read at a time, and the read characters can be temporarily stored in this array.

    Example: Take data out of a file - File Reader

First: read() - A character-by-character reading

public static void main(String[] args) throws IOException {
		//Create objects and associate files. The associated files must exist
		FileReader fileReader = new FileReader("temp2.txt");
	
		
		//* The first type: read() -- a character-to-character reading
		//a: Read the current character out b: Move the current pointer back by one character
		int data;
//		data = fileReader.read(); // Here we get the ASCII value
//		System.out.println((char)data);
//		data = fileReader.read(); // Here we get the ASCII value
//		System.out.println((char)data);
//		data = fileReader.read(); // Here we get the ASCII value
//		System.out.println((char)data);
//		data = fileReader.read(); // Here we get the ASCII value
//		System.out.println((char)data);
//		data = fileReader.read(); // Here we get the ASCII value
//		System.out.println((char)data);
//		data = fileReader.read(); // Here we get the ASCII value
//		System.out.println(data); // / Here returns - 1, which means read out.
		
		while ((data = fileReader.read()) != -1) {
			System.out.println((char)data);
		}
		
		//Closed flow
		fileReader.close();
	}

The second is read (character array) - multiple characters can be read at a time, and the read characters can be temporarily stored in this array.

public static void main(String[] args) throws IOException {
		//Create objects and associate files. The associated files must exist
		FileReader fileReader = new FileReader("temp2.txt");
		
		
		/*
		 *Arrays are temporary places to store data. We put the characters we read into the array. The size of the array determines the number of characters we can read at a time.
		 *But from the memory point of view, the larger the temporary array, the better. The size of the general array is about 1Kb.
		 * Return value: Represents the number of real characters read this time, and if the return value is - 1, it means read out.
		 */
		char[] arr = new char[2];
		int num;
//		num = fileReader.read(arr);
//		System.out.println(new String(arr,0,num)+"    "+num);
//		num = fileReader.read(arr);
//		System.out.println(new String(arr,0,num)+"    "+num);
//		num = fileReader.read(arr);
//		System.out.println (new String (arr, 0, num) +"+num);// stands for converting num characters of character array from subscript 0 to string
//		num = fileReader.read(arr);
//		System.out.println(new String(arr)+"+num);//-1, indicating that you have finished reading
		
		while ((num = fileReader.read(arr)) != -1) {
			System.out.println(new String(arr,0,num)+"    "+num);
		}
		
		//Closed flow
		fileReader.close();
	}

Explanation of the Path:

  • Relative path and absolute path

  • Example: D: workspace BigData 1715N19 src com qianfeng test Demo1. Java absolute path

  • Com qianfeng test Demo1. Java relative path

  • Relative Path: Starting somewhere in the middle of the path and continuing to the current file name

  • Absolute path: The complete path of a file, that is, the path starting from the root directory.

  • In the path, / \ is an escape character \==/

Example: Implementing file replication

  • Requirement: Copy Demo1 content to temp3.txt
public static void main(String[] args) throws IOException {

		//1. Create read-in and write-out streams and associate the corresponding files
		FileReader fileReader = new FileReader("src/com\\qianfeng\\test\\Demo1.java");
		FileWriter fileWriter = new FileWriter("temp3.txt");
		
		//2. read and write
		//More than one character at a time
//		char[] arr = new char[10];
//		int num;
//		while ((num = fileReader.read(arr)) != -1) {
//			// Write part of the character array to a file
//			fileWriter.write(arr, 0, num);
//		}
		
		//Read a character at a time
		int num;
		while ((num = fileReader.read()) != -1) {
			fileWriter.write(num);//Encoding
		}
		
		//3. Closing resources
		fileReader.close();
		fileWriter.close();
	
	}

Posted by Wales on Mon, 28 Jan 2019 17:27:15 -0800