Skip to main content

3 Ways to Read a File using FileInputStream in Java IO

Reading a file in Java IO (streams) with FileInputStream is much easier than what you think of. Here are 3 effective ways you can use to achieve it.The sample tutorial decides and simplifies doing the task.


import java.io.*;
class ReadAndPrintFile
{
public static void main(String args[]) throws Exception
{
//---------------------------First way-------------------------//
FileInputStream fin=new FileInputStream("ReadAndPrintFile.java");
// Create a byte array
byte[] b=new byte[fin.available()];
// Read data into the array
fin.read(b);
for(int i=0;i<b.length;i++)
{
System.out.print((char)b[i]);
}
//---------------------------Second way-------------------------//
int k=0;
FileInputStream fin1=new FileInputStream("ReadAndPrintFile.java");
System.out.println("\n\nSecond way\n");
// Read till the end of file
while((k=fin1.read())!=-1)
{
System.out.print((char)k);
}
//---------------------------Third way-------------------------//
FileInputStream fin2=new FileInputStream("ReadAndPrintFile.java");
byte b1[]=new byte[fin2.available()];
fin2.read(b1,0,b1.length);
System.out.println("\n\nThird way\n");
for(int i=0;i<b1.length;i++)
{
System.out.print((char)b1[i]);
}

// Close the document
fin.close();
fin1.close();
fin2.close();
}
}
fin.read(b): Dump the entire data that is read into the byte array which is stored in RAM.
(k=fin.read())!=-1: Reading each byte from the file starting from the index '0' till the end of the file. If  end of the file is reached the fin.read() will return -1.
fin.read(b1,0,b1.length): The first parameter is the byte array in which the data should be dumped into, the second parameter is the start-index of the file, that is from which the dumping should start, the last parameter is the index. Here i'm saying to dump till the end of the file i.e. the entire file is dumped as i am specifying '0' as start and 'b1.length' as last index which is equal to fin.available().

-------------------------------------
Output
-------------------------------------

import java.io.*;
class ReadAndPrintFile
{
        public static void main(String args[]) throws Exception
        {

//---------------------------First way-------------------------//
        FileInputStream fin=new FileInputStream("ReadAndPrintFile.java");

        // Create a byte array
        byte[] b=new byte[fin.available()];

        // Read data into the array
        fin.read(b);

                for(int i=0;i<b.length;i++)
                {
                System.out.print((char)b[i]);
                }

//---------------------------Second way-------------------------//

        int k=0;

        FileInputStream fin1=new FileInputStream("ReadAndPrintFile.java");
        System.out.println("\n\nSecond way\n");
                // Read till the end of file
                while((k=fin1.read())!=-1)
                {
                        System.out.print((char)k);
                }

//---------------------------Third way-------------------------//

        FileInputStream fin2=new FileInputStream("ReadAndPrintFile.java");
        byte b1[]=new byte[fin2.available()];
        fin2.read(b1,0,b1.length);

        System.out.println("\n\nThird way\n");
                for(int i=0;i<b1.length;i++)
                {
                System.out.print((char)b1[i]);
                }


        // Close the document
        fin.close();
        fin1.close();
        fin2.close();
        }
}

Second way

import java.io.*;
class ReadAndPrintFile
{
        public static void main(String args[]) throws Exception
        {

//---------------------------First way-------------------------//
        FileInputStream fin=new FileInputStream("ReadAndPrintFile.java");

        // Create a byte array
        byte[] b=new byte[fin.available()];

        // Read data into the array
        fin.read(b);

                for(int i=0;i<b.length;i++)
                {
                System.out.print((char)b[i]);
                }

//---------------------------Second way-------------------------//

        int k=0;

        FileInputStream fin1=new FileInputStream("ReadAndPrintFile.java");
        System.out.println("\n\nSecond way\n");
                // Read till the end of file
                while((k=fin1.read())!=-1)
                {
                        System.out.print((char)k);
                }

//---------------------------Third way-------------------------//

        FileInputStream fin2=new FileInputStream("ReadAndPrintFile.java");
        byte b1[]=new byte[fin2.available()];
        fin2.read(b1,0,b1.length);

        System.out.println("\n\nThird way\n");
                for(int i=0;i<b1.length;i++)
                {
                System.out.print((char)b1[i]);
                }


        // Close the document
        fin.close();
        fin1.close();
        fin2.close();
        }
}

Third way

import java.io.*;
class ReadAndPrintFile
{
        public static void main(String args[]) throws Exception
        {

//---------------------------First way-------------------------//
        FileInputStream fin=new FileInputStream("ReadAndPrintFile.java");

        // Create a byte array
        byte[] b=new byte[fin.available()];

        // Read data into the array
        fin.read(b);

                for(int i=0;i<b.length;i++)
                {
                System.out.print((char)b[i]);
                }

//---------------------------Second way-------------------------//

        int k=0;

        FileInputStream fin1=new FileInputStream("ReadAndPrintFile.java");
        System.out.println("\n\nSecond way\n");
                // Read till the end of file
                while((k=fin1.read())!=-1)
                {
                        System.out.print((char)k);
                }

//---------------------------Third way-------------------------//

        FileInputStream fin2=new FileInputStream("ReadAndPrintFile.java");
        byte b1[]=new byte[fin2.available()];
        fin2.read(b1,0,b1.length);

        System.out.println("\n\nThird way\n");
                for(int i=0;i<b1.length;i++)
                {
                System.out.print((char)b1[i]);
                }


        // Close the document
        fin.close();
        fin1.close();
        fin2.close();
        }
}

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.