Directory
- Objective
- Code Walkthrough
- Character Stream
- Filereader,filewriter:
- Bufferedreader,bufferedwriter:
- Inputstreamreader,outputstreamwriter:
- BYTE stream
- Fileinputstream,fileoutputstream
- Bufferedinputstream,bufferedoutputstream
- Objectoutputstream,objectinputstream
- Summarize
- IO Stream classification
- Input/output stream
- Byte Stream and character stream
Objective
In roost interview was asked the Io stream, NIO, and now often hear the Netty,io stream can answer a point, the others do not know how to answer;
Recently in the company also nothing to do, just learn to the IO stream, to tidy up it;
This is the Java IO Stream class diagram, many online, I am also on the internet
Code Walkthrough
In order to practice directly, all the documents involved in the text are created first;
Character Stream
All with reader,writer belong to character stream
- Generally, the plain text is manipulated to read the text file in characters, so the following instance is
char
an array of definitions
You can read multiple bytes at a time, depending on the Code table mapping character.
Filereader,filewriter:
Working directly with files
FileReader Filereader=null; FileWriter Filewriter=null; File Readfile=new file ("E:/javaio" +file.separator+ "Javastudy.txt"); File Writefile=new file ("e:/javaio/a" +file.separator+ "test.txt"); try {filereader= new FileReader (ReadFile); Filewriter=new FileWriter (WriteFile); Char[] Read=new char[1024]; int len=0; while (Len=filereader.read (read))!=-1) {filewriter.write (Read,0,len); }} catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }finally {try {if (filereader!=null) {filereader.close (); } if (Filewriter!=null) {filewriter.close (); }} catch (IOException e) {e.printstacktrace (); } }
Bufferedreader,bufferedwriter:
Puts a stream of file operations into a buffer, which is processed
BufferedReader
Can read a line of text, but the output needs to be interlaced, otherwise the data will be tightly together;
try {file reader = new File ("E:/javaio" + file.separator + "Javastudy.txt"); File writer = new file ("e:/javaio/a" + file.separator + "test.txt"); BufferedReader BufferedReader = new BufferedReader (new FileReader (reader)); BufferedWriter bufferedwriter = new BufferedWriter (new FileWriter (writer)); Char[] Read=new char[1024]; int Len; while (Len=bufferedreader.read (read))!=-1) {bufferedwriter.write (Read,0,len); }/* String line; while ((Line=bufferedreader.readline ())!=null) {bufferedwriter.write (line); Bufferedwriter.newline (); }*/Bufferedwriter.flush (); Bufferedreader.close (); Bufferedwriter.close (); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }
Note: Here called the flush
method, because the code snippet uses the buffer, the buffer data is only full before it is sent automatically, where the end of the full need to call the method to send the buffer data, otherwise there will be no data in the text;
Inputstreamreader,outputstreamwriter:
Converts a byte stream to a character stream, and is processed;
File reader = new File("E:/Javaio" + File.separator + "Javastudy.txt"); File writer = new File("E:/Javaio/a" + File.separator + "test.txt"); try { InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(reader)); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(writer)); char[] read=new char[1024]; int len; while ((len=inputStreamReader.read(read))!=-1){ outputStreamWriter.write(read,0,len ); } inputStreamReader.close(); outputStreamWriter.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
BYTE Stream Fileinputstream,fileoutputstream
Directly read the file for operation
try {/*//Picture file InputStream = new file ("E:/javaio" + file.separator + "1.png"); File OutputStream = new file ("e:/javaio/a" + file.separator + "1.png"); *//music file InputStream = new File ("E:/javaio" + file.separator + "Test.mp3"); File OutputStream = new file ("e:/javaio/a" + file.separator + "Test.mp3"); if (outputstream.exists ()) {outputstream.createnewfile (); } fileinputstream FileInputStream = new FileInputStream (InputStream); FileOutputStream FileOutputStream = new FileOutputStream (outputstream); Byte[] Read=new byte[1024]; int Len; while (Len=fileinputstream.read (read))!=-1) {fileoutputstream.write (Read,0,len); } fileinputstream.close (); Fileoutputstream.close (); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) { E.printstacktrace (); }
Bufferedinputstream,bufferedoutputstream
Putting a file action stream into a buffer and then manipulating it
Because it is the direct operation of the file, it is not possible to read one line, nor that method;
/*//Picture file InputStream = new file ("E:/javaio" + file.separator + "1.png"); File OutputStream = new file ("e:/javaio/a" + file.separator + "1.png"); *//music file InputStream = NE W File ("E:/javaio" + file.separator + "Test.mp3"); File OutputStream = new file ("e:/javaio/a" + file.separator + "Test.mp3"); if (outputstream.exists ()) {outputstream.createnewfile (); } bufferedinputstream Bufferedinputstream = new Bufferedinputstream (new FileInputStream (InputStream)); Bufferedoutputstream Bufferedoutputstream = new Bufferedoutputstream (new FileOutputStream (OutputStream)); Byte[] Read=new byte[1024]; int Len; while (Len=bufferedinputstream.read (read))!=-1) {bufferedoutputstream.write (Read,0,len); } bufferedoutputstream.flush (); Bufferedinputstream.close (); Bufferedoutputstream.close ();
Note: buffers are used here, and methods need to be called flush
;
Objectoutputstream,objectinputstream
Persist the object to the local TXT file, note that in the TXT file is binary code, not garbled, so do not think it is garbled;
User user=new User(); user.setUsername("username"); user.setPassword("password"); File out=new File("E:/Javaio/a"+File.separator+"object.txt"); if(out.exists()){ out.createNewFile(); } ObjectOutputStream outs=new ObjectOutputStream(new FileOutputStream(out)); outs.writeObject(user); outs.close(); ObjectInputStream inputStream=new ObjectInputStream(new FileInputStream(out)); System.out.println(inputStream.readObject()); inputStream.close();
There are many other classes and methods are not written, here only to get the operation of the entry, the Code section is here;
Summarize
IO stream
IO Stream classification
- Divided by Flow direction: input stream and output stream
- Divide by processing type: byte stream and character stream
Input/output stream
Input stream can only be read, output stream can only write
Byte Stream and character stream
- The byte stream can operate on all files, for example: Pictures, music files, movies, etc.; character streams only process text documents, such as txt text, XML,YML, etc.
- Byte stream is in bytes, the character stream is in characters, you can see the upper character stream of the code is all used in the
char
array, and the byte stream is all used in the byte
array
Refer to a
The content in this is very detailed;
Getting Started with Java IO