Validate a file name on Windows

后端 未结 11 2054
一生所求
一生所求 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:26

    Well, I think the following method would guarantee a valid file name:

    public static boolean isValidName(String text)
    {
        try
        {
            File file = new File(text);
            file.createNewFile();
            if(file.exists()) file.delete();
            return true;
        }
        catch(Exception ex){}
        return false;
    }
    

    What do you think?

提交回复
热议问题