First, the process flow
package cn.Buffered; import java.io.BufferedReader; import java.io.BufferedWriter; /* * 字符缓冲流+新增方法(不能发生多态) */ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; public class Buffereds { public static void main(String[] args) throws IOException { File src=new File("C:/Users/Administrator/Desktop/sun/a.txt"); File src2=new File("C:/Users/Administrator/Desktop/sun/b.txt"); BufferedReader reader=null; BufferedWriter writer=null; try { reader=new BufferedReader(new FileReader(src)); writer=new BufferedWriter(new FileWriter(src2,true)); //3.读取操作 // char[] s=new char[1024]; // int len=0; // try { // while(-1!=(len=reader.read(s))) { // String info=new String(s,0,len); // System.out.println(info); // writer.write(s, 0,len); // } String line=null; while(null!=(line=reader.readLine())) { writer.write(line); // writer.append("\r\n"); writer.newLine(); //换行符 } writer.flush(); } catch (IOException e) { e.printStackTrace(); System.out.println("文件读取失败"); } } }
Second, converting streams: byte stream into a character stream processing distortion (code set decoded set) byte character encoding, decoding byte character
1. The encoding and decoding concept
Encoding: binary coded character set character ---
Decoding: --- decoding binary character set character
2. garbage problem
1) encoding and decoding character set is not uniform
2) lack of bytes, length is lost
Sample Code
Package cn.Buffered; Import java.io.UnsupportedEncodingException; public class to Test01 { public static void main (String [] args) throws UnsupportedEncodingException { String str2 = "Chinese" ; byte [] = Data str2.getBytes (); // Word incomplete section number System.out.println ( new new String (Data, 0,3 )); // Test01 (); } // encoding and decoding must be the same character set, otherwise distortion public static void Test01 () throws{UnsupportedEncodingException // decode ---- char byte String STR = "Chinese"; // GBK // encoding ---- byte char byte [] = Data str.getBytes (); // encoding and decoding uniform character set; System.out.println ( new new String (Data)); byte [] = str.getBytes DATAS ( "UTF-. 8"); // set coded character set // not uniform distortion System.out.println ( new new String (DATAS)); @ encoded byte [] = DATA2 "Chinese" .getBytes ( "UTF-. 8"); // into byte code //Decoding STR = new new String (DATA2, "UTF-. 8"); // decode a string System.out.println (STR); } }
Commutations: Switch byte character
* 1, the output stream encoding OutputStreamWriter
* 2, decodes the input stream InputStreamReader
Package cn.Buffered; Import java.io.BufferedReader; Import java.io.File; Import a java.io.FileInputStream; Import java.io.FileNotFoundException; Import java.io.IOException; Import the java.io.InputStreamReader; Import Java. io.UnsupportedEncodingException; / * * commutations: Switch byte character * 1, the output stream encoding OutputStreamWriter * 2, decodes the input stream InputStreamReader * * / public class CoverDemo { public static void main (String [] args) throws IOException { / /指定解码字符集 BufferedReader br=new BufferedReader( new InputStreamReader( new FileInputStream( new File("C:/Users/Administrator/Desktop/sun/a.txt")),"UTF-8") ); String info =null; while(null!=(info=br.readLine())) { System.out.println(info); } br.close(); } }