FileInputStream - sample program in Java

By: Sam Chen  

The FileInputStream class creates an InputStream that you can use to read bytes from
a file. Its two most common constructors are shown here:

FileInputStream(String filepath)
FileInputStream(File fileObj)

Either can throw a FileNotFoundException. Here, filepath is the full path name of a file, and fileObj is a File object that describes the file. The following example creates two FileInputStreams that use the same disk file and each of the two constructors:

FileInputStream f0 = new FileInputStream("/autoexec.bat")
File f = new File("/autoexec.bat");
FileInputStream f1 = new FileInputStream(f);

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 reading. 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 bytes. It also illustrates how to use available( ) to determine the number of bytes remaining, and how to use the skip( ) method to skip over unwanted bytes. The program reads its own source file, which must be in the current directory.

// Demonstrate FileInputStream.
import java.io.*;
class FileInputStreamDemo {
public static void main(String args[]) throws Exception {
int size;
InputStream f =
new FileInputStream("FileInputStreamDemo.java");
System.out.println("Total Available Bytes: " +
(size = f.available()));
int n = size/40;
System.out.println("First " + n +
" bytes of the file one read() at a
time");
for (int i=0; i < n; i++) {
System.out.print((char) f.read());
}
System.out.println("\\nStill Available: " + f.available());
System.out.println("Reading the next " + n +
" with one read(b[])");
byte b[] = new byte[n];
if (f.read(b) != n) {
System.err.println("couldn't read " + n + " bytes.");
}
System.out.println(new String(b, 0, n));
System.out.println("\\nStill Available: " + (size = 
f.available()));
System.out.println("Skipping half of remaining bytes with
skip()");
f.skip(size/2);
System.out.println("Still Available: " + f.available());
System.out.println("Reading " + n/2 + " into the end of
array");
if (f.read(b, n/2, n/2) != n/2) {
System.err.println("couldn't read " + n/2 + " bytes.");
}
System.out.println(new String(b, 0, b.length));
System.out.println("\\nStill Available: " + f.available());
f.close();
}
}

Here is the output produced by this program:

Total Available Bytes: 1433
First 35 bytes of the file one read() at a time
// Demonstrate FileInputStream.
im
Still Available: 1398
Reading the next 35 with one read(b[])
port java.io.*;
class FileInputS
Still Available: 1363
Skipping half of remaining bytes with skip()
Still Available: 682
Reading 17 into the end of array
port java.io.*;
read(b) != n) {
S
Still Available: 665

This somewhat contrived example demonstrates how to read three ways, to skip input, and to inspect the amount of data available on a stream.

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".




Archived Comments

1. it is a line to line copy of the topic in the book complete reference to java by herbert schildt
View Tutorial          By: pavan at 2013-04-26 05:33:49

2. I Want a Simple program , Input two number & find the sum of them in Java language.
View Tutorial          By: Sandeep Kumar Roy at 2012-07-07 12:43:57

3. You can save a file from the content of the input. This is my method to do it.

publi

View Tutorial          By: anne at 2011-11-22 10:50:10

4. How can I delete the data that I have entered into a file
View Tutorial          By: selina at 2011-08-15 05:15:45

5. How to write data from keyboard to a file in java?
View Tutorial          By: Ashish Ranjan at 2011-07-13 01:04:38

6. thank you so much, i didn't know the avaible() method, it helped me a lot in my job
View Tutorial          By: michael rodas at 2010-12-27 13:20:36


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial