共計 390 個字符,預計需要花費 1 分鐘才能閱讀完成。
在 Java 中,可以使用 File 類的 exists() 方法來判斷路徑是否存在。具體代碼如下:
import java.io.File;
public class PathExist {public static void main(String[] args) {
String path = "C:\\path\\to\\file.txt";
File file = new File(path);
if (file.exists()) {System.out.println("路徑存在");
} else {System.out.println("路徑不存在");
}
}
}
在上述代碼中,我們創建一個 File 對象,傳入要判斷的路徑,然后使用 exists() 方法來判斷路徑是否存在。如果路徑存在,exists() 方法會返回 true,否則返回 false。根據返回值,我們可以對路徑是否存在進行相應的操作。
丸趣 TV 網 – 提供最優質的資源集合!
正文完