Validate a file name on Windows

后端 未结 11 2062
一生所求
一生所求 2020-11-29 23:28
public static boolean isValidName(String text)
{
    Pattern pattern = Pattern.compile(\"^[^/./\\\\:*?\\\"<>|]+$\");
    Matcher matcher = pattern.matcher(text         


        
11条回答
  •  不知归路
    2020-11-30 00:06

    Not sure how to implement it in Java (either Regex or own method). But, Windows OS has the following rules to create file/directory in the file system:

    1. Name is not only be Dots
    2. Windows device names like AUX, CON, NUL, PRN, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, cannot be used for a file name nor for the first segment of a file name (i.e. test1 in test1.txt).
    3. Device names are case insensitive. (i.e. prn, PRN, Prn, etc. are identical.)
    4. All characters greater than ASCII 31 to be used except "*/:<>?\|

    So, the program needs to stick with these rules. Hope, it covers the validation rules for your question.

提交回复
热议问题