InputStreamReader, OutputStreamReader conversion stream usage

InputStreamReader, OutputStreamReader conversion stream usage

  1. Conversion stream: belongs to the character stream
  • InputStreamReader: Convert a byte of input into a character input stream
  • OutputStreamReader: Convert a character input into a byte input stream
  1. Role: Provide conversion between byte stream and byte stream main
  2. Encoding: byte, byte array converted to character array string
    Decoding: character array, string converted to byte, byte array
public classInputStreamReaderTest{
    
    
	@Test
	public void test(){
    
    
		FileInputStream fis = new FileInputStream("hello.txt");
		//如果不指定自字符集使用默认的字符集
		//InputStreamReader isr = new InputStreamReader(fis);
		//具体使用哪个字符集,要看文件在保存的时候使用的是什么字符集
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
		
		char[] buf = new char[20];
		int len;
		while((len = isr.read(buf)) != -1){
    
    
			String str = new String(buf,0,len);
			//输出到控制台,也可以输入到别的文件中
			System.out.println(str);
		}
		isr.close();
	}
}

Insert picture description here

Character set of the converted file

@Test 
public void test() throws IOException{
    
    
	File file1 = new File("hello1");
	File file2 = new File("hello1");
	
	FileInputStream fis = new FileInputStream(file1);
	FileOutputStream fos = new FileOutputStream(file2);
	
	InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
	OutPutStreamWriter osw = new OutPutStreamWriter(osw,"gbk");
    
    char[] cbuf = new char[20];
    int lenth= 0;
    //读写过程
    while((lenth = isr.read(buf)) != -1){
    
    
		osw.write(cbuf,0,len);
	}
	//关闭资源
	isr.close();
	osw.close();
}
  • In order to make it easier to read and throw exceptions, the actual development uses try catch finally to ensure that the resources are closed.
  1. character set
  • ASCII: American Standard Information Interchange Code, which can be represented by 7 bits of one byte
  • ISO8859-1: Latin code table, European code table, represented by 8 bits of one byte
  • GB2313: China's Chinese encoding table, up to two bytes to encode all characters
  • GBK: China's Chinese encoding table has been upgraded, incorporating more Chinese text symbols, up to two bytes of encoding
  • Unicode: The international standard encoding, which combines all the characters currently used by humans, assigns a unique character code to each character, and all characters are represented by two bytes
  • UTF-8: variable length encoding method, 1-4 bytes can be used to represent a characterInsert picture description here

Guess you like

Origin blog.csdn.net/weixin_43941676/article/details/108412735