Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 834419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:40:08+00:00 2026-05-15T04:40:08+00:00

When I pass File file to a method I’m trying to get its full

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:40:08+00:00Added an answer on May 15, 2026 at 4:40 am

    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 property user.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:

    • you could use a FilenameFilter to find the desired files,
    • note that list and listFiles return null for non-directory objects, so we need an extra check for that,
    • you could also use 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.