共計 412 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以使用 File 類的 exists() 方法來判斷某個路徑是否存在。示例如下:
import java.io.File;
public class CheckPathExists {public static void main(String[] args) {String path = "/path/to/file/or/directory";
File file = new File(path);
if(file.exists()) {System.out.println("The path exists.");
} else {System.out.println("The path does not exist.");
}
}
}
在上面的示例中,首先創建一個 File 對象,然后調用 exists() 方法來判斷該路徑是否存在。如果路徑存在,則打印 "The path exists.“;否則打印 "The path does not exist.”。
丸趣 TV 網 – 提供最優質的資源集合!
正文完