The FileinputStream class creates an InputStream that you can use to read bytes from a file. Its two most common constructors are shown here
Either can throw a FileNotfoundexception. Here,ji/cpatt Is the full path name of a file Is a File object that describes the file.
The following example creates two Files input Streams that use the same disk file and each of the two constructors
Although the first constructor is probably more commonly used, the second allows us to closely examine" the file using the File methods, before we attach it to an input stream. When a fileinputStream is created, it is also opened for rending.
FileInputStream overrides six of the methods in the abstract class InputStream. The mark( ) and reset ) methods are not overridden, and any attempt to use reset( ) on a FileInputStream will generate an IOException.
The next example shows how to read a single byte, an array of bytes, and a subrange array.of b~es. It also illustrates how to use available () to determine the number of bytes remaining, and how to use the script ) method to skip over unwanted bytes. The program rends its owii source file, which must be in the current directory.
This somewhat contrived example demonstrates how to read three ways, to skip input, and to inspect the amount of data available on a stream.
FileOutputStream creates an OutputStream that you can use to write bytes to a file. Its
most commonly used constructors are shown here:
FileOutplItStream(StringjilePnffl,. boolean nppel/d)
They can throw an IOException or a SecurityException. Here,filePath is the full path name of a file, and file9bj is a File object that describes tho file. !f append is true, the file is opened in append mode.
Creation of FileOutputStream is not dependent on the file already 'existing. FileOutputStream. will create the file before opening it for output when you create' the object. In the case where you attempt to open a read-only file, an IOException will be thrown.
The following example creates a sample buffer of bytes by first making a String . and then using the gct Bytes() method to extract the byte array equivalent, If then creates three files. The first, file text, will contain every other byte from the sample. The second, file2.txt, will contain the entire set of bytes: The third and last, file3.txt, will contain only the last quarter. Unlike the Fileinputstream methods-all of the, FileOutputStream methods have a return typo of void. In the case of an errol', these methods will throw an IOException.