Get full path to file in java

When I pass the File file method I'm trying to get, the full path is like file.getAbsolutePath(); I always get the same result no matter which one I use either the absolute or canonical path PATH_TO_MY_WORKSPACE/projectName/filename , and it doesn't exist, how can I get the exact file location?

thanks

DETAILS:

Here is some code and this solution (its bad, but its work):

  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 loop the directories directories where factDir src/main , I look for files only for BeProcessed.txt only for the value TEMP_COMPARE_FILE, and I send them to a processing method that reads the file and processes it.

If anyone can improve the decision, I would be supportive

+7
java
source share
2 answers

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.

+13
source share

I think there is a way in which it can help you if and only if the file is in the program directory.

first you get the program directory:

 new File(".").getCanonicalPath() 

then:

if file is inside a specific directory, such as folder\\filename full path will be

 (new File(".").getCanonicalPath() + "\\folder\\filename") 

or if file is located directly inside the program directory: the full path will be

 (new File(".").getCanonicalPath() + "\\filename") 

I want this answer to help you :)

+2
source share

All Articles