如何使用时间戳读取文本文件?在Java中

时间:2019-02-26 05:17:50

标签: java file

Asterisk是否可以在Java中工作?我想读取带有时间戳的文件。 taxonomy_timestamp.txt,但不起作用。

    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;
    }

1 个答案:

答案 0 :(得分:2)

尝试一下。

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());
    }
}