I'm trying to filter the files by date and to read each file. I have a find() method that read each filename and it returns a files start by "B" in array. The second method filesort(), it will return all the file dates from filename that sent from find() method. In main method I want to read the file by the specific date i'm giving. if all the files has the same date, it reads all the file. However, from the files one of the file has different date it will throw error.
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
because the first file is 04/16/2019, it will throw incorrect date. if 3 of them has 04/17/2019, it will read without issue. but for now i want to read only the file that has a date 04/17/2019