java判断文件是否存在

如题所述

import java.io.File;

public class $ {

    public static void main(String[] args) {

        File f = new File("d:/a.txt");
        System.out.println(f.exists());
    }
}

false.不存在

true.存在

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-02-19
File类中有个方法public boolean exists()

测试此抽象路径名表示的文件或目录是否存在。

返回:
当且仅当此抽象路径名表示的文件或目录存在时,返回 true;否则返回 false
第2个回答  2014-02-19
if(new File("d:/test.txt").exists()){
System.out.println("文件已经存在");
}
第3个回答  2014-02-19
file.exists()