-----------------------------------------------------------------------------------
FileInputStream
class declaration: PublicClass FileInputStream extends InputStream
Located under the Java.io package
The official notes to it:
A fileinputstream obtains input bytes from a file in a file system. What files is available depends on the host environment.
(Simple translation: FileInputStream gets the input bytes from a file in the file system.)
Construction Method :
FileInputStream (file file);
Creates a FileInputStream instance by opening a connection to the actual file, which is specified by the file object in the filesystem.
FileInputStream (String name);
Create an FileInputStream instance by opening a connection to the actual file, which is specified by the pathname name in the file system.
FileInputStream (FileDescriptor fdobj);
Create an FileInputStream instance by using the file descriptor fdobj.
Main methods:
-int available (): Returns the number of bytes that can be read in the input stream of a byte file
-void Close (): Closes this file input stream and frees the system resources associated with the stream.
-protected void Finalize (): Make sure to call its close () method when the file input stream is no longer referenced.
-int read (): reads a byte of data from the file input stream
-int read (byte[] b): reads up to b.length bytes of data from a file input stream into byte array b
-int read (byte[] b,int off,int len): Reads a maximum of Len bytes of data from the file input stream into byte array b
-long Skip (long n ): Skips and discards n bytes of data from the file input stream
View Source code :
FileInputStream the role of this class is to read the contents of the file into the program, the most critical of which is the three read method, the source code is called by the native method to achieve.
1 Public native intRead ()throwsIOException;2 Public intReadbyteB[])throwsIOException {3 returnReadbytes (b, 0, b.length);4 }5 Public intReadbyteB[],intOffintLenthrowsIOException {6 returnReadbytes (b, off, Len);7 }8 Private native intReadbytes (byteB[],intOffintLenthrowsIOException;9 Public native LongSkipLongNthrowsIOException;Ten Public native intAvailable ()throwsIOException;
View Code
--------------------------------------------------------------------------------------------------------------- ---
FileOutputStream
class declaration: PublicClass FileOutputStream extends OutputStream
Located under the Java.io package
The official notes to it:
A file output stream is the output stream for writing data to a file or to a filedescriptor.
(Simple translation: The FileOutputStream file output churn is used to write data to the output stream of file or FileDescriptor.)
Construction Method:
FileOutputStream (file file);
Creates a file output stream that writes data to the file that is represented by the specified Files object.
FileOutputStream (File file,boolean append);
Creates a file output stream that writes data to the file that is represented by the specified Files object.
FileOutputStream (String name);
Creates an output file stream that writes data to a file with the specified name.
FileOutputStream (String name,boolean append);
Creates an output file stream that writes data to a file with the specified name.
FileOutputStream (FileDescriptor fdobj);
Create an FileOutputStream instance by using the file descriptor fdobj.
Main methods:
-Void Close (): Closes this file output stream and frees the system resources associated with the stream.
-protected void Finalize (): Cleans up the link to the file to ensure that its close () method is called when the file output stream is no longer referenced.
-Void Write (byte[] b): writes B.length bytes from a specified byte array b to this file output stream.
-Void Write (byte[] b,int off,int len): Writes the specified byte array b to this file output stream from a Len byte starting at offset off.
-Void Write (int b): Writes the specified bytes to this file output stream
View Source code:
FileOutputStream the role of this class is to write the program's byte data into the specified file, the most critical of which is the three write method, the source code is called by the native method to achieve.
1 Private native voidWriteintBBooleanAppendthrowsIOException;2 Public voidWriteintbthrowsIOException {3 Write (b, append);4 }5 Private native voidWritebytes (byteB[],intOffintLenBooleanAppendthrowsIOException;6 Public voidWritebyteB[])throwsIOException {7Writebytes (b, 0, B.length, append);8 }9 Public voidWritebyteB[],intOffintLenthrowsIOException {Ten Writebytes (b, off, Len, append); One}
View Code
Examples of FileInputStream and FileOutputStream: Implementing File Replication.
Content in the FileIn.txt file:
Abcdefgheretrtrt
1 ImportJava.io.File;2 ImportJava.io.FileInputStream;3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 6 Public classFileinputoutputstreamdemo {7 8 Public Static voidMain (string[] args)throwsIOException {9 //target fileTenFile File =NewFile ("FileIn.txt"); One //creates an FileInputStream instance that is specified by the file object AFileInputStream FIS =Newfileinputstream (file); - - byte[] FileInput =New byte[1024]; the intCount = 0; - intbytes =Fis.read (); - //The loop reads the contents of the file FileText.txt into the byte array fileinput - while(Bytes! =-1){ +fileinput[count++] = (byte) bytes; -bytes =Fis.read (); + } A at //Output File -File FileOutput =NewFile ("FileOut.txt"); - if(!fileoutput.exists ()) { - fileoutput.createnewfile (); - } - //creating a file output stream object inFileOutputStream fos =NewFileOutputStream (fileOutput); - //outputs the contents of the byte array fileinput to the file FileOut.txt to Fos.write (fileInput); + } -}
Java byte stream: FileInputStream fileoutputstream