Byte file input and output streams

A File input stream of bytes

A basic steps using the input four streams:
(1) setting the input source stream.
(2) create the input stream to the source.
(3) allows the input data read from the source stream.
(4) closing the input stream.

II. Concrete realization

1 to step 2 implemented: You can use the following constitution methods:
FileInputStream (String name);
using the name given to create a stream FileInputStream file name.

FileInputStream (File file);
use the File object to create an input stream.

(The above two methods and parameters file name specified source file input stream is called, the role of the input stream is to open a channel of the file is reached)
is used as follows:

try{
 FileInputStream in = new FileInputStream("hello.txt"); 
}
catch(IOException e){
 System.out.println("File read error:"+e);
}

(Similar to the second construction method described above)

3. Let the input data stream read from the source:
implemented by using a read method:
int read ();
This method reads a single byte of data from the source, returns an integer between 0 to 225, if unread byte returns -1.

int read (byte b [])
; This method attempts to read from the source to b.length bytes byte array b, return the actual number of bytes read. If you reach the end of the file or -1.

int read (byte b [],
int off, int len); This method attempts to read len bytes to a byte array b, and returns the number of bytes actually read from the source. Off parameter specifies the data read from the storage start position of a byte array. If you reach the end of the file, -1 is returned.

4. Close stream
Close ();
This method is a method to close the stream.

Note: FileInputStream stream sequentially read the file, they do not close the stream, each time it calls the read method sequentially reads the contents of the source remaining until the end of the source or turned off.

II. Byte file output stream

A basic steps of the output stream:
1. given destination output stream.
2. Create a destination point to the output stream.
3. Let the output stream to write data to the destination.
4. Close the output stream.

II embodied
1. destination give an output stream: General File object or destination file specified String object.

2. Create a destination point output stream:
create a link to a file that we can use FileOutputStream class constructor has a refresh function output streams:
FileOutputStream (String name);
This method uses the given file name name is created FileOutputStream stream.

FileOutputStream (File file);
This method creates a FileOutputStream stream using the File object.

(The above two methods and parameters file name referred to a destination specified file output stream, the output stream of the action is to open a path of the file is reached)

3. Write byte stream using the output
method:
void Write (n-int);
the method writes a single byte to the destination.

void write (byte b [])
; the method writes a byte array to the destination.

void write (byte b [],
int off, int len); the method given byte array starting at offset off to take len bytes written to the destination.

void close ();
close the output stream.
Note: FileOutputStream stream sequentially written to the file, it does not close the stream, on each call the write method of sequentially writing to destination, know the stream is closed

Published 35 original articles · won praise 0 · Views 1292

Guess you like

Origin blog.csdn.net/c1776167012/article/details/103956606