File 类
文件(File)和目录(文件夹 Folder)路径名的抽象表示形式。
文件指的是一个路径的抽象表现形式
static String pathSeparator
与系统有关的路径分隔符,为了方便,它被表示为一个字符串。
static char pathSeparatorChar
与系统有关的路径分隔符。
static String separator
与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
static char separatorChar
与系统有关的默认名称分隔符。
D:\Java_0408\JavaSE\day05\代码
静态常量成员能够适配不同的操作系统
构造方法
public File(String pathname) pathname文件路径名
public File(String parent,String child) 父路径名 子路径名
public File(File parent,String child) 通过父文件对象和子文件名称构造文件对象
public class FileDemo01 {
public static void main(String[] args) throws IOException {
System.out.println(File.pathSeparator); // 路径分隔符 ;
System.out.println(File.separator); // 路径名称分隔符
String fileName = "D:\\Java_0408\\JavaSE\\day05\\代码";
String fileName2 = "D:" + File.separator + "JavaSE" + File.separator + "day05";
System.out.println(fileName2);
// public File(String pathname) pathname文件路径名
File f = new File("D:\\Java_0408\\JavaSE");
// public File(String parent,String child) 父路径名 子路径名
File f2 = new File("D:\\Java_0408", "JavaSE");
System.out.println(f2);
// public File(File parent,String child) 通过父文件对象和子文件名称构造文件对象
File f3 = new File(new File("D:\\Java_0408"), "JavaSE");
System.out.println(f3);
File f4 = new File("a.txt"); // 相对于系统工程目录下
// D:\Java_0408\CodeLibraryForJava0408\day0430_filedemo\a.txt
System.out.println(f4.getAbsolutePath());
System.out.println("createNewFile: " + f4.createNewFile());
// 类路径 src下面
File f5 = new File("src\\b.txt");
System.out.println("createNewFile: " + f5.createNewFile());
File f6 = new File("src/com/sxt/filedemo/FileDemo02.java");
System.out.println("createNewFile: " + f6.createNewFile());
System.out.println(f6.getAbsolutePath());
}
}
File的成员方法
文件的操作
1.基本操作
创建
public boolean createNewFile() 若文件不存在则创建新文件
public boolean mkdir() 创建目录 make Directory
public boolean mkdirs() 创建目录及不存在的父目录
删除
public boolean delete() 删除文件及空目录
重命名
查询
判断
获取
2.批量操作
批量创建
批量删除
批量修改
批量查询
需要具备的知识点:
1.字符串操作 【正则表达式】
2.递归操作
重要:
1.只要是对文件的操作,第一步一定要封装文件的路径
2.凡是封装文件路径,文件路径的父目录一定要存在
public class FileDemo02 {
public static void main(String[] args) throws IOException {
// 在工程目录下创建 hello.txt
File f = new File("hello.txt");
System.out.println("createNewFile: " + f.createNewFile());
// 在工程目录下创建 hello文件夹
File f2 = new File("helloworld");
System.out.println("mkdir: " + f2.mkdir());
// 在工程目录下创建 good/boy.txt文件夹
File f3 = new File("good");
File f4 = new File(f3, "boy.txt");
System.out.println("mkdir: " + f3.mkdir());
System.out.println("createNewFile: " + f4.createNewFile());
// 在工程目录下创建 aaa/bbb/ccc/ddd/eee.txt
File f5 = new File("aaa/bbb/ccc/ddd");
File f6 = new File(f5, "eee.txt");
System.out.println("mkdirs: " + f5.mkdirs());
System.out.println("createNewFile: " + f6.createNewFile());
}
}
public class FileDemo03 {
public static void main(String[] args) throws IOException {
File f = new File("good/boy.txt");
System.out.println("delete: " + f.delete());
File f2 = new File("good");
System.out.println("delete: " + f2.delete());
File f3 = new File("aaa");
System.out.println("delete: " + f3.delete());
}
}
/*
文件的相关属性功能
public boolean isFile() 是否文件
public boolean isDirectory() 是否目录
public boolean canRead() 是否可读
public boolean canWrite() 是否可写
public boolean exists() 是否存在
public boolean isHidden() 是否隐藏
public long length() 长度
public String getAbsolutePath() 绝对路径
public String getPath() 定义的路径
public String getName() file名字
public long lastModified() 最后一次修改时间
*/
public class FileDemo06 {
public static void main(String[] args) {
File f = new File("aaa/bbb/ccc/ddd/eee.txt");
System.out.println("isFile: " + f.isFile()); // true
System.out.println("isDirectory: " + f.isDirectory()); // false
System.out.println("canRead: " + f.canRead()); // true
System.out.println("canWrite: " + f.canWrite()); // true
System.out.println("isHidssden: " + f.isHidden()); // false
System.out.println("length: " + f.length()); // 8
System.out.println("getAbsolutePath: " + f.getAbsolutePath());
// D:\Java_0408\CodeLibraryForJava0408\day0430_filedemo\aaa\bbb\ccc\ddd\eee.txt
System.out.println("getPath: " + f.getPath()); // aaa/bbb/ccc/ddd/eee.txt
System.out.println("getName: " + f.getName()); // eee.txt
System.out.println("lastModified: " + f.lastModified()); // 上次修改时间
// 1556596186514
Date d = new Date(1556596186514L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(d);
System.out.println(format);
}
}