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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:19:11+00:00 2026-06-15T10:19:11+00:00

Using Java 1.6 Filepath can be entered by user and then I apply various

  • 0

Using Java 1.6 Filepath can be entered by user and then I apply various regular expressions to remove characters that are invalid for the platform (such as ‘?’ is invalid on Windows), and check path length to ensure we end up with a valid filepath for the OS before trying to create the filepath.

But there are two problems:

  1. Its a pain working out what is valid or not for each platform.
  2. I’m making assumptions based on default filesystem for the platform, but of course an OSX system could be writing to a non-mac filesystem such a FAT32, in which case these checks will not be valid.

So I was hoping there would be a better way to do it with NIO2 in Java 7, but haven’t found a solution yet, is there one ?

  • 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-06-15T10:19:12+00:00Added an answer on June 15, 2026 at 10:19 am

    Depending on your expected result (corrected String? Invalid character position? Exception?), this should give you an idea of what can be done:

    import java.io.File;
    import java.nio.file.InvalidPathException;
    public class Test {
        public static final void main(final String[] args) {
            final String current = new File(".").toPath().toAbsolutePath().normalize().toFile().toString();
            Test.correctPath(current);
            Test.correctPath(current + "aValidExpression");
            Test.correctPath(current + "aValidExpression?;:-&é");
            Test.correctPath(current + "aValidExpr//ession?;:-&é");
            Test.correctPath(current + "aValidExpre\\ssion?;:-&é");
        }
    
        public static final String correctPath(final String path) {
            try {
                final String returnValue = new File(path).toPath().toAbsolutePath().normalize().toFile().toString();
                System.out.println(returnValue);
                return returnValue;
            } catch (final InvalidPathException e) {
                System.err.println(e.getMessage());
                final int errorIndex = e.getIndex();
                final String newPath = path.substring(0, errorIndex - 1) + path.substring(errorIndex + 1);
                return Test.correctPath(newPath);
            }
        }
    }
    

    I hope it helps.

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

Sidebar

Related Questions

Using java If I store windows paths in a h2 database, and then try
Using Java (1.6) I want to split an input string that has components of
By using java reflection, we can easily know if an object is an array.
I have a Java application were the user can create a text file and
I am reading image files in Java using java.awt.Image img = Toolkit.getDefaultToolkit().createImage(filePath); On some
Possible Duplicate: How can I lock a file using java (if possible) I have
I have created a program in Java using eclipse that contains a couple of
I wrote a simple client/server in java that transfers files using java NIO Socketchannel.
I'm importing a CSV file to MySQL database. This can be done using java.mysql
Can someone point me to some java code that can upload a file via

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.