-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

2
  • 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. Commented Jul 23, 2013 at 9:39
  • @PLB, in This Post there's the code i used for this program :) Commented Jul 23, 2013 at 9:48

3 Answers 3

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
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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
}

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.