0

I am trying to do some file editing in a directory which has some subfolders in it. However my java app doesnt go in depths of subfolders and only do operation in first subfolders like below

Folder1 (works) -folder11 (works) --files(works) --folder111 (doesnt work) -folder12(works) --files(works) -folder13(works) --files(works)

What am i doing wrong?

Here is my code:

btnBasla.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                File dir= new File(Path2.toString());
                File files[]=dir.listFiles();
                for (File f: files) {
                    if (f.isDirectory()) {
                        try {
                            listDir(f.toString());
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    } else {
                         if(f.getName().lastIndexOf('.')>0) {
                              int lastIndex = f.getName().lastIndexOf('.');
                              String str = f.getName().substring(lastIndex);
                              if(str.equals(".txt") || str.equals(".sub") || str.equals(".srt")) {
                                  try {
                                    String sonuc = islem.koddegıstır(f.getAbsolutePath());
                                    textField.append(sonuc+"\n");
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                              }
                         }                      
                    }

                }
            }
            public void listDir (String dir) throws IOException{
                File place = new File(dir);
                if(place.isDirectory()){
                    File files[]= place.listFiles();
                    for (File f:files) {
                        if (f.isDirectory()) {
                            listDir(f.getName());
                        } else {
                             if(f.getName().lastIndexOf('.')>0) {
                                  int lastIndex = f.getName().lastIndexOf('.');
                                  String str = f.getName().substring(lastIndex);
                                  if(str.equals(".txt") || str.equals(".sub") || str.equals(".srt")) {
                                      String sonuc = islem.koddegıstır(f.getAbsolutePath());
                                      textField.append(sonuc+"\n");
                                  }
                             }      
                        }
                    }
                }
            }
        });
Faruk
  • 773
  • 1
  • 6
  • 20
  • Possible duplicate of [list all files in the folder and also sub folders](https://stackoverflow.com/questions/14676407/list-all-files-in-the-folder-and-also-sub-folders) – Umer Farooq Apr 22 '18 at 10:04

2 Answers2

1

Something goes wrong with your function that lists files.
I think you should get all files in sub folders by using this function.

public List<File> listFiles(String dirPath) {
    List<File> files = new ArrayList<>();
    File dir = new File(dirPath);

    if(dir.isDirectory()) {
        for(File file : dir.listFiles()) {
            if(file.isDirectory()) files.addAll(listFiles(file));
            else files.add(file);
        }
    }

    return files;
}

As you can see, this function listing the files recursively.

  • it gives "Type mismatch: cannot convert from List to File[] error when i try this code." and "The method isDirectory() is undefined for the type String" when i try to use your code mate. – Faruk Apr 23 '18 at 16:18
0

iRandomXx's answer is correct, it's a recursion problem. I wanted to add to it though if it can be useful to you.

Consider separating the logic a bit more.

1- get all the files with a recursive method to walk them. It looks like you're only interested in files (and not directories). iRandomXx's method will get only the files and leave the directories, making your if-directory check redundant (I removed it).

2- iterate the list and do whatever you need to with each file. If an exception occurs, you should do something useful with it like show a meaningful error message to the user or add it to a list of errors.

btnBasla.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // get all files first
        File files[]=listFiles(Path2.toString()); // iRandomX's recursive method
        for (File f : files) {
            // your validation check for extensions
            // modified a bit
            try {
                String fName = f.getName();
                int dotPos = fName.lastIndexOf('.');
                if(dotPos > 0) {
                    String extension = fName.substring(dotPos);
                    if(VALID_EXTENSIONS.contains(extension)) {
                        textField.append(islem.koddegıstır(f.getAbsolutePath())+"\n");
                    }
                }
            } catch (Exception e) {
                // do something useful with the exception, like show the user an error dialog
            }
        }
    }
}


// private class member
private static final List<String> VALID_EXTENSIONS = new ArrayList<>();

// static block to initialize valid extensions
static {
    VALID_EXTENSIONS.add(".txt");
    VALID_EXTENSIONS.add(".sub");
    VALID_EXTENSIONS.add(".srt");
}

// iRandomX's recursive method here
geco17
  • 5,152
  • 3
  • 21
  • 38