One: Concept
1. Character, Byte, character set (encoding)
bytes are the units in which memory stores data, and characters are data. bytes are the basic unit of storage capacity, 1 bytes = 8 bits, that is, a 8-bit binary number, is a very specific storage space, such as 0X01,0XFA.
Characters are letters, numbers, Chinese characters, and various symbols, such as ' 1 ', ' Medium ', ' # '. A character is represented in a computer with a binary number of bytes, a west character of 1 bytes, and a 2-byte Chinese character.
Character Set (encoding): a standard that specifies which characters are used, each character is stored with one or more bytes, and which bytes are stored;
2. Stream, character stream and byte stream
Flow: An ordered set of data sequences that can be understood as a transport channel. The IO stream in Java is the input and output stream.
How can I tell if I am an input stream or an output stream?
The memory is the reference, if the data is to the memory flow is the input stream, and vice versa is the output stream.
Think of the input and output two endpoints as two factories, the factories need to transport goods between each other, and the flow is two factories between the road, no road can not transport each other, as for the characters, bytes and binary can be regarded as the mode of transport and unit size, for example, the binary into a tricycle, then the byte is a small truck, The character is a more advanced set of cards and the like.
The byte stream can handle all types of data, such as MP3, picture, text, read, read to a byte and return one bytes; The classes in Java that correspond to byte streams end with "Stream";
a character stream can handle only plain text data, read to one or more bytes, look for the specified encoding table, and then return the found characters. the classes corresponding to the character stream in Ava End with "Reader" or "Writer";
The bottom of the character stream is the byte stream. and the character stream is mainly read the text file content, you can read a character one character, you can read a line of text file content. and the byte stream reading unit is byte.byte as the computer storage most basic unit, may use the byte stream to read many other formats the file, the slice video and so on. Based on B/S and c/s file transfer can be used in the form of byte stream.
Two: function
1. Include in the input and output function of the package java.io, all methods encountered errors will throw IOException exception.
The 2.java stream is divided into two streams
BYTE stream: Can be used to read and write binary files and any type of file
Character stream: Can be used to read and write file text
BYTE stream: input (inputstream) output (OutputStream)
Character stream: input (Reader) output (writer)
The read () method of the Reader class returns the type int: The character read as an integer (two bytes total 16 bits), the range between 0 and 65535 (0X00-0XFFFF), or 1 if the end of the stream has been reached (indicating that the read has been successfully completed)
The read () of InputStream also returns int, but since this class is byte-oriented and has 8 bits for one bytes, it returns an int byte value in the range of 0 to 255 . If no bytes are available since the end of the stream has been reached, the return value-1. So for values that cannot be represented by 0-255, a character stream is used to read it! such as Chinese characters.
3.FileInputStream is the file input stream, which inherits from InputStream.
In general, we use FileInputStream to get input bytes from a file.
FileOutputStream is the file output stream, which inherits from OutputStream.
In general, we use FileOutputStream to write data to the output stream of File or FileDescriptor.
4. A stream with a cache
If you do not specify a buffer size, the default is to allocate 32 bytes.
The Java.io.BufferedReader and Java.io.BufferedWriter classes each have a buffer of 8192 characters.
BufferedReader: A wrapper class that can wrap a stream of characters, put a stream of characters into the cache, first read the character into the cache, or when the cache is full or you flush, and then read into memory, designed to provide read efficiency. If the buffer data is not sufficient, it will be read from the file again.
BufferedWriter: The data written is not output to the destination first, but is first stored in the buffer. If the data in the buffer is full, the destination is written out one at a time.
5. When the user input is read directly from the standard input stream system.in, the user enters one character at a time and system.in reads one character. To be able to read the input of a row of users at a time, BufferedReader is used to buffer the characters entered by the user. The ReadLine () method passes the entire line of strings again when it reads the newline character of the consumer.
System.in is a bit stream, in order to convert to a character stream, you can use InputStreamReader for character conversion, and then use BufferedReader for it to add buffering capabilities
Three: Process
Operation Flow
In Java, the IO operation also has the corresponding steps, take the file operation as an example, the main operation flow is as follows:
1 opening a file using the file class
2 Specify the location of the output by a subclass of Byte stream or character stream
3 for read/write operations
4 off input/output
IO operation is a resource operation, always remember to close
Four: Common methods
BYTE stream:
InputStream:
Read (int);
Read (byte);
Reset ();
Mark ();
Skip (long n);//The above three are only useful for certain subclasses
Marksupported ();
OutputStream (all Methods return void):
write (int);
Write (byte);
Flush (); The method in Bufferedoutputstream is used to flush memory and write the data in memory immediately.
Close ();
Character Stream:
BufferedReader:
Reading a single character
Read ();//reading a single character
ReadLine ();//reads a line of text, returns a string, and returns null for countless data
BufferedWriter:
Write ();
Flush ();
NewLine (); Write a line delimiter
Five: Code
One: Byte stream
1. Byte stream read, write, copy
Where a string is used to convert a byte array to another:
GetBytes (): Converts a string into a byte array byte[]:getbytes ("Utf-8");
//FileInputStream ImportJava.io.FileInputstream;Importjava.io.IOException;Importjava.io.FileNotFoundException; Public classreadbytestream{ Public Static voidMain (String args[]) {//the file class opens the data to be read intoFileInputStream FIS =NewFileInputStream ("Text.txt");//use a relative path//Call the Read method to stream the bytes into the byte array and first create the byte array:Byte input =NewByte[20]; Fis.read (input); //converted to a string because the content being read is textString inputstring =NewString (input);//You can specify the encoding method laterSystem.out.println ("InputString"); }}
//FileOutputStreamImportJava.io.FileOutputstream;Importjava.io.IOException;Importjava.io.FileNotFoundException; Public classwritebytestream{ Public Static voidMain (String args[]) {//Specify the folder to write outFileOutputStream fos =NewFileOutputStream ("Text.txt"); //Call the Write method, first the data to be written
String outputstring = "Lalala 123";
You can get an array of bytes directly from the current string
byte output[] = Outputstring.getbytes ("UTF-8");// call GetBytes () method
fos.write (output); } }
//copy of Byte stream//Open the file you want to copyFileInputStream FIS =NewFileInputStream ("1. txt "); FileOutputStream Fos=NewFileOutputStream ("Copy1.txt");//Calling Methods//To define a byte array: To avoid an array being too small, the copy is incomplete and needs to be read by loop//The Read method returns a value of data that has been read from the file, or 1 to indicate that the end of the file has been readbyteInput[] =NewByte (50); while(Fis.read (input)! =-1) {fos.write (input);}//Remember to closefis.close (); Fos.close (); System.out.println ("ok!");
2. Read the buffered byte stream, write it out (high efficiency when the file size is large)
//1. Use a byte stream with buffer to read in, copyFileInputStream fis=NewFileInputStream ("Movio.mp4);//2. Get input bytes from this file//3. Create a buffered input byte stream, the incoming parameter is the input stream, which is the current FileInputStreamBufferedinputstream bis =NewBufferedinputstream (FIS);//12. Parameters can also be passed in: Buffer size //8. Make a copy of the file:FileOutputStream fos =NewFileOutputStream ("Newmovio.mp4"); Bufferedoutputstream BOS =NewBufferedoutputstream (FOS);//9. Band up the buffer//4. Call the Read method, first create a character array, the data in the file to read into this arraybyteInput[] =New byte(100);intCount = 0;//7. Define integer types to count how many bytes are read while(Bis.read (input)! =-1) {5.//still use loops to avoid reading incompleteBos.write (input);//10. Write out the byte streamcount++;}//6. When closing the file: Turn it off firstbis.close (); Fis.close (); Bos.close ();//11. Closing is also the samefos.close (); Sysytem.out.println ("Total read:" +count + "times! ");
2.1 Buffer to do optimization:///////////////////////////////////
Two: Character stream
1. Reading and writing data using a character stream
VI: Controversy
Byte stream in the operation of the time itself is not used in the buffer (memory), and the file itself is directly manipulated, and the character stream is used in the operation of the buffer
Byte stream when manipulating a file, the file can output even if the resource is not closed (the Close method), but if the character stream does not use the Close method, nothing is output, the character stream is buffered, and the flush method can be used to force the flush buffer. Then you can output the content without close.
Java:io Summary article