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

  • Home
  • SEARCH
  • 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 6379189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:06:20+00:00 2026-05-25T02:06:20+00:00

I am trying to construct a Java File object based on a user provided

  • 0

I am trying to construct a Java File object based on a user provided file name (could be absolute or relative) and a environment dependent base directory. The java doc for java.io.File(File parent, String child) says the following:

If the child pathname string is absolute then it is converted into a relative pathname in a system-dependent way.

That made me think that if I have the following code:

public class TestClass {
    public static void main(String[] args) throws IOException {
        File file = new File(new File("C:/Temp"),"C:/Temp/file.txt");
        System.out.println(file.getAbsolutePath());
    }
}

the output would be

C:\Temp\file.txt

and then I’d be in business because it would not really matter anymore if the user provided an absolute or relative path. But in fact, the output is

C:\Temp\C:\Temp\file.txt

Which means I have to figure out the exact relative path (or at least test different options to see if the file exists). Am I misunderstanding the JavaDoc?

  • 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-25T02:06:21+00:00Added an answer on May 25, 2026 at 2:06 am

    If the child pathname string is absolute then it is converted into a relative pathname in a system-dependent way.

    I assume this means that even if you provide an absolute path, it will be converted to (in a system dependent way), and treated as, a relative path.

    Which means I have to figure out the exact relative path (or at least test different options to see if the file exists).

    Yes, I believe so.

    This could perhaps be easily done with

    file.getAbsolutePath().startsWith(parent.getAbsolutePath());
    

    to check if it is an absolute path to a directory in parent, and

    file.getAbsolutePath().substring(parent.getAbsolutePath().length());
    

    to get the relative part.

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

Sidebar

Related Questions

My string.xml has a string: <string name=loginLocation>http://website.com/afile.php</string> In my java file, I am trying
I'm trying to construct a java web app along modular principles, with some common
I'm currently trying to construct a list of bean classes in Java from a
I'm trying to figure out how to read the META-INF/MANIFEST.MF file form a java
I am trying to get a FileInputStream object on an image that the user
I'm trying create an immutable object and initialise it from xml config file in
I'm trying to load a java .class file dynamically and call it by reflection.
I've written a Java program to generate an m3u file based on a CD
I'm trying to understand the contract of drainPermits in the Java Semaphore class The
Am trying to construct a simple update query in my model class Model_DbTable_Account extends

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.