output is displaying all the files , but in the end it displaying a string of question marks(?????????????).what to do

This code shows the content of multiple files using vector object, extracting from this object using enumeration.

import java.util.*;
import java.io.*;

class IO14 {

    public static void main(String []args) throws IO Exception {

        FileInputStream fis= new FileInputStream("io1.java");
        FileInputStream fis1=new FileInputStream("Treesetdemo2.java");
        FileInputStream fis2=new FileInputStream("Treesetdemo4.java");
        FileInputStream fis3=new FileInputStream("Treesetdemo5.java");
        FileInputStream fis4=new FileInputStream("Treesetdemo8.java");
        FileInputStream fis5=new FileInputStream("Treesetdemo6.java");

        // vector object to hold all streams

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

        Enumeration e=v.elements();

        SequenceInputStream sis=new SequenceInputStream(e);
        int i=0;

        while((i=sis.read())!=1){
            System.out.print((char )i);
        }
        System.out.println("work is done");
        sis.close();
        fis.close();
        fis1.close();
        fis2.close();
        fis3.close();
        fis4.close();
        fis5.close();
    }
}
Jon Skeet
people
quotationmark

You seem to be expecting that InputStream.read() will return 1 when it's reached the end of its content - it won't, it'll return -1. So your while loop should look like this:

while((i=sis.read()) != -1)

... although you're still converting one byte at a time into a character, effectively treating each file as ISO-8859-1. It would be better to use an InputStreamReader and specify the encoding, assuming these really are text files.

Additionally, you should use try-with-resources blocks (or try/finally if you're using Java 6 or earlier) to close the streams even if an exception is thrown, and consider using List<FileInputStream> (e.g. with an ArrayList implementation) instead of the raw Vector type.

people

See more on this question at Stackoverflow