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

The FileInputStream class creates an InputStream that you can use to read bytes from a file.

For this you need to create a text file called Hello.txt with some text. We will read from this file and display it in console.

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

FileInputStreamDemo1.java

Check out the while loop used for looping the File

import java.io.*;

class FileInputStreamDemo1
{
	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 i = 0;
			while(i!=-1)
			{
				i = fis.read();
				if(i!=-1)
				System.out.print((char)i);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

Snapshot of the o/p- Same as this program

https://programmingpalace.wordpress.com/2011/08/27/java-program-demonstrating-the-usage-of-fileinputstream-in-java-io-package/         

Advertisements

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