1:io Flow system: Read and write data to the operation. So IO is either read or write.
2:io classes related to streams: java.io packages.
IOException exceptions are generated for IO operations
3:io: Reference is a program,
I:input. Come in and read the data into the program.
O:output: Go out and write the data out of the program.
4:io the classification of streams, the related classes will have corresponding words.
4.1: From the data flow points. Personal understanding is that the data exchange between the memory and the hard disk, the data on the memory is temporary, and the data on the hard disk is permanent.
Input: Read the data and stream the data to the program. Or, put the data on the hard disk in memory
Output: Writes out data and writes the data out of the program. Or, put the memory data on the hard drive.
4.2: Byte stream and character stream
Stream: Byte stream, all data can be manipulated with a byte stream. What the computer can really recognize is 010101 of these things, 8 0 or 1 groups of synthetic bits, and 8 bits is a byte.
Reader or writer: a stream of characters. The character stream is a coding mechanism based on the byte stream. Only the text can be manipulated, although the text, more aptly speaking is a text file, does not include. doc and other Rich text files, because these files can be placed in pictures or other non-text content. Not able to manipulate pictures or audio files.
Inputsteam: reads the byte stream. OutputStream: Output byte stream.
Reader: Reads a stream of characters, Writer: writes a stream of characters.
5: File byte stream
OutputStream,: Abstract class
File output byte stream: FileOutputStream: node stream, associated with the target device.
Example one:
1 ImportJava.io.FileOutputStream;2 Importjava.io.IOException;3 4 Public classFileOutputStreamDemo1 {5 6 Public Static voidMain (string[] args)throwsIOException {7 //1. Create a file output stream object with the parameter full path name8FileOutputStream fos =NewFileOutputStream ("E:\\aa.txt");9 //2. Write the DataTen //2.1 Preparing Data OneString str = "Data to write"; A //2.2 Convert data to byte array form - byte[] B =str.getbytes (); - //3.0 Write the data in the destination file in bytes the /*for (int i=0; i<b.length; i++) { - Fos.write (B[i]); - }*/ - //3.1 The following is the output of the entire array, and 3.0 can reach the same effect + Fos.write (b); - //4. Releasing Resources + fos.close (); A } at -}
file Output Byte stream example one
Bufferedinputstream: The flow must have a node stream before it can be processed. In order to increase efficiency. The way to handle this is to wrap up the node stream
1 Public Static voidTestbufferedoutputstream ()throwsioexception{2 //when using a byte stream with buffers, you need to have a node stream as a parameter, or specify a target file first .3FileOutputStream fos =NewFileOutputStream ("D:\\test.txt");4Bufferedoutputstream BOS =NewBufferedoutputstream (FOS);5 6 //write the following text to the target file.7String str = "These words are about to be written to a text file via a byte stream with buffers";8 Bos.write (Str.getbytes ());9 //after writing, remember to refresh to actually write to the file, otherwise it will be saved in the buffer until the resources are freedTen Bos.flush (); One //Freeing Resources (closing the stream) A bos.close (); - -}
byte stream with buffer
InputStream: abstract class.
File input byte stream: FileInputStream: node stream, associated with the file to be read
1 ImportJava.io.FileInputStream;2 Importjava.io.IOException;3 4 Public classFileInputStreamDemo1 {5 Public Static voidMain (string[] args)throwsIOException {6 //1. Create a byte input stream object7FileInputStream FIS =NewFileInputStream ("E:\\bb.txt");8 //2. Start reading data9 //System.out.println ((char) fis.read ());Ten intb; One while((B=fis.read ())!=-1){ ASystem.out.print ((Char) b); - } - fis.close (); the } -}
file byte output stream
Bufferedoutputstream: With buffer. Also to increase efficiency, the way to handle this is to wrap up the node stream
ImportJava.io.BufferedInputStream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;/** Output The text of a file to the console*/ Public classBufferedInputStreamDemo1 { Public Static voidMain (string[] args) {FileInputStream fis=NULL; Bufferedinputstream bis=NULL; Try{FIS=NewFileInputStream ("D:\\aa.txt"); Bis=NewBufferedinputstream (FIS); //The byte array is used to load the read data byte[] B =New byte[1024]; intLen =-1; while(len = Bis.read (b))!=-1) {System.out.println (NewString (b, 0, Len)); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally{ Try{bis.close (); } Catch(IOException e) {e.printstacktrace (); } } }}
file byte input stream with buffer
6: Byte stream: in the file system, any file is stored in binary form, binary with bit embodiment (8 bits for a binary unit), 8 bits = 1 bytes, so the byte stream can handle any file.
Some of the common classes of 6.1-byte throttling:
6.1.1 input Stream Read (), reading to the end returns-1
Commonly read (byte[] b, int off, int len) reads a maximum of Len bytes of data from this input stream into a byte array.
FileInputStream: File input stream
Bufferedinputstream: Input stream with buffer, first pass in a node stream (input stream) as a parameter
6.1.2 output stream Write ()
Common write (byte[] b, int off, int len) writes the bytes in the specified byte array from the offset to the off
len
file output stream.
FileOutputStream: File input stream
Bufferedoutputstream: Input stream with buffer, first pass in a node stream (input stream) as a parameter
A tool to write a file copy with the input/output stream:
Public Static BooleancopyFile (String filepath,string target) {BooleanFlag =false; //output stream, writing data to the destination fileFileOutputStream fos =NULL; //input stream, the text that will be used to get to the programFileInputStream FIS =NULL; //reading text byte[] B =New byte[1024]; //valid length of character data per read intLength =-1; Try{FIS=NewFileInputStream (FilePath); FOS=NewFileOutputStream (target); /** Fis.read (b): reads 1024 bytes each time from a file associated with the FIS, and saves the bytes read to this array, * but this file is not just 1024. Big, it is possible to read several times, so to use the loop * to find the condition is to read the file is not read, is read to the end of the reading, * read to the end of the return value is-1, if the return value is-1 Stop the loop * */ while((Length=fis.read (b))! =-1) {//read to the end of the file returns-1, not read down//writes the read data to the destination fileFos.write (b, 0, length); Fos.flush ();//to brush data to a destination file} System.out.println ("File copy succeeded!" "); } Catch(FileNotFoundException E1) {e1.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); }finally{ //turn off freeing resources Try{fos.close (); Fis.close (); } Catch(IOException e) {e.printstacktrace (); }} Flag=true; returnFlag; }
Io stream (i) byte stream