FileInputStream
In this chapter you will learn:
- How to Java FileInputStream
- Read a file with FileInputStream
- How to read a single byte from FileInputStream
- How to Skip n bytes
FileInputStream class
java.lang.Object
|
+-java.io.InputStream
|
+-java.io.FileInputStream
The FileInputStream
class creates an InputStream
that
you can use to read bytes from a file.
Its two most common constructors:
FileInputStream(String filepath)
filepath
is the full path name of a fileFileInputStream(File fileObj)
fileObj
is a File object that describes the file.
Either can throw a FileNotFoundException
.
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);
Read a file with FileInputStream
FileInputStream
overrides six of the
methods in the abstract class InputStream
.
We can use those methods to read file content.
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
// j a v a 2s. co m
public class Main {
public static void main(String args[]) throws IOException {
InputStream f = new FileInputStream("FileInputStreamDemo.java");
int size = f.available();
System.out.println("Total Available Bytes: " + size);
int n = 40;
for (int i = 0; i < n; i++) {
System.out.print((char) f.read());
}
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));
f.skip(30);
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));
f.close();
}
}
Read a single byte
The following code reads one byte from the 'a.htm'.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//from ja va2s . c o m
public class Main {
public static void main(String[] args) throws FileNotFoundException {
FileInputStream file = null;
byte x = -1;
try {
file = new FileInputStream("a.htm");
x = (byte) file.read();
} catch (FileNotFoundException f) {
throw f;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (file != null) {
file.close();
}
} catch (IOException e) {
}
}
}
}
Skip n bytes
The following code does a skip n bytes while reading the file usingFileInputStream
.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//from j av a 2 s. com
public class Main {
public static void main(String[] args) {
File file = new File("C:/data.dat");
try {
FileInputStream fin = new FileInputStream(file);
int ch;
fin.skip(10);
while ((ch = fin.read()) != -1){
System.out.print((char) ch);
}
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the file " + ioe);
}
}
}
Next chapter...
What you will learn in the next chapter:
- How to use ObjectInputStream
- How to read data through ObjectOutputStream
- Object serialization and ObjectInputStream
Home » Java Tutorial » Reader Writer