我们先来看完整的代码:
import java.io.file; public class judgefile { public static void main(string[] args) { file dir = new file("d:/"); //声明d磁盘 file file = new file(dir,"test"); //声明d磁盘根目录下名为test的文件 boolean d=dir.exists(); boolean f=file.exists(); if(d==true){ system.out.println(dir.getabsolutepath()+"目录存在"); } else{ system.out.println(dir.getabsolutepath()+"目录不存在"); } if(f==true){ system.out.println(file.getabsolutepath()+"文件存在"); } else{ system.out.println(file.getabsolutepath()+"文件不存在"); } } }
说明:
exists() 方法来检测文件或目录是否存在
getabsolutepath()方法打印磁盘路径
如果不声明目录,例如file file = new file(“test”); 默认路径为java项目夹的路径
如对本文有疑问, 点击进行留言回复!!
解决idea中出现“illegal character U+200B” 问题
荐 为什么加了@Transactional注解,事务没有回滚?
Attribute ‘sklearn.linear_model._logistic.LogisticRegression.multi_class‘ must be explicitly set to
Java/Python实现 LeetCode剑指Offer 14-I.剪绳子(动态规划)
网友评论