When I pass File file
to a method I'm trying to get its full path like file.getAbsolutePath();
I always get the same result no matter which one I use either absolute or canonical path PATH_TO_MY_WORKSPACE/projectName/filename
and it is not there, how can I get exact location of the file?
Thank you
DETAILS:
Here is some code and this solutions(its bad but its working):
private static void doSomethingToDirectory(File factDir) throws IOException {
File[] dirContents = factDir.listFiles();
if(factDir.isDirectory() && dirContents.length > 0){
for (int i = 0; i < dirContents.length; i++) {
for (String str : dirContents[i].list()) {
if(str.equals(TEMP_COMPARE_FILE)){
process(new File(dirContents[i].getAbsolutePath() + "\\" + str));
}
}
}
}
}
I'm looping trough directories where factDir is src/main
, I'm seeking toBeProcessed.txt files only that is TEMP_COMPARE_FILE value and I'm sending them to process method which reads the file and does processing of it.
If someone could better solution I'd be greatful
This quote from the Javadoc might be helpful:
A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the
java.io
package always resolve relative pathnames against the current user directory. This directory is named by the system propertyuser.dir
, and is typically the directory in which the Java virtual machine was invoked.
I interpret this so that if you create your File
object with new File("filename")
where filename
is a relative path, that path will not be converted into an absolute path even by a call to file.getAbsolutePath()
.
Update: now that you posted code, I can think of some ways to improve it:
list
and listFiles
return null
for non-directory objects, so we need an extra check for that,listFiles()
again in the inner loop, thus avoiding the need to create new File
objects with hand-assembled paths. (Btw note that appending \\
manually to the path is not portable; the proper way would be to use File.separator
).The end result is
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);
}
}
Note that this code mimics the behaviour of your original piece of code as much as I understood it; most notably, it finds the files with the proper name only in the direct subdirectories of factDir
, nonrecursively.
I think there is a way it may help you if and only if the file is in the program directory.
first you get the program directory by :
new File(".").getCanonicalPath()
then :
if file
is inside a specific directory like folder\\filename
the full path will be
(new File(".").getCanonicalPath() + "\\folder\\filename")
or if file
is directly inside the program directory:
the full path will be
(new File(".").getCanonicalPath() + "\\filename")
i wish this answer help you :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With