简体   繁体   English

验证字符串输入可以是有效路径

[英]Validate String input COULD be a valid path

I wish to validate a string path. 我希望验证一个字符串路径。 I don't want to check that the path exists or create the path (that includes create + then delete), I simply wish to check that the input string COULD be a validate path on the executing system. 我不想检查路径是否存在或创建路径(包括create + then delete),我只想检查输入字符串COULD是执行系统上的验证路径。

So far I have been messing with the File class with no luck. 到目前为止,我一直在搞乱File类,没有运气。 I would expect the following to fail on my OSX machine but it doesn't: 我希望我的OSX机器上的以下内容失败,但它没有:

File f = new File("!@£$%^&*()±§-_=+[{}]:;\"'|>.?/<,~`±");
System.out.println(f.getCanonicalPath());

Is there anything that will do this for me? 有什么能帮我的吗?

You can do it via regex: File path validation in javascript 你可以通过正则表达式来实现: javascript中的文件路径验证

Or by checking if the parent of the path exists: Is there a way in Java to determine if a path is valid without attempting to create a file? 或者通过检查路径的父节点是否存在: Java中是否有办法确定路径是否有效而不尝试创建文件?

Just note that the path depends on the OS: https://serverfault.com/questions/150740/linux-windows-unix-file-names-which-characters-are-allowed-which-are-unesc 请注意,路径取决于操作系统: https//serverfault.com/questions/150740/linux-windows-unix-file-names-which-characters-are-allowed-which-are-unesc

Also, just because the path is valid, it doesn't mean the file can be written there. 另外,仅仅因为路径有效,并不意味着可以在那里写入文件。 For example, in Linux, you need to be a super user to write to /usr/ 例如,在Linux中,您需要成为超级用户才能写入/ usr /

You could choose to use regex to check the path. 您可以选择使用正则表达式来检查路径。 Regex would look like: 正则表达式看起来像:

^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$

And an example can be seen here at Regexr . Regexr就是一个例子。 You can check this in Java using the String.matches(regex) function. 您可以使用String.matches(regex)函数在Java进行检查。 An example below: 以下示例:

public static void main(String[] args) throws Exception {

    String regex = "^(?:[a-zA-Z]\\:|\\\\\\\\[\\w\\.]+\\\\[\\w.$]+)\\\\(?:[\\w]+\\\\)*\\w([\\w.])+$";
    String path = "c:\\folder\\myfile.txt";

    System.out.println(path.matches(regex));

}

( Note that the regex looks significantly longer due to having to escape the \\ characters ). 请注意,由于必须转义\\字符,正则表达式看起来要长得多 )。 Just call yourPathString.matches(regex) , and it will return true if it is a valid path. 只需调用yourPathString.matches(regex) ,如果它是有效路径,它将返回true。

If the path is valid, then at least one of the chain of parent files must exist. 如果路径有效,则必须至少存在一个父文件链。 If no parent exists, then it must be invalid. 如果不存在父级,则它必须无效。

public static boolean isValidPath(File file) throws IOException {
    file = file.getCanonicalFile();

    while (file != null) {
        if (file.exists()) {
            return true;
        }

        file = file.getParentFile();
    }

    return false;
}

System.out.println(isValidPath(new File("/Users/something")));  // true (OS X)
System.out.println(isValidPath(new File("asfq34fawrf")));  // false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM