一些对文件的操作
package TestCode5;
import java.io.File;
import java.io.IOException;
public class File_Test1 {
public static void main(String[] args) throws IOException {
File f = new File("D:\\Idea项目\\NewStudy\\1.txt");
// System.out.println(f.exists());
System.out.println("文件绝对路径:"+f.getAbsolutePath());
System.out.println("文件构造路径:"+f.getPath());
System.out.println("文件名称:"+f.getName());
System.out.println("文件长度:"+f.length()+"字节");
System.out.println("文件是否隐藏:"+f.isHidden());
File f2 = new File("src/TestCode3");
System.out.println("文件绝对路径:"+f2.getAbsolutePath());
System.out.println("文件构造路径:"+f2.getPath());
System.out.println("文件名称:"+f2.getName());
System.out.println("文件长度:"+f2.length()+"字节");
System.out.println("文件是否隐藏:"+f2.isHidden());
System.out.println("------------");
System.out.println("d:\\aaa 是否存在:"+f2.exists());
// 判断是文件还是目录
System.out.println("d:\\aaa 文件?:"+f2.isFile());
System.out.println("d:\\aaa 目录?:"+f2.isDirectory());
目录的创建
File f2= new File("newDir");
System.out.println("是否存在:"+f2.exists());// false
System.out.println("是否创建:"+f2.mkdir()); // true
System.out.println("是否存在:"+f2.exists());// true
System.out.println(f2.delete());
创建多级目录
File f3= new File("newDira\\newDirb");
System.out.println(f3.mkdir());// false
File f4= new File("newDira\\newDirb");
System.out.println(f4.mkdirs());// true
f3.delete();
f4.delete();
File f5 = new File("1.txt");
System.out.println(f5.createNewFile());
f5.delete();
//遍历文件夹
File f6 = new File("src/TestCode/");
String[] arr = f6.list();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
//遍历文件夹得到file对象的文件
File[] arr2 = f6.listFiles();
for (File file : arr2) {
System.out.println(file.getAbsoluteFile());
}
//
}
}
标签:文件,f2,12,19,System,2024,File,println,out
From: https://www.cnblogs.com/XYu1230/p/18620097