How to convert path to valid file path in Java 1.7

Using Java 1.6 The file path can be entered by the user, and then I use various regular expressions to remove platform-invalid characters (such as "?" On Windows) and check the path length to make sure that we are done with a valid file path for OS before trying to create a file path.

But there are two problems:

  • His pain is that which is valid or not for each platform.
  • I make assumptions based on the default file system for the platform, but of course the OSX system can write to a file system other than mac, such as FAT32, in which case these checks will not be valid.

So, I was hoping there would be a better way to do this with NIO2 in Java 7, but have not yet found a solution, is there one?

+4
source share
3 answers

Depending on your expected result (corrected line? Invalid character position? Exception?), This should give you an idea of ​​what you can do:

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); } } } 

Hope this helps.

+4
source

The key to your question is the phrase "remove characters that are not valid for the platform." Various String to Path conversion functions, such as get() and resolve() , will tell you if the string was valid as a path, but it will not indicate why it is not valid. One way to be invalid is to contain invalid characters. Another would be, say, too many slash characters. Despite this, the library does not provide more information than this; it does not provide an opportunity to assist in verifying user input in any way that will help the user correct an input error. Admittedly, this should be standard practice, but it is hardly a practice at all.

Upshot: you will need to write such a validation library yourself if you want to use it. Potential: Of course, you are not the only person with such a problem.

+2
source

I think you should look at Path.getPath

 public static Path get(String first, String... more) getPath("/foo","bar","gus")-->/foo/bar/gus 

Converts a path string or sequence of strings that, when combined, form a path string into Path. If no more elements are specified, then the value of the first parameter is the path string to convert. If one or more elements indicates more, each non-empty line, including the first, is considered a sequence of name elements (see Path) and combined to form a path line. The details of how strings are concatenated are specific providers, but they will usually be concatenated using a name separator as a separator. For example, if the name separator is "/" and getPath ("/ foo", "bar", "gus"), then the path string "/ foo / bar / gus" is converted to Path. A path representing an empty path is returned if it is first an empty string and no longer contains any non-empty string.

+1
source

Source: https://habr.com/ru/post/1438706/


All Articles