0

I want to take all .txt files according to the file stored order in same folder. I used several ways like followings. But, I cound't take them in correct order.

1st try:

List<File> filesInFolder = Files
    .walk(Paths
    .get("C:/Users/Desktop/read"))
    .filter(Files::isRegularFile).map(Path::toFile)
    .collect(Collectors.toList());

2ns try:

File file = new File("C:/Users/Desktop/read");
File[] filesInFolder = file.listFiles();

3rd try:

File dir = new File("C:/Users/Desktop/read");
List<File> filesInFolder = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);

But, these codes not success for take file in order. I used 30 files and it read an order like,

1       10      11      12      13      14      15      16      17      18      19      2       20      21      22      23      24      25      26     27       28      29      3       30      4       5       6       7       8     9 

My files names result1.txt, result2.txt,result3.txt,resul4.txt.........

How to read files in correct order ( as stored / )like,

result1.txt, result2.txt,result3.txt,result4.txt, result5.txt, result6.txt, result7.txt, result8.txt, result9.txt, result10.txt, result11.txt, result12.txt.................................
Emalka
  • 381
  • 2
  • 4
  • 16
  • 2
    http://stackoverflow.com/questions/16898029/how-to-sort-file-names-in-ascending-order – AJ. Jun 01 '16 at 04:05
  • @AJ. I used `Arrays.sort(filesInFolder);` . But it take like, `1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 4 5 6 7 8 9` – Emalka Jun 01 '16 at 04:13
  • 2
    Direct sort is not going to work because these numbers are of `string` type not `int` type. First convert it in `int` array and then try to sort. Solution is already provided in mentioned link. – AJ. Jun 01 '16 at 04:20

2 Answers2

0

Your FileUtils Should be look like this-- call this class inside the Foreach loop

public class FilesUtil {

public static void saveFile(File file, String fileName, String filesDirectory) throws IOException{
    FileInputStream in = null;
    FileOutputStream out = null;

    File dir = new File (filesDirectory);
    if ( !dir.exists() )
       dir.mkdirs();

    String targetPath =  dir.getPath() + File.separator + fileName;
    System.out.println("source file path ::"+file.getAbsolutePath());
    System.out.println("saving file to ::" + targetPath);
    File destinationFile = new File ( targetPath);
    try {
        in = new FileInputStream( file );
        out = new FileOutputStream( destinationFile );
        int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

    }finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }

}

}

0

You need to sort your list. Something like this should do:

List<File> filesInFolder = Files.walk(
        Paths.get("C:/Users/Desktop/read"))
    .filter(Files::isRegularFile)
    .map(Path::toFile)
    .sorted((l,r)->{l.getName().compareTo(r.getName())})
    .collect(Collectors.toList());

Ah, you want non-standard int ordering within strings. There is not really a clean way to do this, but one way would be to use a Comparator like this one that understands the "natural order".

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • @patrick It gave an error in `sorted` as `The method sorted(Comparator super File>) in the type Stream is not applicable for the arguments (( l, r) -> {})` – Emalka Jun 01 '16 at 10:12