IO stream packaging stream and encoding and decoding flow chart

0 The order of encoding and decoding and how to identify:

 



 

 

 

 

1 Summary outline:

 

Processing Streams: Enhancements, Provides Performance, Node Streams
1. Buffer stream
1), byte buffer stream
BufferedInputStream
	InputStream is =new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream
	OutputStream os =new BufferedOutputStream( new FileOutputStream(dest));
2), character buffer stream
BufferedReader   readLine()
	BufferedReader reader =new BufferedReader(new FileReader(new File(src)));
BufferedWriter    newLine()
	BufferedWriter wr =new BufferedWriter(new FileWriter(new File(dest)));

2. Convert stream: Convert byte stream to character stream to process garbled code (encoding set, decoding set)
1. The concept of encoding and decoding is program-centric
 Encoding: char---encoded charset>binary
 decode: binary -- decode charset --> char

 Number----->character
 character -----> number (encoding, human theme, programming human world symbols into the computer world)
2. Garbled code:
1) The character sets of encoding and decoding are not unified
2) Byte is missing, length is missing
3. The file is garbled
InputStreamReader(byte input stream, "decoded set")
OutputStreamWriter(character output stream, "encoding set")

 

 

2 code:

 

2.1 Byte-packed stream to implement copy:

/**
 * Byte stream file copy + buffer stream to improve performance
 * Buffered stream (node ​​stream)
 * @author Administrator
 *
 */
public class BufferedByteDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	}
	/**
	 * a copy of the file
	 * @param source file path
	 * @param directory file path
	 * @throws FileNotFoundException,IOException
	 * @return
	 */
		public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
			//1. Establish contact source (exist and file) + destination (file may not exist)  
			File src =new File(srcPath);
			File dest =new File(destPath);
			if(!src.isFile()){ //not a file or null
				System.out.println("Only copy files");
				throw new IOException("Only copy files");
			}
			//2, select stream
			InputStream is =new BufferedInputStream(new FileInputStream(src));
			OutputStream os =new BufferedOutputStream( new FileOutputStream(dest));
			//3, file copy loop + read + write
			byte[] flush =new byte[1024];
			int len ​​= 0;
			//read
			while(-1!=(len=is.read(flush))){
				//write out
				os.write(flush, 0, len);
			}
			os.flush(); //Force flush
			
			//close the stream
			os.close();
			is.close();
		}

}

 

 

2.2 Character wrapping stream to implement copy:

 

/**
 * Character buffer stream + new method (polymorphism cannot occur)
 * @author Administrator
 *
 */
public class BufferedCharDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//Create plain text whose source is limited to characters
		File src =new File("E:/xp/test/Demo03.java");
		File dest =new File("e:/xp/test/char.txt");
		// select stream
		BufferedReader reader =null;		
		BufferedWriter wr =null;
		try {
			reader =new BufferedReader(new FileReader(src));
			wr =new BufferedWriter(new FileWriter(dest));
			// read operation
			/*
			char[] flush =new char[1024];
			int len ​​= 0;
			while(-1!=(len=reader.read(flush))){
				wr.write(flush, 0, len);
			}*/
			// Operation of the new method
			String line =null;
			while(null!=(line=reader.readLine())){
				wr.write(line);
			//wr.append("\r\n");
				wr.newLine(); //newline symbol
			}
			wr.flush();//Force flush out
		} catch (FileNotFoundException e) {
			e.printStackTrace ();
			System.out.println("The source file does not exist");
		} catch (IOException e) {
			e.printStackTrace ();
			System.out.println("Failed to read file");
		}finally{
			try {
				if (null != wr) {
					wr.close();
				}
			} catch (Exception e2) {
			}
			try {
				if (null != reader) {
					reader.close();
				}
			} catch (Exception e2) {
			}
		}
	}

}

 

 

2.3 The ultimate file copy of the package stream, (this writing method can only copy text files, not applicable to pictures, audio, video, etc.), pay attention to the comments of the code

 

Experience the purpose of the File class being wrapped layer by layer, and the traversal method of the classes InputStreamReader BufferedWriter BufferedReader

use of readLine writeLine

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * Convert stream: bytes to characters
 * 1. Output stream OutputStreamWriter encoding (write human-readable characters into the computer to encode these characters numerically)
 * 2. Input stream InputStreamReader decoding (the files stored in the calculation are binary, and the binary numbers should be converted into human-readable characters to be interpreted and decoded)
 *
 * Make sure the source is not garbled
 * @author Administrator
 *
 */
public class ConverDemo02 {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		//Specify the decoding character set
		BufferedReader br =new BufferedReader( // Only this class can efficiently read characters, arrays and lines
				new InputStreamReader( // InputStreamReader is a bridge from byte stream to character stream: it uses the specified charset to read bytes and decode them into characters, which is the parent class of FileReader, and only InputStreamReader can specify the character set, InputStream--> Reader = InputStreamReader
					new BufferedInputStream(
							new FileInputStream(  
									new File("E:/xp/test/Demo03.java"))),"UTF-8")    -- 从       BufferedInputStream( FileInputStream(file) )  
				);
		//write out the file encoding
		BufferedWriter bw =new BufferedWriter(
				new OutputStreamWriter(
					new BufferedOutputStream(	
					new FileOutputStream(new File("E:/xp/test/encode.java")))));
				
		String info =null;
		while(null!=(info=br.readLine())){
			//System.out.println(info);
			bw.write(info);
			bw.newLine();
		}
		bw.flush();
		bw.close();
		br.close();
	}

}

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326493288&siteId=291194637