Class FileInputStream extends Inputstream implements closeable
function: Used to get input bytes from a file in a file system, processing binary raw byte files, such as EXE images. 1. Construct the associated file can be described using a String, or you can use file, the FileDescriptor object represents 2, read is likely to block the current thread. Blocking occurs when other operations such as read have more bytes than the number of bytes that this object is directly available (available). 3. Read to the end of the file return-1The constructor fileinputstream (file file) creates a FileInputStream by opening a connection to the actual file, which is specified by a file object in the filesystem. FileInputStream (FileDescriptor fdobj) creates a fileinputstream by using the file descriptor Fdobj, which represents an existing connection to an actual file in the file system. FileInputStream (String name) creates a fileinputstream by opening a connection to the actual file, which is specified by the pathname name in the file system. APIintavailable () returns the estimated number of remaining bytes that the next method invoked on this input stream can be read (or skipped) from this input stream in a blocked manner. If 5 is returned, then call Read (b,0, 5) is not blocking threads because data that is explicitly 5 bytes is available directly. voidClose () closes this file input stream and frees all system resources related to this stream. intread () reads a byte of data from this input stream. intReadbyte[] b) read a maximum of b.length bytes of data from this input stream into abytethe array. intReadbyte[] B,intOffintLen) reads a maximum of Len bytes of data from this input stream into thebytein array B. Off means that the first byte of data is stored in B[off]. The second one is stored in the B[off+1] ... len is the maximum number of bytes readLongSkipLongN) skips and discards n bytes of data from the input stream. protected voidFinalize () ensures that its Close method is called when the file input stream is no longer referenced. FileChannel Getchannel () returns the unique FileChannel object associated with this file input stream. FileDescriptor GETFD () returns a FileDescriptor object that represents a connection to the actual file in the file system, which is being used by this fileinputstream.
class fileoutputstream extends outputstream implements closeable, flushable
role: Used to write byte (binary) data into the associated file. 1. Files associated with the construct can use String to describe the path, or use file, Filedescripter class object. 2, Append is true, writes bytes to the end of the file, not to the beginning of the file. 3. If the file does not exist, the file will be created first. Represents an append, and append is true. When Append is false and the file already exists, it is the original content of this read-write overwrite file.
The constructor fileoutputstream (file file) creates a file output stream that writes data to a file that is represented by the specified file object. FileOutputStream (file file,Booleanappend) FileOutputStream (FileDescriptor fdobj) creates an output file stream that writes data to the specified file descriptor, which represents an existing connection to an actual file in the file system. FileOutputStream (String name) creates an output file stream that writes data to a file with the specified name. FileOutputStream (String name,Booleanappend) creates an output file stream that writes data to a file with the specified name.
APIvoidClose () closes this file output stream and frees all system resources related to this stream. void Flush ()
Refreshes the buffer of the output data in memory, forcing the data to be written out to the underlying file. When Close is executed, it is automatically flush. voidWritebyte[] b) b.length bytes from the specifiedbytethe array is written to this file in the output stream. voidWritebyte[] B,intOffintlen) will specifybyteThis file output stream is written to the Len byte of the array starting at offset off. Data flow: b----files associated with >fileoutputstreamvoidWriteintb) writes the specified bytes to this file output stream. protected voidFinalize () cleans up the connection to the file and ensures that the Close method of this stream is called when the file output stream is no longer referenced. FileChannel Getchannel () returns the unique FileChannel object associated with this file output stream. FileDescriptor GETFD () returns the file descriptor associated with this stream.
Here is an example of copying an EXE executable file via FileInputStream and FileOutputStream.
ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream; Public classmain{ Public Static voidMain (string[] args) {InputStream input=NULL ; OutputStream out=NULL; Try{input=NewFileInputStream (NewFile ("D:\\2.exe")); out=NewFileOutputStream (NewFile ("D:\\2_copy.exe"),false); byte[] Buff =New byte[1024]; intLen//saves read the number of bytes actually read. while( -1!= (len = input.read (buff, 0, Buff.length))) {out.write (buff,0, Len); } out.flush (); //forces the output stream to be flushed to write buffered data to the hard diskSystem.out.println ("Copy success!"); } Catch(FileNotFoundException e) {e.printstacktrace (); System.out.println ("File Open Failed"); } Catch(IOException e) {e.printstacktrace (); System.out.println ("File read and write Failed"); } finally { if(NULL!=Out ) { Try{out.close (); out=NULL; } Catch(IOException e) {System.out.println ("Output file shutdown failed"); } } if(input!=NULL) { Try{input.close (); Input=NULL; } Catch(IOException e) {System.out.println ("Input file shutdown failed"); } } } }}
"Java IO" FileInputStream and FileOutputStream