4

Possible Duplicate:
Is there a way in Java to determine if a path is valid without attempting to create a file?

I want to check if a string is a valid path name for a folder or a file. Valid path names can be like C:\Games\WarCraft and invalid path is c:[][]Games\\WarCraft. How do i check if this is correct ?

I am NOT interested in checking if that file or folder exists. I don't want to use any external libraries like Apache Commons to do it, just plain old java.

Also, I want to we make my code system independent - UPDATE that is not necessary though

Clues please ! :)

Community
  • 1
  • 1
david blaine
  • 5,683
  • 12
  • 46
  • 55
  • If you try to create a file, doesn't it already throw an exception for this? (creating a java.io.File btw) – OmniOwl Dec 18 '12 at 01:03
  • 1
    Doing this in a system independent way is nigh impossible since each OS defines a valid path name differently. For example, the example of a valid path which you gave is only valid on Windows, not on Linux or Mac. – Code-Apprentice Dec 18 '12 at 01:06
  • @Code-Guru - then we could use code to find out the system and run "path-checker" method for that system. – david blaine Dec 18 '12 at 01:09
  • I don't know of any method in the Java API which will check if a path name is valid. However, it shouldn't be difficult to implement at least a primitive validator using the static fields in [java.io.File](http://docs.oracle.com/javase/7/docs/api/java/io/File.html). In particular, [separatorChar](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#separatorChar) would be extremely useful. – Code-Apprentice Dec 18 '12 at 01:14
  • please re open this question. This question is hardly related to http://stackoverflow.com/questions/468789/is-there-a-way-in-java-to-determine-if-a-path-is-valid-without-attempting-to-cre?lq=1 – david blaine Dec 18 '12 at 01:33
  • proof - put this in the constructor of File "c:\\[][]cygwin\\cygwin.bat"...then put print statements in both ifs - "dir found" in 1st if and "file found" in second if. output is dir found which is obviously not what i had asked for. – david blaine Dec 18 '12 at 01:36

1 Answers1

2

You cannot do this in a system independent way, and I don't think there are any standard APIs for pathname validation.

The best suggestion I can think of is to use a regex to match the pathname. The exact details will depend on how loosely or tightly you want to constrain the pathname. For instance, on Linux you might want to disallow spaces in pathnames, because while they are valid they also cause problems for poorly written shell scripts. Or you might want to insist on absolute (or relative) pathnames.

But before you do the regex match, it would be a good idea to canonicalize the pathname using File.getCanonicalFile(). That will do things like checking symbolic links, getting rid of redundant "." and ".."s, and turning drive letters into standard form ... which should simplify your regex.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Is get canonical file followed by regex the only way to do it ? – david blaine Dec 18 '12 at 01:23
  • Well you could fiddle with the details, but basically yes. – Stephen C Dec 18 '12 at 07:16
  • Stephen C, please use your admin super powers to make SO add a button which lets us "request to re-open a question". People just close a post, run away without giving reasons and never come back. Utterly disappointing. – david blaine Dec 18 '12 at 08:31
  • **spaces cause problems for scripts poorly written** aaargh!!, Why didn't I have this info before? http://stackoverflow.com/q/34905403/111110 – Broken_Window Mar 14 '16 at 15:32