Validate a file name on Windows

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

    Not enough,in Windows and DOS, some words might also be reserved and can not be used as filenames.

    CON, PRN, AUX, CLOCK$, NUL
    COM0, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9
    LPT0, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
    

    See~

    http://en.wikipedia.org/wiki/Filename


    Edit:

    Windows usually limits file names to 260 characters. But the file name must actually be shorter than that, since the complete path (such as C:\Program Files\filename.txt) is included in this character count.

    This is why you might occasionally encounter an error when copying a file with a very long file name to a location that has a longer path than its current location.

提交回复
热议问题