java最大的成功就是具有很强的可读性,基本每个类所对应的英文就是它要实现的内容。
File文件操作很简单,能找到当前文件的路径。虽然一下程序没有实现这个功能,但是想信只要你愿意,这就是一种通用的方式。
import java.io.File;
import java.io.IOException;
/**File文件操作*/
public class FileControlDemo {
public static void main(String[] args) {
try{
File file=new File(".");
String path=file.getCanonicalPath();
String AbsolutePath=file.getAbsolutePath();
System.out.println("当前路径:"+path);
System.out.println("绝对路径:"+AbsolutePath);
File dir=new File("register");
//File文件中很多方法都是返回Boolean类型的结果。
System.out.println("相对路径:"+dir.exists());
if(!dir.exists()){
dir.mkdirs();//创建路径
}
File file1=new File(dir,"register.txt");//通过路径获取文件
if(!file1.exists()){
file.createNewFile();//条件满足创建register.txt
}else{
file.canExecute();//执行文件
}
System.out.println(file.getParent());
System.out.println("是否可执行:"+file.canExecute());
System.out.println("是否可读:"+file.canRead());
System.out.println("获取当前文件:"+file.getAbsoluteFile());
file.delete();
dir.delete();
//file.deleteOnExit();//删除文件并退出
//dir.delete();//删除路径并退出
}catch(IOException e){
e.getMessage();
throw new RuntimeException(e);
}
}
}
这个程序当然不是很难,甚至可以说是超级简单,但是我觉得不管怎样只有亲自试一下,调试一下,才会真的理解File类