开发者

How to read files of a directory by the order of modified time

开发者 https://www.devze.com 2023-03-26 23:24 出处:网络
I have one question for read files of a directory. First, String[] files = sourceDirectory.list(); I collect some data traces and put them in a directory. I wish to read the files in the order of c

I have one question for read files of a directory. First,

String[] files = sourceDirectory.list();

I collect some data traces and put them in a directory. I wish to read the files in the order of created time because I want to keep the sequence of data traces.

In the File.list() documentation, it can not guarantee a consistency order. How can I read files in an order?

The second question is: I want to compute the interval time between 2 messages from message name because message names have time stamp information. For example,

trace20开发者_StackOverflow11_Aug_3__0_0_1 and trace2011_Aug_3__0_0_5. How can I convert the string values into Date object and compute the difference between them?

Thank you very much.


Get the Files using listFiles() and then sort them by the last modified time using File.lastModified().

File[] files = sourceDirectory.listFiles();

Arrays.sort( files, new Comparator<File>() {
    public int compare( File a, File b ) {
        return a.lastModified() - b.lastModified();
    }
});

To convert a file name with a date in it, extract the date portion using String.substring() and then convert that resulting substring into a date. After that, getting the difference should be easy.


For the first half of your question:

File[] files = sourceDir.listFiles();

Arrays.sort(files, new Comparator<File>() { 
    public int compare(File f1, File f2) {
        return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
    }
});

// Now files should be sorted by timestamp.


Regarding your first question: just use File.listFiles() and sort the resultaing array of File instances using Arrays.sort and a custom comparator comparing the lastModified value of the files. Then iterate through the sorted array.

Regarding the second question: Use String.substring to extract the date part from the message, and use SimpleDateFormat with the appropriate pattern to change each string into a Date object. The interval is just the difference between the time in milliseconds (obtained via Date.getTime()) of the two dates.


The only way I know to do this is to shell out and read the output. This link has a pretty ugly method for getting the creation date from the file:

http://www.daniweb.com/software-development/java/threads/51670

As far as the date parsing is concerned, you can create a custom date formatter and use it to parse your date values:

http://exampledepot.com/egs/java.text/ParseDate.html

Once you have the date parsing working, use this method to get the difference:

How can I calculate a time span in Java and format the output?

Good luck!


First question: see tskuzzy´s answer.

Second question: Write a parser-class/method and use the substring method to convert the strings to date. (in fact only you know, how the string is built). Maybe Apache´s String Utils help you with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消