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
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