1

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,

trace2011_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.

adarshr
  • 61,315
  • 23
  • 138
  • 167
susan
  • 11
  • 1
  • 2

5 Answers5

5

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.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
2

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.
adarshr
  • 61,315
  • 23
  • 138
  • 167
0

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

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!

Community
  • 1
  • 1
Justin Beckwith
  • 7,686
  • 1
  • 33
  • 55
  • Just a side note: I was assuming you needed date created, not date modified - if you're looking for the modified date, the other methods in this post are a better bet. – Justin Beckwith Aug 09 '11 at 16:36
0

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
CSan
  • 954
  • 1
  • 10
  • 25