Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Monday, July 7, 2014

Java: How to read data from multiple files at one time.

Reading data from more than two files (sequentially) at a time can be a tedious job, but java makes it easy. Sun (now Oracle) has provided java.io package to deal with Streams. 

To read files sequentially we have SequenceInputStream class in java.

Constructor of SequenceInputStream class.


public SequenceInputStream(Enumeration<? extends InputStream> e)

public SequenceInputStream(InputStream s1,InputStream s2)

If you want to read data from two files you can use the second constructor of SequenceInputStream class, whereas if you want to read data from multiple files you can use first constructor.

First constructor takes Enumeration as argument. To do so, we can use Vector class.

Example of SequenceInputStream class using first constructor.


import java.io.*;
import java.util.*;
class SequenceReading
{
            public static void main(String[] args)
            {
                        try
                        {
                        FileInputStream fis1=new FileInputStream("File1.txt");
                        FileInputStream fis2=new FileInputStream("File2.txt");
                        FileInputStream fis3=new FileInputStream("File3.txt");
                        FileInputStream fis4=new FileInputStream("File4.txt");
                        FileOutputStream fos=new FileOutputStream("Resultant.txt");

                        Vector v=new Vector();
                        v.add(fis1);
                        v.add(fis2);
                        v.add(fis3);
                        v.add(fis4);

                        Enumeration en=v.elements();
                        SequenceInputStream sis=new SequenceInputStream(en);

                        int data;
                        while((data=sis.read())!=-1)
                        {
                                                fos.write(data);
                        }
                        }
                        catch (FileNotFoundException fnfe)
                        {

                                    System.out.println("One or more files not found "+fnfe);
                        }

                        catch(IOException ioe)
                        {

                                    System.out.println("IO exception occurs "+ioe);
                        }         
                       
            }
}

Example of SequenceInputStream class using second constructor.


import java.io.*;
import java.util.*;
class SequenceReading
{
            public static void main(String[] args)
            {
                        try
                        {
                        FileInputStream fis1=new FileInputStream("File1.txt");
                        FileInputStream fis2=new FileInputStream("File2.txt");                    
                        FileOutputStream fos=new FileOutputStream("Resultant.txt");      
                        SequenceInputStream sis=new SequenceInputStream(fis1,fis2);

                        int data;
                        while((data=sis.read())!=-1)
                        {
                                                fos.write(data);
                        }
                        }
                        catch (FileNotFoundException fnfe)
                        {

                                    System.out.println("One or more files not found "+fnfe);
                        }

                        catch(IOException ioe)
                        {

                                    System.out.println("Unexpected error occures");
                        }                                 
            }
}

Since FileInputStream, FileOutputStream classes throws checked exception, handling of these exceptions are necessary, otherwise compiler will throw an error saying Unreported exception must be caught or declared to be thrown.


You can copy paste the above code in your java editor, and run.

No comments: