The while loop in your Concatenate class is wrong.
It should be:
Code:
while ((c = s.read()) != -1){
bw.write(c);
System.out.write(c);
}
System.out.flush();
s.close();
bw.close();
You were writing c to your file stream after it already goes through the sequence of streams. So it writes the value of -1 into the stream (it came out as a question mark in the text file). Also, if you are going to write bytes directly to the out stream, then you need to flush the stream (unless the byte is a newline character, ASCII value of 10, then it automatically flushes). You could also simply use System.out.print or .println. Anyways, there ya go, should work fine now. One more thing to think about. It is probably better if in your ListOfFiles class you have a private array of input streams rather then Strings. In the constructor pass it an array of strings and convert them to FileInputStreams storing them inside the InputStream array. Java by default uses polymorphism, so there is no problem doing this. This way would make your nextElement function a lot cleaner. Like this:
Code:
public class ListOfFiles implements Enumeration {
private InputStream[] listOfStreams;
private int current = 0;
public ListOfFiles(String[] listOfFiles) {
this.listOfStreams = new InputStream[listOfFiles.length];
for(int i=0; i<listOfFiles.length; i++){
try {
listOfStreams[i] = new FileInputStream(listOfFiles[i]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean hasMoreElements() {
if (current < listOfStreams.length)
return true;
else
return false;
}
public InputStream nextElement(){
InputStream temp;
if(hasMoreElements()){
temp = listOfStreams[current];
current++;
return(temp);
}
else{
throw new NoSuchElementException("No more files.");
}
}
}
Either way the program works, and i guess it just comes down to personal preference.
Bookmarks