Personal understanding:
After the difference between character stream and byte stream appreciated. If you want to read when the specified file encoding format, especially not the default format, you need to convert a stream, the stream should be noted that the character is a need to clear the buffer; when you need a quick read, you need to buffered stream. There is reasonable, and all have their advantages and disadvantages of each stream, paying particular attention to good!
A converted stream:
. 1, the OutputStreamWriter a bridge from character streams to byte streams: using the specified character code table, the inflow of characters to be written into byte code. Its role is, converted into a string of bytes according to the specified code tables, the stream using the bytes out of byte write.
public class Demo01 { public static void main (String [] args) throws IOException { // clear destination FileOutputStream fos = new new FileOutputStream ( "D: \\ \\ a.txt the Java" ); // Create a stream object default GBK conversion OSW = the OutputStreamWriter new new the OutputStreamWriter (fos, "UTF-. 8" ); // write word osw.write ( "Hello" ); osw.flush (); // release resources (flow function needs to be turned, fos here is osw call, you do not need to shut down by osw responsible for closing --- who calls, who is responsible for closing) osw.close (); }
In fact, maintenance OutputStreamWriter own stream buffer when the write method we call OutputStreamWriter object, the query will be held to a specified character code table, to be found in the character encoding value converted into the number of bytes stored in the buffer OutputStreamWriter area. Then call the refresh function or turn off data byte stream, or the buffer is full will use a byte stream buffer is written to the specified file.
2, the InputStreamReader is a bridge from byte streams to character streams: it uses the specified character code table reads bytes and decodes them into characters. It uses the character set may be specified by the name or given explicitly, or may accept the platform's default character set.
public class Demo02 { public static void main (String [] args) throws IOException { // clear the data source the FileInputStream FIS = new new the FileInputStream ( "D: \\ \\ a.txt Java" ); // create a conversion stream object InputStreamReader isr = new new the InputStreamReader (FIS, "UTF-. 8" ); int len = 0 ; char [] = CH new new char [1024 ]; // begin reading the while ((len = isr.read (CH)) = -. 1! ) { System.out.println (new new String (CH, 0, len)); // if you do not write 0 and len into 1024 bytes, back to spaces instead } // release resources isr.close (); } }
3, the difference between commutations and subclasses:
OutputStreamWriter InputStreamReader and character bytes and bridges: character conversion can also be called flow. Character conversion-flow principle: + byte stream coding table.
FileWriter and FileReader: as a sub-category, only convenient class as the operation characters of the file exists. When the file operation, using the coding table can not default parent class and subclass directly to complete the operation, simplifies the code.
InputStreamReader isr = new InputStreamReader (new FileInputStream ( "a.txt")); // default character set.
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"GBK");//指定GBK字符集。
FileReader fr = new FileReader("a.txt");
It features three of the code is the same, in which the third sentence is most convenient.
Note: Once you want to specify a different encoding, absolutely can not use the subclass must use character conversion flow. When a subclass of it?
condition:
1, the operation of the file. 2, using the default encoding.
to sum up:
Byte ---> character: do not understand ---> read to understand. You need to read. Input stream. InputStreamReader
Character ---> bytes: read to understand ---> I do not understand. You need to write. The output stream. OutputStreamWriter
public class the Copy { public static void main (String [] args) throws IOException { // clear the data source the FileInputStream FIS = new new the FileInputStream ( "D: \\ \\ a.txt Java" ); // Create a character input stream conversion InputStreamReader = ISR new new InputStreamReader (FIS, "Utf-8" ); // clear destination FileOutputStream fos = new new FileOutputStream ( "D: \\ \\ aa.txt the Java" ); // create a character output stream conversion OutputStreamWriter OSW = new new OutputStreamWriter (fos, "UTF-8 "); // 5. Start copying char [] = CH new new char [1024 ]; int len = 0 ; the while ! ((Len = isr.read (CH)) = -. 1 ) { osw.write (CH, 0 , len); osw.flush (); } // release resources isr.close (); osw.close (); } }
Second, the buffer flow:
In order to increase the speed of read and write IO stream:
1, the buffer byte stream:
public class Demo03 { public static void main (String [] args) throws IOException { Long Start = System.currentTimeMillis (); // clear the data source the FileInputStream FIS = new new the FileInputStream ( "E: \\ \\ shipin 1.rar" ) ; // Create a byte stream input buffer BufferedInputStream BIS = new new BufferedInputStream (FIS); // clear the destination a FileOutputStream fos = new new a FileOutputStream ( "D: \\ \\ shipin.rar Java" ); // Create a byte output buffer flow BufferedOutputStream bos =new new BufferedOutputStream The (fos); byte [] bytes = new new byte [1024 ]; int len = 0 ; the while (! (len = bis.read (bytes)) = -. 1 ) { bos.write (bytes, 0 , len) ; } Long End = System.currentTimeMillis (); // release resources bis.close (); bos.close (); System.out.println ( "packet bytes buffer 479MB compressed stream copying time" + (end -start) + "milliseconds!" ); } }
2, character buffer stream:
newLine () based on the current system, a line break is written!
public class the Copy { public static void main (String [] args) throws IOException { // clear the data source the FileReader fr = new new the FileReader ( "D: \\ \\ hello.txt Java" ); // Create a character input buffer stream BufferedReader = br new new BufferedReader (fr); // clear destination FileWriter fw = new new FileWriter ( "D: \\ \\ nihao.txt the Java" ); BufferedWriter BW = new new BufferedWriter (fw); String Line = null ; the while((line=br.readLine())!=null){ bw.write(line); bw.newLine(); bw.flush(); } bw.close(); br.close(); } }
Third, the flow of the operating rules:
The IO streams summarizes regular (four clear):
1, a clear one: to operate the data is the data source or data purposes.
源:InputStream Reader
目的:OutputStream Writer
According to first clear demand to read or write.
2, clear II: data byte is to be operated or text it?
source:
Byte: InputStream
Text: Reader
purpose:
Byte: OutputStream
Text: Writer
We have made it clear to the specific system.
3, specifically three: clear and specific device data is located.
Source device:
Hard disk: File File beginning.
Memory: arrays, strings.
Keyboard: System.in;
Network: Socket
The purpose device:
Hard disk: File File beginning.
Memory: arrays, strings.
Screen: System.out
Network: Socket
Which flow can clear and specific objects you want to use.
4, clear four: the need for additional functions?
Additional features:
Conversions? Conversion flow. InputStreamReader OutputStreamWriter
Efficient do? Buffer object. BufferedXXX