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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:09:14+00:00 2026-05-13T22:09:14+00:00

Right now we’ve got a hard-coded set of characters that we check for —

  • 0

Right now we’ve got a hard-coded set of characters that we check for — :*?"<>|/\ — basically the ones Windows complains about. However, running on Linux, this is way too restrictive.

I know that in Java 7 NIO, the Path class is supposed to be smart enough to check this in an OS-dependent way, and throw an InvalidPathException if you specify an invalid filename. But we’re not running Java 7. Is there an reliable way to do this in Java 6?

(Note that new File("foo:bar") in Windows does appear to work. But what you actually get if, for instance, try to use a FileWriter to write to your new File, is an empty file called foo. exists() on the File will return true at that point, but it’s more or less lying.)

  • 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-13T22:09:15+00:00Added an answer on May 13, 2026 at 10:09 pm

    For lack of anything clever, here’s what I came up with — the meat of it, anyway.

    Strictly speaking, it’s probably the combination of OS and filesystem that determines the invalid character set, but for my purposes a hack based simply on the OS seems to be good enough.

    Also, these invalid character sets are empirical, not official. The Windows invalid characters are taken from the error message you get in XP when you try to rename a file on an NTFS volume to something invalid. For Unix/Linux, I think you can get away with pretty much anything except a path separator (please correct me if you know better). For MacOS, whether : or / is the path separator seems to depend on the filesystem — for my purposes it’s safest just to include both. (And hope they’re not mounting FAT or NTFS.)

    List<Integer> invalidIndices = new LinkedList<Integer>();
    
    String invalidChars;
    if (OS.isWindows()) {
        invalidChars = "\\/:*?\"<>|";
    } else if (OS.isMacOSX()) {
        invalidChars = "/:";
    } else { // assume Unix/Linux
        invalidChars = "/";
    }
    
    char[] chars = filename.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if ((invalidChars.indexOf(chars[i]) >= 0) // OS-invalid
            || (chars[i] < '\u0020') // ctrls
            || (chars[i] > '\u007e' && chars[i] < '\u00a0') // ctrls
        ) {
            invalidIndices.add(i);
        }
    }
    
    return invalidIndices;
    

    Note: this is using the SwingX OS utility class to determine the operating system, but if you don’t have that, it doesn’t do anything magical either — just parses System.getProperty("os.name").

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

Sidebar

Related Questions

Right now I've hard coded the whole xml file in my python script and
Right now I have an application that loads a bunch of thumbnail images into
Right now I've programmed a method for use in a swing program that is
Right now, I only understand that Global indexes on a partitioned table are for
Right now I am working on c# windows applications developement. But my concern is
Right now I want to store a text file that goes like this: 1
Right now I can set a specific length, or a range, but not two
Right now, I have a jQuery UI pop-up dialog that reads from an external
Right now we have a dll file that contains all the database calls and
Right now I have a script that creates symlinks to anything newer than 2

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.