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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:17:05+00:00 2026-05-24T02:17:05+00:00

I am getting some strange behavior when trying to convert between Files and URLs,

  • 0

I am getting some strange behavior when trying to convert between Files and URLs, particularly when a file/path has spaces in its name. Is there any safe way to convert between the two?

My program has a file saving functionality where the actual “Save” operation is delegated to an outside library that requires a URL as a parameter. However, I also want the user to be able to pick which file to save to. The issue is that when converting between File and URL (using URI), spaces show up as “%20” and mess up various operations. Consider the following code:

//...user has selected file
File userFile = myFileChooser.getSelectedFile();
URL userURL = userFile.toURI().toURL();

System.out.println(userFile.getPath());
System.out.println(userURL);

File myFile = new File(userURL.getFile());

System.out.println(myFile.equals(userFile);

This will return false (due to the “%20” symbols), and is causing significant issues in my program because Files and URLs are handed off and often operations have to be performed with them (like getting parent/subdirectories). Is there a way to make File/URL handling safe for paths with whitespace?

P.S. Everything works fine if my paths have no spaces in them (and the paths look equal), but that is a user restriction I cannot impose.

  • 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-24T02:17:07+00:00Added an answer on May 24, 2026 at 2:17 am

    The problem is that you use URL to construct the second file:

    File myFile = new File(userURL.getFile());
    

    If you stick to the URI, you are better off:

    URI userURI = userFile.toURI();
    URL userURL = userURI.toURL();
    ...
    File myFile = new File(userURI);
    

    or

    File myFile = new File( userURL.toURI() );
    

    Both ways worked for me, when testing file names with blanks.

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

Sidebar

Related Questions

I'm getting something pretty strange going on when trying to read some data using
I'm getting some strange behavior in Firefox whenever I put checkboxes inside a list
I'm getting a very strange behavior in MySQL, which looks like some kind of
I am getting some strange behavior from list view. I need to change background
I am getting some strange behavior involving database queries that I have never seen
I'm getting some strange behavior from a listview/the getChildAt method. I have a HashSet,
I'm getting some strange, intermittent, data aborts (< 5% of the time) in some
I am trying to make a form with some dynamic behavior. Specifically, I have
I'm getting so very strange behavior in my Objective C library (for iPhone). What
I'm trying to learn python and have encountered some strange behaviour. I am experimenting

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.