多个'if'语句到一个outputStream中

时间:2011-01-29 10:33:16

标签: java file-io if-statement compiler-errors

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");
  }
}

嘿那里,对上面的编码有一点问题,并希望有人可以提供帮助。这是代码的部分版本,因为真正的代码还有4个if / else if语句。

程序编译并开始正常运行。它会从正在读入/然后在input2中读出的文件返回多个结果,但随后停止,不再产生结果并给出错误:

  

线程“main”java.io.EOFException中的异常       at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)       在java.io.DataInputStream.readUTF(DataInputStream.java:572)       在java.io.DataInputStream.readUTF(DataInputStream.java:547)       在YDSearch.listFiles(YDSearch.java:85)       在YDSearch.main(YDSearch.java:93)

有人知道为什么会发生这种情况并找到他们可以分享的解决方案吗? 我也尝试使变量'b'在if语句中,但由于范围不适用。如果b由if定义,则只需要一个if语句输出到文件

如果您有任何想法,请告诉我,我真的很感激:)

1 个答案:

答案 0 :(得分:2)

据我所知,您并不总是为每个名称输出一个输出记录,仅适用于名称与您的某个模式匹配的情况。但是,您确实尝试读取每个名称的输入记录。

如果您有任何文件名与您尝试阅读的模式不符,那么您将获得EOF。

编辑:

更详细地说,问题是你得到一个以“mp3”,“mp4”,“3gp”,“mov”,“avi或”wmv“结尾的所有文件的列表。然后你处理那个列表,并为每个“mp3”和“mp4”文件写出C:/Users/Jonathan/Desktop/ReadyForAnalysis.dat的内容。然后假设对于文件列表中的每个条目,您将在ReadyForAnalysis.dat中有一个条目。但是,如果有任何文件以“3gp”,“mov”,“avi或”wmv“结尾,那么这将不成立。