
以下实例演示了运用 File 类的 file.exists() 要领来检测文件是不是存在:
import java.io.File; public class Main { public static void main(String[] args) { File file = new File("C:/java.txt"); System.out.println(file.exists()); } }
运转效果为(假如你的 C 盘中存在文件 java.txt):
true
exists():
public boolean exists()测试此笼统路径名示意的文件或目次是不是存在。
返回:当且仅当此笼统路径名示意的文件或目次存在时,返回true;不然返回false;
抛出:SecurityExcepTIon假如存在平安管理器,且其SecurityManager.checkRead(java.lang.String)要领谢绝对文件或目次举行写访问。
更多java学问请关注java基础教程栏目。
以上就是java读取文件是不是存在的要领的细致内容,更多请关注ki4网别的相干文章!