-1

In my Java program i process a certain amount of files. Those Files are named in this way:

thu 21 mar 2013_01.55.22_128.txt
thu 21 mar 2013_01.55.22_129.txt
thu 21 mar 2013_01.55.22_130.txt
....
sat 23 mar 2013_01.45.55_128.txt
sat 23 mar 2013_01.45.55_129.txt
sat 23 mar 2013_01.45.55_130.txt

Where the last three numbers are the cell number. Consider that i already read in order of date the files coming from the same cell. Consider that all the files are on the same folder. Consider also that this problem, but for a single cell, was correctly solved on This Post

My question now is: how can i read first all the txt coming from a specific cell (e.g 128), then all the files coming from cell 129 and so on? (below: a graphic example)

thu 21 mar 2013_01.55.22_128.txt
sat 23 mar 2013_01.45.55_128.txt
...
thu 21 mar 2013_01.55.22_129.txt
sat 23 mar 2013_01.45.55_129.txt
...
thu 21 mar 2013_01.55.22_130.txt
sat 23 mar 2013_01.45.55_130.txt

I hope I was clear

Community
  • 1
  • 1
alessandrob
  • 1,605
  • 4
  • 20
  • 23
  • 2
    Answer is quite clear. All you need is to write code that _reads first all the txt coming from a specific cell (e.g 128), then all the files coming from cell 129 and so on_. Unless you show what you've tried, we are unable to help you. – Leri Jul 23 '13 at 09:39
  • @PLB, in [This Post](http://stackoverflow.com/questions/17772716/java-string-comparing-among-many-txt-files) there's the code i used for this program :) – alessandrob Jul 23 '13 at 09:48

3 Answers3

1

You may get all files in directory using listFiles() into array then sort it using custom comparator.

File[] files = dir.istFiles();
Array.sort(files, new Comparator<File> {
    @Override
    public int compare(File lhs, File rhs) {
        //return -1 if lhs should go before
        //0 if it doesn't matter
        //1 if rhs should go after
    }
});
RiaD
  • 46,822
  • 11
  • 79
  • 123
0

Well, you could read the folder in order to get the File objects (or maybe just file names). Then parse the file names, extract the cell and put the files into a map whose key is the cell.

Some pseudo code:

Map<String, List<File>> filesPerCell = new LinkedHashMap<String, List<File>>();

File[] files = folder.listFiles();

for( File file : files ) {
  String filename = file.getName();
  String cell = ... ; //extract from filename
  List<File> l = filesPerCell.get( cell );

  //create new list if l is null

  l.add( file ); 
}

for( List<File> cellList : filesPerCell.values() ) {
  //do whatever you want with the files for that cell
}
Thomas
  • 87,414
  • 12
  • 119
  • 157
0

You will have your file names sorted by cell number, and inside the cell, by date/time. You could do this most easily, if your file names were like this:

cellnumber_yyyymmdd_hhmmss

where cellnumber would be the same number of digits in all cases.

Otherwise you must write a custom comparator (as @RiaD writes), but it is not trivial because of the dates that must be parsed so one could decide on later/earlier.

Ingo
  • 36,037
  • 5
  • 53
  • 100