2

I'm working on a program for which I need to see if a string could theoretically be a filepath/directory (ether absolute or relative). I've been looking around trying to see if there is an easy way to do this and I'm finding loads of questions which focus on also seeing if the file/folder exists (e.g., this or this)

Is there a way to see if a string is a filepath/directory without it actually having to exists?

EDIT: I was asked in the comments to elaborate on what a valid filepath/directory is:

The way my program handles filepaths is that it usually (depending on exactly what it needs to load) takes a few "parts" of the filepath/directory and uses that to read a file.

Therefore we can use the following rules:

either the string contains a filepath which:

  • starts with the name of the root folder (absolute filepath), or starts with a directory name (relative filepath), or starts with a filename.
  • has 0 or more / characters to indicate subfolders.
  • ends with an extension (Example: .txt, .vs or .fs)

Directories, however

  • starts with the name of the root folder (absolute directory), or starts with a directory name (relative directory)
  • has 0 or more / characters to indicate subfolders.
  • ends with a / character.

Also, no spaces are allowed in the string.

I'm making the software to run on Windows initially but I might need to port it to Linux (therefore changing the name of the root folder and maybe more).

KevinO
  • 4,303
  • 4
  • 27
  • 36
BRHSM
  • 854
  • 3
  • 13
  • 48
  • 1
    Can you give a sample of what would and what wouldn't be considered a legitimate filepath? Because suprisingly many arbitrary string values are legitimate paths. – Joachim Sauer Oct 24 '18 at 09:28
  • 1
    You could try a regular expression. The only problem: what is valid and what not depends on the file system. And no, I don't know the relevant regular expressions. – Ralf Renz Oct 24 '18 at 09:30
  • 1
    To expand on Joachim's comment: a relative path could only be a filename and since those aren't restrict very much you could have _theoretical_ relative paths such as `"I don't have any idea if this is a path"` (note that files don't have to have extensions). So you'd probably want to impose some more restrictions, one of which might be that the path in questions would have to exist. – Thomas Oct 24 '18 at 09:33
  • 1
    *"...ether absolute or relative..."* So `"foo"` would be a path, then. Also, just about anything is a valid filename in \*nix file systems... – T.J. Crowder Oct 24 '18 at 09:34
  • Perhaps java.io.FileSystem.normalize(String pathname) – charlb Oct 24 '18 at 09:40
  • @RalfRenz i think nobody really "knows" regular expressions to be honest! I've edited my question to include what a valid file is. – BRHSM Oct 24 '18 at 09:43
  • Sorry for the vague question, I've edited it hoping it clears up some stuff – BRHSM Oct 24 '18 at 09:44
  • Can you show us what you got? If those are the rules, it should be very easy to write a check for it. – Wesley De Keirsmaeker Oct 24 '18 at 09:47
  • @WesleyDeKeirsmaeker I don't really have anything to be honest, I tired using regular expressions but I could not find anything that would really work for every rule. – BRHSM Oct 24 '18 at 09:52
  • @BRHSM: if I read your rules correctly, then they can boil down to "has no spaces and ends in either a file extension or a slash", since everything else is optional. Is `this is a sentence. and another one.` a file path? Since technically `. and another one.` is the extension. I think the problem itself is very ill-defined. **Why** do you care if something "could be a file name or directory name"? – Joachim Sauer Oct 24 '18 at 09:58
  • @JoachimSauer because if there is no filename specified I know that it's a directory and that more stuff needs to be added to it. For example I have a resource directory with 2 folders "foo" and "bar" which both have 4 subfolders "sub1","sub2","sub3" and "sub4" with the files i want to use in them. I specify in strings which folder,subfolder and file to use by the program and therefor I need to see if the entire path leads to a file. But I want to check if for each string the path is correct, even if it's not the total path. I don't need to theck the total path. – BRHSM Oct 24 '18 at 10:16

1 Answers1

2

You can use Paths.get() to determine if a path is valid or not.

try {
    Paths.get(path);
} catch (InvalidPathException | NullPointerException ex) {
    return false;
}
return true;

For checking a semantically valid absolute path, you can use File.isAbsolute().

EDIT: Based on your requirement of not allowing spaces, you will need to check that separately. Windows allows spaces to be part of file names.

Khurram
  • 181
  • 1
  • 4
  • 1
    OP says the path don't have to exist, but that they could exist. – Wesley De Keirsmaeker Oct 24 '18 at 09:48
  • This solution indeed checks if the filepath also excists, which is not what I want. – BRHSM Oct 24 '18 at 09:50
  • 1
    It does *not* check if path exists on filesystem. Paths.get() simply creates a Path object if path is semantically valid. – Khurram Oct 24 '18 at 09:54
  • @Khurram sorry, I've seen this method before somewhere else, where they said it also checked for file excistance. Does this method also work if there is no file specified but only a directory? Regarding the spaces, the folders I tend to use don't contain spaces, therefor I want to check for that. But I guess I can find a separate solution for that – BRHSM Oct 24 '18 at 10:17