FileInputStream example in Java to read a file

import java.io.IOException;
import java.io.FileInputStream;

public class FileInputStreamExample {
	public static void main(String[] args) throws IOException {

		FileInputStream fileInputStream = null;
		int content;

		try {
		  // Creates a new FileInputStream by opening a connection to an actual file
		  fileInputStream = new FileInputStream("C://readMe.txt");

		  System.out.println("File size(Byte to read):"+fileInputStream.available()+"byte");
		  
		  // read until it reach end of the file
		  while ((content = fileInputStream.read()) != -1) {
			// convert from int to char and print it
			System.out.print((char) content);
		  }
		  
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			// releases all system resources from the streams
			if (fileInputStream != null)
				fileInputStream.close();
		}
	}
}
Output
File size(Byte to read): 53 byte Hello, you are reading file content. Before EOF..










Related Examples


Write a program to check if an input String is Palindrome or not?
Calculate the factorial of a given number using Java?
How to reverse a number in Java?
Write a program to convert from decimal to binary?
Producer Consumer Example in Thread
Write a program to reverse a String?








Receive Latest Java Examples in Your Email: