2013-04-30 19 views

Odpowiedz

10

!Files.exists() powraca:

  • true jeśli plik nie istnieje lub jego istnienie nie może być określona
  • false jeśli plik istnieje

Files.notExists() Powroty:

  • true jeśli plik nie istnieje
  • false jeśli plik istnieje, czy jego istnienie nie może być określona
1

Z Oracle docs z notExists.

Należy zauważyć, że ta metoda nie jest uzupełnieniem istniejącej metody. Gdzie nie można ustalić, czy plik istnieje, czy nie, obie metody zwracają fałsz. ...

Moje wyróżnienie.

3

Jak widzimy z Files.exists wynik zwracany jest:

TRUE if the file exists; 
FALSE if the file does not exist or its existence cannot be determined. 

I od Files.notExists wynik zwracany jest:

TRUE if the file does not exist; 
FALSE if the file exists or its existence cannot be determined 

Więc jeśli !Files.exists(path) powrót TRUE oznacza, że ​​nie istnieje lub istnienie nie może być określony (2 możliwości) i dla Files.notExists(path) return TRUE oznacza, że ​​nie istnieje (tylko 1 możliwość).

Podsumowanie !Files.exists(path) != Files.notExists(path) lub 2 possibilities not equals to 1 possibility (patrz powyższe wyjaśnienie dotyczące możliwości).

3

Poszukując kodu źródłowego różnic, obaj robią dokładnie to samo z jedną zasadniczą różnicą. Metoda notExist(...) ma dodatkowy wyjątek do złapania.

istnieje:

public static boolean exists(Path path, LinkOption... options) { 
    try { 
     if (followLinks(options)) { 
      provider(path).checkAccess(path); 
     } else { 
      // attempt to read attributes without following links 
      readAttributes(path, BasicFileAttributes.class, 
          LinkOption.NOFOLLOW_LINKS); 
     } 
     // file exists 
     return true; 
    } catch (IOException x) { 
     // does not exist or unable to determine if file exists 
     return false; 
    } 

} 

nie istnieje:

public static boolean notExists(Path path, LinkOption... options) { 
    try { 
     if (followLinks(options)) { 
      provider(path).checkAccess(path); 
     } else { 
      // attempt to read attributes without following links 
      readAttributes(path, BasicFileAttributes.class, 
          LinkOption.NOFOLLOW_LINKS); 
     } 
     // file exists 
     return false; 
    } catch (NoSuchFileException x) { 
     // file confirmed not to exist 
     return true; 
    } catch (IOException x) { 
     return false; 
    } 
} 

W efekcie różnice są następujące:

  • !exists(...) zwraca plik jako nie istnieje, jeśli IOException jest generowany podczas próby pobrania pliku.

  • notExists(...) powraca jako plik nie istnieje, upewniając się specyficzna IOException subexception NoSuchFileFound jest wyrzucane, oraz że nie ma innej subexception na IOException powodującej nie znaleziono wynik

+0

Witam, to jest poważne, jestem programistą PHP i wykonuję projekt w Javie. Tak więc nie ma prostego sposobu na ustalenie, czy folder istnieje i czy jest w 100% pewny. Używam JDK 6. Myślałem, że Java to dojrzały język ... – BojanT

0

Można Określ tylko absolutna ścieżka, jeśli katalog/katalogi nie zostaną zamknięte, utworzy dectory/katalogi.

private void createDirectoryIfNeeded(String directoryName) 
{ 
File theDir = new File(directoryName); 
if (!theDir.exists()) 
    theDir.mkdirs(); 
} 
Powiązane problemy