Q:

Multiple 'if' statements into one outputStream

import java.io.*;
import java.io.File;
import java.io.FilenameFilter;

public class YDSearch{
    public void listFiles(String dir) throws IOException{

    File directory = new File(dir);
    if (!directory.isDirectory()) {
      System.out.println("No directory provided");
      return;
    }
    //create a FilenameFilter and override its accept-method

    FilenameFilter filefilter = new FilenameFilter() {

      public boolean accept(File dir, String name) {
        //if the file extension is .mp3 return true, else false
        return name.endsWith(".mp3")||name.endsWith(".mp4")||name.endsWith(".3gp")
            ||name.endsWith(".mov")||name.endsWith(".avi")||name.endsWith(".wmv");
      }
    };
    String[] filenames = directory.list(filefilter);

    DataOutputStream output = new DataOutputStream(new FileOutputStream("C:/Users/Jonathan/Desktop/YouDetect/SearchByFileType/AllMediaFiles.dat"));

    for (String name : filenames) {
      output.writeUTF(dir + name);
    }
    output.close();

    DataInputStream input = new DataInputStream(new FileInputStream("C:/Users/Jonathan/Desktop/YouDetect/SearchByFileType/AllMediaFiles.dat"));
    DataOutputStream output2 = new DataOutputStream(new FileOutputStream("C:/Users/Jonathan/Desktop/ReadyForAnalysis.dat"));
    for (String name : filenames) {
    FileInputStream in = new FileInputStream(input.readUTF());
        int byteCounter = 0;
        int rowCounter = 0;
        long bufferCounter = 0;
        if(name.endsWith(".mp3")){
            byte[] b = new byte[36];
            int read = in.read(b, 0, 36);
            if (byteCounter != 1000){
                if (rowCounter == 1){
                    System.out.println("\n");
                    rowCounter = 0;
                }
                output2.writeUTF(org.apache.commons.codec.binary.Hex.encodeHexString(b)+ " " + dir + name);
                bufferCounter ++;
                rowCounter ++;
            }else{
                    byteCounter = 0;
                    try{
                        Thread.sleep(200);
                    }catch(InterruptedException e) {
                    }
            }
        }
        else if(name.endsWith(".mp4")){
            byte[] b = new byte[29];
            int read = in.read(b, 0, 29);

            if (byteCounter != 1000){
                if (rowCounter == 1){
                    System.out.println("\n");
                    rowCounter = 0;
                }
                output2.writeUTF(org.apache.commons.codec.binary.Hex.encodeHexString(b)+ " " + dir + name);
                bufferCounter ++;
                rowCounter ++;
            }else{
                    byteCounter = 0;
                    try{
                        Thread.sleep(200);
                    }catch(InterruptedException e) {
                    }
            }
        }
        //System.out.println("====================");
    }
    output2.close();
    input.close();

    DataInputStream input2 = new DataInputStream(new FileInputStream("C:/Users/Jonathan/Desktop/ReadyForAnalysis.dat"));
    for (String name : filenames) {
    System.out.println(input2.readUTF()+"\n");
    }
}
  public void checkHeaderSC(String allFiles)throws IOException{
  }

  public static void main(String[] args) throws IOException {
    YDSearch YDSearch = new YDSearch();
    YDSearch.listFiles("C:/Users/Jonathan/Desktop/YD Tests/1) High Quality/");
    YDSearch.listFiles("C:/Users/Jonathan/Desktop/YD Tests/2) Medium Quality/");
    YDSearch.listFiles("C:/Users/Jonathan/Desktop/YD Tests/3) Low Quality/");
    YDSearch.checkHeaderSC("C:/Users/Jonathan/Desktop/YouDetect/SearchByFileType/ReadyForAnalysis.dat");
  }
}

Hey there, having a little issue with the above coding and hoped someone here might be able to help. This is sort of a partial version of the code as the real one has 4 more if/else if statements involved.
The program compiles and begins to run fine. It produces several results back from the file that is being read into/then out of again in input2 but then stops, produces no more results and gives the error:

Exception in thread "main" java.io.EOFException
      at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
      at java.io.DataInputStream.readUTF(DataInputStream.java:572)
      at java.io.DataInputStream.readUTF(DataInputStream.java:547)
      at YDSearch.listFiles(YDSearch.java:85)
      at YDSearch.main(YDSearch.java:93)

Anybody know why this might be happening and have a solution they could share?
I've also tried making the variable 'b' to be inside of an if statement but that doesn't work because of scope. If b was defined by if's  then there would only need to be one if statement to output to the file
Please let me know if you've got any ideas, I'd really appreciate it :)

A:

As far as I can see, you don't always put out an output record for every name, only for when the name matches one of your patterns. However, you do try to read an input record for every name.
Ergo, if you have any filenames that don't match the patterns you try to read more than you write, and you will get the EOF.
EDIT:
In more detail, the problem is that you get a list of all the files that end with "mp3", "mp4", "3gp", "mov", "avi or "wmv". You then process that list, and write out something into C:/Users/Jonathan/Desktop/ReadyForAnalysis.dat for each "mp3" and "mp4" file. You then assume that for each entry in your list of files, that you will have an entry in ReadyForAnalysis.dat. However, if there are any files ending in "3gp", "mov", "avi or "wmv" then this will not hold true.

