SORU
18 Temmuz 2011, PAZARTESİ


Doğrulamak Windows dosya adı

public static boolean isValidName(String text)
{
    Pattern pattern = Pattern.compile("^[^/./\\:*?\"<>|] $");
    Matcher matcher = pattern.matcher(text);
    boolean isMatch = matcher.matches();
    return isMatch;
}

Bu yöntem Windows üzerinde geçerli bir dosya adı garanti ediyor mu?

CEVAP
24 Temmuz 2011, Pazar


Şartları daha önce belirtilen verilen cited MSDN documentation aşağıdaki normal ifade oldukça iyi bir iş yapmak gerekir:

public static boolean isValidName(String text)
{
    Pattern pattern = Pattern.compile(
        "# Match a valid Windows filename (unspecified file system).          \n"  
        "^                                # Anchor to start of string.        \n"  
        "(?!                              # Assert filename is not: CON, PRN, \n"  
        "  (?:                            # AUX, NUL, COM1, COM2, COM3, COM4, \n"  
        "    CON|PRN|AUX|NUL|             # COM5, COM6, COM7, COM8, COM9,     \n"  
        "    COM[1-9]|LPT[1-9]            # LPT1, LPT2, LPT3, LPT4, LPT5,     \n"  
        "  )                              # LPT6, LPT7, LPT8, and LPT9...     \n"  
        "  (?:\\.[^.]*)?                  # followed by optional extension    \n"  
        "  $                              # and end of string                 \n"  
        ")                                # End negative lookahead assertion. \n"  
        "[^<>:\"/\\\\|?*\\x00-\\x1F]*     # Zero or more valid filename chars.\n"  
        "[^<>:\"/\\\\|?*\\x00-\\x1F\\ .]  # Last char is not a space or dot.  \n"  
        "$                                # Anchor to end of string.            ", 
        Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.COMMENTS);
    Matcher matcher = pattern.matcher(text);
    boolean isMatch = matcher.matches();
    return isMatch;
}

Bu düzenli ifade dosya adı uzunluğu herhangi bir sınırlama getirmez, ama gerçek bir dosya adı 260 veya 32767 karakter platforma bağlı olarak sınırlı olabilir unutmayın.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Fr. Eckle Studios

    Fr. Eckle St

    29 Kasım 2006
  • InsideBlackBerry

    InsideBlackB

    14 Aralık 2009
  • Miles Fisher

    Miles Fisher

    8 NİSAN 2009