There are many other streams in the java.io package, mainly to improve performance and ease of use. C + + can only provide byte streams. There are two kinds of streams in Java, one is the byte stream, the other is a stream of characters, which is represented by four abstract classes (each stream includes input and output, so altogether four): Inputstream,outputstream,reader,writer. Other streams of varying variations in Java are derived from them.
character streams and byte streams are differentiated according to the processing of data. BYTE stream in accordance with 8-bit transmission, byte throttling is the most basic, all files are stored in byte (byte) of storage, on the disk is not the character of the file, but first encoded characters into bytes, and then store these bytes to disk.
1. The byte stream can be used for any type of object, including binary objects, while the character streams can only handle characters or strings;
2. The byte stream provides the ability to handle any type of IO operation, but it cannot handle Unicode characters directly, and the character streams can.
When reading text, use character streams, such as TXT file. When you read a text file, you use a byte stream, such as MP3. Theoretically any file can be read by a byte stream, but when reading the text data, in order to be able to revert to the text you have to go through a conversion process, relative to the character streams to save the trouble, you can have a way to read directly.
A character stream is a 2-byte Unicode character that operates on characters, character arrays, or strings, and the Byte throttle Processing unit is 1 bytes, manipulating bytes and byte arrays. So the character stream is made by a Java virtual machine that converts bytes into 2-byte Unicode characters, so it is more supportive of multiple languages.
1. Byte stream: inherited from Inputstream/outputstream.
OutputStream provides the following methods:
void write (int b): Writes a byte of data
void Write (byte[] buffer): Writes the data of the array buffer to the stream
void Write (byte[] buffer,int offset,int len): Starting from Buffer[offset, writing len bytes of data
void Flush (): Forces the data in the buffer to be written to the stream
void Close (): Off stream
InputStream provides the following methods:
int read (): reads a byte of data, if reached the end of the file, the return value is-1
int read (byte[] buffer): reads the buffer size data, the return value is actually reads the number of bytes
int read (byte[] buffer,int offset,int len)
int available (): Returns the number of bytes available in the stream for reading
Long Skip (long N): Skips n bytes of data, returns the actual number of data skipped
void Close (): Off stream
2. Character Stream, inherited from Inputstreamreader/outputstreamwriter.
A class of character streams: 1, BufferedReader is a filter (extends FilterReader). Filter
The device is used to process and then output the data of the stream. The constructor functions are:
BufferedReader (Reader in): Generates a buffered character input stream, in a reader
BufferedReader (Reader in,int size): Generates a buffered character input stream and specifies the size of the buffer
public class Iostreamdemo {public void samples () throws IOException {//1. This is a line of data that is read from the keyboard and returns a string Buffere
Dreader stdin =new BufferedReader (New InputStreamReader (system.in)); System.out.print ("Enter a Line:"); System.out.println (Stdin.readline ());//2. This is a line-by-row reading of data from a file BufferedReader in = new BufferedReader (New FileReader ("Iostreamdemo.java")); String s, s2 = new
String (); while ((s = in.readline ())!= null) s2 + + + "n"; In.close (); 3. This is read from one string to the byte StringReader in1 = new StringReader (s2); int c; while ((c = In1.read ())! =-1) System.out.print ((char) c);//4.
This is to write a string to the file try {bufferedreader in2 = new BufferedReader (new StringReader (S2));
PrintWriter out1 = new PrintWriter (New BufferedWriter ("FileWriter"));
int linecount = 1; while ((s = in2.readline ())!= null) out1.println (linecount++ + ":" + s); Out1.close ();} catch (Eofexception e) {System.err.println ("En D of Stream ");}}}
For the above example, the following points need to be explained:
1. InputStreamReader is a bridge between InputStream and reader, because System.in is a byte stream, it needs to be packaged and then transformed into a character streams supply bufferedreader use.
3. PrintWriter out1 = new PrintWriter (new BufferedWriter) (New FileWriter ("Iodemo.out"));
This statement embodies a Java input and output system of a feature, in order to achieve a certain purpose, need to wrap several layers. First of all Output destination is file Iodemo.out, so the most inner packaging is filewriter, create an output file stream, Next, we hope that this stream is buffered, so use bufferedwriter to wrap it to achieve the goal, finally, we need to format the output results, so we will PrintWriter Wrap in the outermost layer.
Another important use of Java streams is the use of object streams to serialize objects.
When a program is running, the variable data is stored in memory, once the program is finished, the data will not be saved, one solution is to write the data to the file, and Java provides a mechanism that can write objects in the program to the file, and then read the object from the file to re-establish. This is called object serialization. It was introduced in Java primarily for RMI (Remote method invocation) and Java beans, but it is also a useful technique in peacetime applications.