Conversion flow: output conversion character stream
The character written to the stream into byte code. The effect is that the string will be converted into bytes according to the specified code tables, the stream using the bytes out of byte write.
Conversion Flow Overview
OutputStreamWriter: character-output stream - write data
public OutputStreamWriter(OutputStream out)
public OutputStreamWriter(OutputStream out,String charsetName)
InputStreamReader: character input stream - the read data
public InputStreamReader(InputStream in)
public InputStreamReader (InputStream in, String charsetName )
to create a txt file encoding to utf-8 format, if the text file with your program is not a stopwatch, then converted with a conversion stream
com.oracle.demo01 Package; Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; Import java.io.OutputStreamWriter; Import java.io.UnsupportedEncodingException; public class Demo01 { public static void main (String [] args) throws IOException { // clear the destination a FileOutputStream fos = new new a FileOutputStream ( "D: \\ \\ a.txt Java"); // Create commutations OutputStreamWriter osw = new OutputStreamWriter (fos, "UTF- 8 "); // write characters osw.write (" starting in all but extraordinary "); osw.flush (); // release resources osw.close (); } }
package com.oracle.demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class Demo02 { public static void main(String[] args) throws IOException { //明确数据源 FileInputStream fis= new FileInputStream("D:\\java\\a.txt"); //创建转换流对象 InputStreamReader isr=new InputStreamReader(fis,"UTF-8"); int len=0; char[] ch=new char[1024]; //开始读 while((len=isr.read(ch))!=-1){ System.out.println(new String(ch,0,len)); } //释放资源 isr.close(); } }
A.txt copy the same file format as well as the contents of the file to a.txt
com.oracle.demo01 Package; Import a java.io.FileInputStream; Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; Import the java.io.InputStreamReader; Import java.io.OutputStreamWriter; Import java.io.UnsupportedEncodingException; public class the Copy { public static void main (String [] args) throws IOException { .. 1 // first data source specific FileInputStream fis = new FileInputStream ( "D : \\ java \\ a.txt") ; // 2 create a character input stream conversion. InputStreamReader ISR = new new InputStreamReader (FIS, "UTF-8"); // 3 clear destination. FileOutputStream fos = new new FileOutputStream ( "D: \\ \\ aa.txt the Java" ); // 4 create a character output stream conversion OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8"); //开始复制 char[] ch=new char[1024]; int len=0; while((len=isr.read(ch))!=-1){ osw.write(ch,0,len); osw.flush(); } //释放资源 isr.close(); osw.close(); } }
Buffered stream: the stream is accelerated to provide efficient write
Keywords: Buffered
Two bytes are buffered stream:
Buffered input stream of bytes: BufferedInputStream
Byte buffered output stream: BufferedOutputStream
com.oracle.demo02 Package; Import java.io.BufferedOutputStream; Import java.io.FileOutputStream; Import java.io.IOException; public class Demo01 { public static void main (String [] args) throws IOException { // clear the destination (output) wrote txt file FileOutputStream fos = new new FileOutputStream ( "D: \\ \\ hello.txt the Java"); // create a buffer objects (added to the output stream of bytes buffer) BufferedOutputStream new new BufferedOutputStream BOS = ( fos); // write byte bos.write ( "abc123" .getBytes ()); // release resources bos.close (); } }
The above code is the byte buffered output stream, which I want to clear the destination file, and then create a buffer used to speed up the write byte, who called on the release of the last who last ran, did not look into a success.
Buffered input stream of bytes: BufferedInputStream
I read data from a file inside
com.oracle.demo02 Package; Import java.io.BufferedInputStream The; Import a java.io.FileInputStream; Import java.io.IOException; public class Demo02 { public static void main (String [] args) throws IOException { // clear the data source (read) the TXT file read out the contents of the FileInputStream new new FIS = the FileInputStream ( "D: \\ \\ hello.txt Java"); // Create a byte stream input buffer BufferedInputStream BIS = new new BufferedInputStream (FIS); int 0 = len; the while ((len = bis.read ()) = -. 1!) { of System.out.print ((char) len); } // release resources bis.close (); } }
Character buffer two fractions:
Character buffer input stream: the BufferedReader eadLine (); row reads a text string that contains the contents of the line, does not contain any terminator, return null;
Character buffer output stream: BufferedWriter unique method newLine (); newline
com.oracle.demo02 Package; Import java.io.BufferedWriter; Import java.io.FileWriter; Import java.io.IOException; public class Demo03 { public static void main (String [] args) throws IOException { // clear the destination FW = new new FileWriter FileWriter ( "D: \\ \\ hello.txt Java", to true); // Create a character buffer output stream BufferedWriter, new new BufferedWriter, BW = (FW); // write byte bw.write ( "you well, the big bear ");! bw.flush (); // wrap bw.newLine (); bw.write (" I'm fine bald strong "); // release resources bw.close (); } }
package com.oracle.demo02; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Demo04 { public static void main(String[] args) throws IOException { //明确数据源 FileReader fr=new FileReader("D:\\java\\hello.txt"); //创建字符输入缓冲流 BufferedReader br=new BufferedReader(fr); String line=null; /*br.readLine(); System.out.println(line); line=br.readLine(); System.out.println(line); line=br.readLine(); System.out.println(line);*/ while((line=br.readLine())!=null){ System.out.println (Line); } // release resources br.close (); } }
eadLine () to read a line of text string that contains the contents of the line, not including any line terminator, if the end of the stream is null