java中可以使用file.exists()方法来检测文件是否存在,public boolean exists()测试此抽象路径名表示的文件或目录是否存在。
java判断文件是否存在的方法:
public static void main(string[] args) { file file = new file("g:\\jeff.txt"); file dir = new file("g:\\jeff"); // 判断文件是否存在 if (file.exists()) { system.out.println("文件已存在"); } else { system.out.println("文件不存在"); try { file.createnewfile(); system.out.println("创建文件"); } catch (ioexception e) { e.printstacktrace(); } } }
java文件类以抽象的方式代表文件名和目录路径名。file类主要用于文件和目录的创建、文件的查找和文件的删除等。
更多java知识请关注java基础教程。
以上就是java判断文件是否存在的详细内容。