Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我正在按日期筛选文件_Java_File_Date_Gzip - Fatal编程技术网

Java I';我正在按日期筛选文件

Java I';我正在按日期筛选文件,java,file,date,gzip,Java,File,Date,Gzip,我正在尝试按日期筛选文件并读取每个文件。我有一个find()方法,它读取每个文件名,并返回一个数组中以“B”开头的文件。第二个方法是filesort(),它将返回从find()方法发送的filename中的所有文件日期。在main方法中,我希望在给定的特定日期前读取文件。如果所有文件都有相同的日期,它将读取所有文件。但是,其中一个文件的日期不同,它将抛出错误 public static String[] find(String rootPath){ File root = new Fil

我正在尝试按日期筛选文件并读取每个文件。我有一个find()方法,它读取每个文件名,并返回一个数组中以“B”开头的文件。第二个方法是filesort(),它将返回从find()方法发送的filename中的所有文件日期。在main方法中,我希望在给定的特定日期前读取文件。如果所有文件都有相同的日期,它将读取所有文件。但是,其中一个文件的日期不同,它将抛出错误

public static String[] find(String rootPath){

   File root = new File(rootPath);
   // Filter files whose name start with "B"
   FilenameFilter beginswithm = new FilenameFilter() {
       public boolean accept(File directory, String filename) {
           return filename.startsWith("B");
       }
   };
   // array to store filtered file names
   String[] files = root.list(beginswithm);
   String[] no = { "nofile" };
   if (files == null) {
       return no;
   }  
   return files;
}

}

}

因为第一个文件是2019年4月16日,它将抛出不正确的日期。如果其中3个已于2019年4月17日发布,则将无需发布。
但现在我只想读取日期为2019年4月17日的文件

如果我们从另一个角度来看问题,那么实现您想要的似乎非常简单。我将给出基本逻辑

  • 从目录中读取文件名。这是一个怎样做的例子
  • 将名称存储在ArrayList中
  • 使用集合链接对ArrayList进行排序有助于您
  • 现在,您已经对目录的文件名进行了排序,只要访问ArrayList元素并使用它访问真实的文件即可

  • 愉快的编码,如果您在查找文件时仍有问题,请告诉我该文件的名称是从“B”开始的,并且包含特定日期,只需按照此过程操作即可

    您可以在类中使用此代码查找所有文件

    从该文件列表中,您可以获取文件名,然后选中以“B”开头的文件名,然后
    检查是否包含特定日期。String对象具有startsWith()方法。您不需要将日期字符串更改为日期对象。只需检查文件名是否包含日期字符串

    切勿使用可怕的
    Date
    DateFormat
    SimpleDateFormat
    类。仅使用java.time类

    答案看起来是正确的。再加上对解析日期的讨论

    通过调用询问每个对象是否代表一个文件而不是一个目录。调用以生成带有文件名文本的
    字符串。然后使用和分析文件名。拉出可能日期的文本。通过尝试将文本解析为一个文本进行验证。使用与预期输入匹配的格式模式定义一个
    DateTimeFormatter

    LocalDate targetDate = LocalDate.of( 2019 , Month.APRIL , 17 ) ;
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu" ) ;
    int lengthOfExpectedDateInput = 10 ; // "01/23/2019" is 10 characters long.
    String fileName = file.getName() ;
    if( file.isFile() && fileName.startsWith( "B" ) ) {
        String possibleDateInput = fileName.substring( 1 , 1 + lengthOfExpectedDateInput ) ;  // Annoying zero-based index counting where 1 means the 2nd position. 
        try {
            LocalDate ld = LocalDate.parse( possibleDateInput , f ) ;  // Parse string as a `LocalDate` object. If input fails to match formatting pattern, a `DateTimeParseException` is thrown.
            if( ld.isEqual( targetDate ) ) {
                // Handle a valid file with expected file name.
                …
            }
        } catch ( DateTimeParseException e ) {
            // Handle unexpected file name.
            …
        }
    }
    
    顺便说一下,让这些文件名的发布者了解标准。日期的格式应为YYYY-MM-DD


    你的问题实际上是许多其他问题的翻版。发布前搜索堆栈溢出。

    我非常建议您不要使用
    SimpleDateFormat
    Date
    。前者是出了名的麻烦,后者的设计也很糟糕,幸运的是两者都早已过时。您应该想使用来自的
    LocalDate
    DateTimeFormatter
    。您能:(1)更清楚地说明您的问题吗?在Java行话中,throw表示抛出异常,但我怀疑这是否是您的问题?或者你说的日期不正确是什么意思?(2) 以更可读的方式缩进和格式化代码?使用编辑字段上方的
    {}
    按钮,确保将代码格式化为问题中的代码。谢谢。请允许我说实话:即使在回答了3个问题之后,我仍然相信我能做出一些有帮助的贡献,但我不想在您的代码中挣扎,因为它目前的状态。
               public static void main(String[] args){
       String path = "C:";
       String [] filesList = find(path);
           for (String file : filesList) {
       if(filesort().equals("04/17/2019"))//to read all files that has 04/17/2018
        {
       reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(path + "//" +file))));
           String content;
           while ((content = reader.readLine()) != null) {
               System.out.println(content);
               }  
       }
           else if (!filesort().equals("04/17/2019")||filesort()==null ) {
               System.out.println("incorect date");
           }
    
    this are the files I'm trying to read
     BProce.Arr.20190416.server10.gz
     BProce.Arr.20190417..ball10.gz
     BProce.Arr.20190417.ball23.gz
    
    public File[] find(String path) {
        File dir = new File(path);
        File[] listOfFiles = null;
        if (dir.isDirectory()) {
             listOfFiles = dir.listFiles();
        }
    
        return fileList;
    }
    
    LocalDate targetDate = LocalDate.of( 2019 , Month.APRIL , 17 ) ;
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu" ) ;
    int lengthOfExpectedDateInput = 10 ; // "01/23/2019" is 10 characters long.
    String fileName = file.getName() ;
    if( file.isFile() && fileName.startsWith( "B" ) ) {
        String possibleDateInput = fileName.substring( 1 , 1 + lengthOfExpectedDateInput ) ;  // Annoying zero-based index counting where 1 means the 2nd position. 
        try {
            LocalDate ld = LocalDate.parse( possibleDateInput , f ) ;  // Parse string as a `LocalDate` object. If input fails to match formatting pattern, a `DateTimeParseException` is thrown.
            if( ld.isEqual( targetDate ) ) {
                // Handle a valid file with expected file name.
                …
            }
        } catch ( DateTimeParseException e ) {
            // Handle unexpected file name.
            …
        }
    }