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 7680497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:07:39+00:00 2026-05-31T18:07:39+00:00

This is a seemingly simple problem but I am having trouble doing it in

  • 0

This is a seemingly simple problem but I am having trouble doing it in a clean manner. I have a file path as follows:

/this/is/an/absolute/path/to/the/location/of/my/file

What I need is to extract /of/my/file from the above given path since that is my relative path.

The way I am thinking of doing it is as follows:

String absolutePath = "/this/is/an/absolute/path/to/the/location/of/my/file";
String[] tokenizedPaths = absolutePath.split("/");
int strLength = tokenizedPaths.length;
String myRelativePathStructure = (new StringBuffer()).append(tokenizedPaths[strLength-3]).append("/").append(tokenizedPaths[strLength-2]).append("/").append(tokenizedPaths[strLength-1]).toString();

This will probably serve my immediate needs but can somebody suggest a better way of extracting sub-paths from a provided path in java?

Thanks

  • 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-31T18:07:40+00:00Added an answer on May 31, 2026 at 6:07 pm

    Use the URI class:

    URI base = URI.create("/this/is/an/absolute/path/to/the/location");
    URI absolute =URI.create("/this/is/an/absolute/path/to/the/location/of/my/file");
    URI relative = base.relativize(absolute);
    

    This will result in of/my/file.

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

Sidebar

Related Questions

I'm having difficulty doing this seemingly simple task. I want to load XML files
I have been trying to get this seemingly simple problem fixed for about half
This is a particular problem that I have come across many times, but I
I'm stumped by a seemingly simple problem. In my ASP.NET page, I have a
I have a problem which appears when using Ubuntu Linux/Gnome, but seemingly not on
Good day once again. I have a (seemingly) simple problem. I'm trying to display
I am trying to do something seemingly simple, yet having problems. Basically, I have
I'm running in to a seemingly simple problem, but can't seem to get around
Need help! I've been looking for a solution for this seemingly simple task but
Hi I have seemingly simple problem which is causing my head to hurt. I

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.