Stream class
Java's streaming input/output is based on four abstract classes: InputStream, OutputStream, Reader, Writer. They are used to create specific stream subclasses. Although the program performs input/output operations through specific subclasses, the top-level class defines the basic general functionality of all stream classes.
InputStream and OutputStream are designed for byte stream design, and reader and writer are designed for character stream, and the byte stream and character stream form separate hierarchies. In general, processing a character or string using a character stream class, processing a byte or binary object using a byte flow .
When you manipulate a file stream, whether it is a character stream or a byte stream, you can do this in the following ways:
1. Use the file class to find an object
2, by the object of the file class to instantiate the byte stream or the subclass of the character stream
3. Byte (character) read and write operations
4. Close file stream
OutputStream (byte output stream)
OutputStream is an abstract class that defines the Java streaming byte input pattern. All methods of the class return a void value and, in the case of an error, raise an abstract method provided by Ioexception,outputstream:
Method |
Role |
void Close () |
Closes the input stream, and the write operation after shutdown throws IOException |
Flush () |
Refreshes this input stream and forces all buffered output bytes to be written out |
Write (byte[] b) |
Writes a single byte to the input stream, noting that the parameter is of type int, which allows the designer to call the Write () method without having to convert the parameter to a byte type |
Write (byte[] b, int off, int len) |
Writes a byte in array b to the file as a starting point of B[off], Len bytes |
Write (int b) |
Write a complete byte array to an output stream |
FileOutputStream (file byte output stream)
Fileoutpustream should be the most common byte output stream in Java, which creates a class outputstream that can write bytes to a file, which is commonly constructed as follows:
1, FileOutputStream (String name)
2, FileOutputStream (file file)
3, FileOutputStream (file file, Boolean append)
The first two constructor methods are similar, the former input the absolute path of the file, the latter input the instance object, and the same as Randomaccessfile, recommend the latter. The third construction method is a bit different, append if set to True, the file is opened in search path mode. the creation of FileOutputStream does not depend on the existence of the file, and when the object is created, Fileoutputsstream creates it before opening the output file. In this case, if you try to open a read-only file, IOException is raised. FileOutputStream, write an example, now my D-disk path does not have "stream.txt" this file:
public static void Main (string[] args) throws exception{ File File = new file ("D:/stream.txt"); OutputStream out = new FileOutputStream (file); byte b0[] = "abcdefghijklmnopqrstuvwxyz". GetBytes (); Operation Byte Stream, to convert into bytes out.write (b0); Out.close ();}
At this point, open "Stream.txt":
See the "Stream.txt" under the D disk, and the contents of the file and we write the same, this example also proves that FileOutputStream does not depend on the existence of the file under the specified path. If the file is already in the specified path, then the write will be overwritten instead of appended , which is a good proof:
public static void Main (string[] args) throws exception{ File File = new file ("D:/stream.txt"); OutputStream out = new FileOutputStream (file); byte b0[] = "abcdefghijklmnopqrstuvwxyz". GetBytes (); Operation Byte Stream, to convert into bytes out.write (b0); Out.close ();}
At this point, open "Stream.txt" again:
The contents of "Stream.txt" have been changed, which proves the conclusion.
InputStream (byte input stream)
InputStream is an abstract class that defines the Java streaming byte input pattern. All of the methods in this class will throw an abstract method provided by Ioexception,inputstream when it goes wrong:
Party |
for with |
int available () |
returns the currently readable number of bytes |
void Close () |
closes this input stream and frees all system resources associated with the stream, which will generate the IoE when it is closed Xception |
int mark (int readlimit) |
places a tag in the input stream that remains valid until n bytes bytes are read |
Td>boolean marksupported ()
if the called Stream supports mark ()/reset () returns True |
int read () |
if the next byte can be Read returns an integer that returns 1 |
int Read (byte b[]) |
tries to read buffer.length bytes into buffer and returns the number of bytes actually successfully read, encountering a file Tail return-1 |
int Read (byte b[], int off, int len) |
reads the maximum number of Len arrays in the input stream directly into a byte array, off indicates the initial offset of the write data in array b 。 Note that three read methods will block until the input data is available, the end of the stream is detected, or an exception is thrown |
void Reset () |
Reset the input pointer to the previous setting The tag of |
long Skip (long N) |
Skip and Discard n bytes of data in this input stream |
FileInputStream (file byte input stream)
FileInputStream should be the most common byte input stream in Java, which creates a InputStream class that reads bytes from a file, and its two common construction methods are as follows:
1, FileInputStream (String name)
2, FileInputStream (file file)
Similar to FileOutputStream, the use of the latter is recommended. FileInputStream, also writes an example, operates the "Stream.txt" generated under the D disk above:
public static void Main (string[] args) throws exception{ File File = new file ("D:/stream.txt"); InputStream in = new FileInputStream (file);
byte b1[] = new byte[(int) file.length ()]; int i = 0; i = In.read (B1); System.out.println (i); System.out.println (New String (B1, 0, I));}
The result of the operation is:
20Hello World!!!
To differentiate clearly, OutputStream's role is to output content from Java memory to a file, and InputStream to input content from a file into Java memory. The read (byte b[]) method was previously understood to mean "an attempt to read buffer.length bytes into buffer and return the actual number of bytes read", returning the actual byte size. Don't be mistaken for "Hello world!!!" is 14 characters or 28 bytes, the byte stream bottom is in bytes, so there is only 14 bytes in the file, as for the return is 20, or because of the "byte alignment" problem.
There is no demo skip method, because it is relatively simple, skip how much more than a few bytes read less, skip (3), then read out is "LO world!!!", you can try it yourself.
Java IO3: Byte stream