-1

Does Asterisk work in java? I want to read a file with timestamp. taxonomy_timestamp.txt but it doesn't work.

    String fileName = "20190215/"+"taxonomy_*.txt";
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {

            String line;

            while ((line = reader.readLine()) != null) {
                if(line.contains(":")) {
                    String[] segmentData = line.split(":");
                    String keyword = segmentData[0];
                    String name = segmentData[1];
                    segmentList.add(new ExternalSegmentDownloader.ExternalSegmentKey(keyword, name));
                }
            }
        }catch(IOException e){
            log.info("File not found.",e);
        }
        return segmentList;
    }
DavidW
  • 33
  • 6
  • what error do you get? and can you please update full path with file name – Ryuzaki L Feb 26 '19 at 05:20
  • I don't have the filename. the prefix of the file is taxonomy_ with a txt extension. but timestamp will come after the prefix. – DavidW Feb 26 '19 at 05:22
  • I found these questions and answers, seems pretty similar. https://stackoverflow.com/questions/794381/how-to-find-files-that-match-a-wildcard-string-in-java https://stackoverflow.com/questions/2102952/listing-files-in-a-directory-matching-a-pattern-in-java – Kareem Feb 26 '19 at 05:23

1 Answers1

2

Try this.

public static void main(String[] args) throws IOException {
    File directory = new File(".");

    File[] files = directory.listFiles();
    System.out.println("All files and directories:");
    displayFiles(files);

    String pattern =  "20190215/"+"taxonomy_[*].txt";
    System.out.println("\nFiles that match regular expression: " + pattern);
    FileFilter filter = new RegexFileFilter(pattern);
    files = directory.listFiles(filter);
    displayFiles(files);

}

public static void displayFiles(File[] files) {
    for (File file : files) {
        System.out.println(file.getName());
    }
}

Mark Melgo
  • 1,477
  • 1
  • 13
  • 30