Java文件类以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。
File对象代表磁盘中实际存在的文件和目录。可以通过以下构造方法创建一个File对象:
public File(String pathName) public File(String parent,String child) public FIle(File parent,String child) public File(URI uri)
@Test
public void test1(){
// 构造器1:
File f1 = new File("Hello.txt");// 相对于当前Module的路径
File f2 = new File("D://code//Java//JavaSenior//Day07//Hello.txt");// 使用绝对路径
// 构造器2:
File f3 = new File("D://code//Java//JavaSenior","Day07");
File f4 = new File("D://code//Java//JavaSenior//Day07","hello.txt");
// 构造器3:
File f5 = new File(f3,"hello.txt");
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
System.out.println(f4);
System.out.println(f5);
}
@Test
public void test2(){
File f1 = new File("Hello.txt");
File f2 = new File("D://code//Java//IOTest");// 使用绝对路径
System.out.println(f1.getAbsoluteFile());
System.out.println(f1.getPath());
System.out.println(f1.getName());
System.out.println(f1.getParent());
System.out.println(f1.length());
System.out.println(f1.lastModified());
System.out.println("-----------------------");
System.out.println(f2.getAbsoluteFile());
System.out.println(f2.getPath());
System.out.println(f2.getName());
System.out.println(f2.getParent());
System.out.println(f2.length());
System.out.println(f2.lastModified());
}
输出结果:
D:/code/Java/JavaSenior/Day07/Hello.txt Hello.txt Hello.txt null 48 1573625332155 ----------------------- D:/code/Java/IOTest D:/code/Java/IOTest IOTest D:/code/Java 0 1573626319416
list()
方法和 listFiles()
方法的测试:
@Test
public void test3(){
File f = new File("D://code//Java//JavaSenior");
String[] list = f.list();
for(String s : list){
System.out.println(s);
}
System.out.println();
File[] files = f.listFiles();
for (File file : files){
System.out.println(file);
}
}
运行结果:
.idea Day01 Day02 Day03 Day04 Day05 Day06 Day07 JavaSenior.iml out src D:/code/Java/JavaSenior/.idea D:/code/Java/JavaSenior/Day01 D:/code/Java/JavaSenior/Day02 D:/code/Java/JavaSenior/Day03 D:/code/Java/JavaSenior/Day04 D:/code/Java/JavaSenior/Day05 D:/code/Java/JavaSenior/Day06 D:/code/Java/JavaSenior/Day07 D:/code/Java/JavaSenior/JavaSenior.iml D:/code/Java/JavaSenior/out D:/code/Java/JavaSenior/src
测试代码:
@Test
public void test5(){
File f1 = new File("Hello.txt"); // 文件
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
System.out.println(f1.exists());
System.out.println(f1.canRead());
System.out.println(f1.canWrite());
System.out.println(f1.isHidden());
}
运行结果:
false true true true true false
说明:要想renameTo返回True,那么重命名的源文件呢在硬盘中必须存在,重名后的文件在硬盘中不能存在。
@Test
public void test4(){
//要想renameTo方法返回true,那么需要f1在硬盘是存在的,且f2是不能存在硬盘的
File f1 = new File("Hello.txt");
File f2 = new File("D://code//Java//IOTest//hi.txt");
boolean isRename = f1.renameTo(f2);
System.out.println(isRename);
}
注意:如果你创建文件或者文件目录没有写盘符路径 ,那么 ,默认在项目路径下 。
@Test
public void test6() throws IOException {
// 文件的创建和删除
File f1 = new File("zabbix.txt");
if (!f1.exists()){
f1.createNewFile();
System.out.println("创建成功");
}else {
f1.delete();
System.out.println("删除成功");
}