简体   繁体   English

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

[英]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. 这是部分代码版本,因为真正的代码还有4个if / else if语句。

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: 它从input2中正在读入/再读出的文件中产生几个结果,但随后停止,不再产生结果并给出错误:

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) 线程“主”中的异常java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)处的java.io.DataInputStream.readUTF(DataInputStream.java:572)处的java.io.DataInputStream.readUTF(位于YDSearch.main(YDSearch.java:93)的YDSearch.listFiles(YDSearch.java:85)的DataInputStream.java:547)

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. 我也尝试过将变量'b'放在if语句中,但是由于作用域而无法正常工作。 If b was defined by if's then there would only need to be one if statement to output to the file 如果b由if定义,那么只需要一个if语句输出到文件

Please let me know if you've got any ideas, I'd really appreciate it :) 如果您有任何想法,请告诉我,我真的很感激:)

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. 如果您有任何文件名与您尝试阅读的模式不符,那么您将获得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. 更详细地说,问题是你得到一个以“mp3”,“mp4”,“3gp”,“mov”,“avi或”wmv“结尾的所有文件的列表。然后你处理那个列表,然后写为每个“mp3”和“mp4”文件输入C:/Users/Jonathan/Desktop/ReadyForAnalysis.dat 。然后假设对于文件列表中的每个条目,您将在ReadyForAnalysis.dat有一个条目。但是,如果有任何以“ 3gp”,“ mov”,“ avi”或“ wmv”结尾的文件,则将不成立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM