This quote from Javadoc may be helpful:
The path name, whether abstract or string, can be absolute or relative. The absolute path name is complete, as no other information is required to find the file that it designates. The relative name of the path, in contrast, should be interpreted in terms of information taken from another path. By default, classes in the java.io package always allow relative paths to the user's current directory. This directory is called the system property user.dir and is usually the directory in which the Java virtual machine was invoked.
I interpret this so that if you create your File object using new File("filename") , where filename is a relative path, that path will not be converted to an absolute path even by calling file.getAbsolutePath() .
Update: now that you have posted the code, I can think of some ways to improve it:
- you can use FilenameFilter to find the files you need,
- note that
list and listFiles return null for objects without a directory, so we need an extra check for this, - you can also use
listFiles() again in the inner loop, thereby avoiding the need to create new File objects with manually assembled paths. (By the way, note that adding \\ manually to the path is not portable, the correct way would be to use File.separator ).
Final result
private static void doSomethingToDirectory(File factDir) throws IOException { if (factDir.isDirectory()) { for (File file : factDir.listFiles()) { if (file.isDirectory()) { for (File child : file.listFiles(new MyFilter())) { process(child); } } } } } class MyFilter implements FilenameFilter { public boolean accept(File dir, String name) { return name.equals(TEMP_COMPARE_FILE); } }
Please note that this code mimics the behavior of your source code, as I understand it; first of all, it finds files with its own name only in direct factDir directories, non-recursively.
Pรฉter Tรถrรถk
source share