我正在尝试按日期过滤文件

问题描述 投票:-2回答:3

我正在尝试按日期过滤文件并读取每个文件。我有一个find()方法读取每个文件名,它返回一个文件以数组开头“B”。第二种方法filesort(),它将从find()方法发送的文件名返回所有文件日期。在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;

}

   public String filesort() throws ParseException {
   String path = "C:";
   String [] filesList = find(path);
   for (String file : filesList) {
       File st = new File(file);
       String name=st.getName();
       name= name.replaceAll("\\D+", "");
       String Datename = name.substring(0, 8);
       DateFormat formatter = new SimpleDateFormat("yyyymmdd");
       Date date = (Date)formatter.parse(Datename);
       SimpleDateFormat newFormat = new SimpleDateFormat("mm/dd/yyyy");
       String finalString = newFormat.format(date);
       return finalString;
   }
   return "error";

}

           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

因为第一个文件是04/16/2019,它会抛出错误的日期。如果他们中的3人有04/17/2019,那么它将毫无问题地阅读。但是现在我想只读取日期为04/17/2019的文件

java file date gz
3个回答
1
投票

如果我们从另一个角度看问题,那么实现你想要的东西似乎很简单。我会给出基本的逻辑。

  1. 从目录中读取文件名。 here就是如何做到这一点的例子
  2. 将名称存储在ArrayList中
  3. 使用集合对ArrayList进行排序following链接可以帮助您
  4. 现在,只要您需要访问,就可以对目录的文件名进行排序,只需访问ArrayList元素并使用它访问真实文件

Happy Coding,如果您还有问题,请告诉我


1
投票

要查找文件名称从“B”开始并包含特定日期,请按照此过程操作。

您可以使用File类使用此代码查找所有文件。

public File[] find(String path) {
    File dir = new File(path);
    File[] listOfFiles = null;
    if (dir.isDirectory()) {
         listOfFiles = dir.listFiles();
    }

    return fileList;
}

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


1
投票

永远不要使用可怕的DateDateFormatSimpleDateFormat课程。仅使用java.time类。

Answer by Rishoban看起来很正确。再加上解析日期的讨论。

通过调用File询问每个File::isFile对象是否代表文件与目录。调用File::getName生成带有文件名文本的String。然后使用String::startsWithString::substring分析文件名。拉出可能日期的文本。通过尝试将文本解析为LocalDate进行验证。使用格式化模式定义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.
        …
    }
}

顺便说一下,教育这些文件名的发布者关于ISO 8601标准。日期应采用YYYY-MM-DD格式。

您的问题实际上与许多其他问题重复。在发布之前搜索Stack Overflow。

© www.soinside.com 2019 - 2024. All rights reserved.