IO stream summary in JAVA and JAVAIO stream Summary
This article is in the study of the summary, welcome to reprint but please note the Source: http://write.blog.csdn.net/postedit/42119261
I think you know something about java I/O streams, and you usually use a lot of them, but you may not know much about their specific classification and inheritance systems, you may rarely go to the relevant API documentation to find out the relationships and their application scenarios. This article sorts out common IO streams and provides examples to describe their applications. I hope this article will help you.
(A) IO streams are roughly divided into two types:
(1) byte stream: The corresponding abstract classes are InputStream (input stream) and OutputStream (output stream ).
(2) streams: the abstract classes are Reader (input stream) and Writer (output stream ).
(B) the specific implementation is as follows::
(1) byte stream: (commonly used) FileInputStream implements InputStream.
(Commonly used) OutputStream implements FileOutputStream.
(2) Hidden stream: (commonly used) BufferedReader, InputStreamReader, and StringReader implement Reader, and FileReader inherits InputStreamReader.
(Commonly used) BufferedWriter, OutputStreamWriter, and StringWriter implement Writer, and FileWriter inherits OutputStreamWriter.
(C) The implementation class details and usage are as follows:
Output stream:
(1) OutputStreamWriter is a bridge between the bytes stream and the byte stream. It uses the specified charset to encode the characters to be written to it as bytes. The character set used can be specified by the name or explicitly specified. Otherwise, the default Character Set of the platform may be accepted. Each time you call the write () method, the encoding converter is called for a given character (or character set. These bytes are accumulated in the buffer before being written to the Basic output stream. You can specify the buffer size. However, the default buffer size is large enough for most purposes. To achieve the highest efficiency, you can consider encapsulating OutputStreamWriter into BufferedWriter to avoid frequent converter calls. For example:
BufferedWriter out = new BufferedWriter (new OutputStreamWriter (System. out ));
(2) A convenient class used by FileReader to read character files. It is used to read character streams.
(3) BufferedReader has a buffer. It reads text from the character input stream and caches various characters to efficiently read characters, arrays, and rows.
(4) StringReader character input stream of a string.
(5) FileInputStream reads the original byte stream. It is often used to read binary files, files, such as slices, sounds, and images.
Input stream:
(1) InputStreamReader is a bridge between byte streams: It reads bytes using the specified charset and decodes them into characters. Each time a read () method is called, one or more bytes are read from the base input stream. To enable effective conversion from byte to character, you can read more bytes from the basic stream in advance, so that it exceeds the bytes required for the current read operation. To achieve the highest efficiency, InputStreamReader should be packaged in BufferedReader. Example: BufferedReader in = new BufferedReader (new InputStreamReader (System. in ));
(2) convenient class for FileWriter to write character files.
(3) BufferedWriter has a buffer to write text into the character output stream and buffer each character, thus providing efficient writing of a single character, array, and string.
(4) StringWriter refers to the character output stream of a string. It can be used to collect the output in the string buffer to construct the string.
(5) FileOutputStream is written to the original byte stream. It is often used to read binary files, files, such as slices, sounds, and images.
(D) analyze code instances as follows:
Import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. io. inputStream; import org. junit. test; public class TestStream {// read the character @ Testpublic void testRead () throws IOException {String path = "read.txt"; FileReader read = new FileReader (path); BufferedReader reader = new BufferedReader (read); while (reader. readLine ()! = Null) {String s = reader. readLine (); System. err. println (s);} reader. close () ;}// write characters to the file @ Testpublic void testWrite () throws IOException {String path = "write.txt"; FileWriter write = new FileWriter (path ); bufferedWriter writer = new BufferedWriter (write); char [] buffer = new char [1024]; for (int I = 0; I <1024; I ++) {if (char) 'A' + I> Character. MAX_VALUE) {buffer [I] = 'Z'; System. err. println (1);} els E {buffer [I] = (char) ('A' + I) ;}} writer. write (buffer); writer. close () ;}// delete file content @ Testpublic void testDelete () throws IOException {String path = "write.txt"; FileWriter write = new FileWriter (path ); bufferedWriter writer = new BufferedWriter (write); StringBuffer buffer = new StringBuffer (); writer. write (buffer. toString (); writer. close () ;}// replace content @ Testpublic void testDeleteAndReplace () throws IOException {String path = "write.txt"; FileWriter write = new FileWriter (path); BufferedWriter writer = new BufferedWriter (write); int I = 0; while (++ I <100) {StringBuffer buffer = new StringBuffer (); buffer. append (Math. random (); writer. write (buffer. toString () + "\ r \ n");} writer. close () ;}// copy the content of one File to another File @ Testpublic void copyFile (String oldPath, String newPath) {try {int byteread = 0; File oldfile = new File (OldPath); if (oldfile. exists () {// when the file exists, InputStream inStream = new FileInputStream (oldPath); // read the original file FileOutputStream fs = new FileOutputStream (newPath ); byte [] buffer = new byte [100]; while (byteread = inStream. read (buffer ))! =-1) {fs. write (buffer, 0, byteread);} inStream. close (); fs. close () ;}} catch (Exception e) {e. printStackTrace () ;}// read the response stream @ Testpublic static void testReadByte () {String path = "write.txt"; File file = new File (path); InputStream in = null; try {// read one byte at a time in = new FileInputStream (file); int tempbyte; while (tempbyte = in. read ())! =-1) {System. out. write (tempbyte);} in. close ();} catch (IOException e) {e. printStackTrace (); return;} try {// read multiple bytes at a time byte [] tempbytes = new byte [100]; int byteread = 0; in = new FileInputStream (path ); while (byteread = in. read (tempbytes ))! =-1) {System. out. write (tempbytes, 0, byteread) ;}} catch (Exception e1) {e1.printStackTrace () ;}finally {if (in! = Null) {try {in. close () ;}catch (IOException e1 ){}}}}}