Java program demonstrating the usage of FileInputStream in java.io package

The FileInputStream class creates an InputStream that you can use to read bytes from a file.
Here we will be reading from a file called Hello.txt

It contains the following text

===

Hello World from Java.

Java is a high level programming language used throughout the
world.

Java is platform independent with a rich API.

Hope you will enjoy being a part of Java.

===

Create Text File Hello.txt  and keep it in the same directory as your program

//Java Program demonstrating the FileInputStream Class in java.io package

FileInputStreamDemo.java


import java.io.*;

class FileInputStreamDemo
{
	public static void main(String[] args)
	{
		try{
			File f = new File("Hello.txt");
			int size;
			FileInputStream fis = new FileInputStream(f);

			System.out.println("Total Available Bytes: " + (size = fis.available()));
			int n = size = fis.available();

			int i = 0;

			for(i=0; i<n;i++)
			{
				System.out.print((char)fis.read());
			}
		}//try
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

Snapshot of the o/p

When we run the program, the contents of the file are displayed on the console.

Advertisements

One thought on “Java program demonstrating the usage of FileInputStream in java.io package

  1. Pingback: Java program demonstrating the usage of FileInputStream in java.io package-Different Way | Programming Palace

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s