public class Fileextends Objectimplements Serializable, Comparable<File>文件和目录路径名的抽象表示形式。
java把电脑中的文件和文件夹(目录)封装为一个File类,我们可以使用File类对文件和文件夹进行操作
常见操作有创建文件(夹)、删除文件(夹)、获取文件(夹)、判断文件(夹是否存在)、遍历文件夹、获取文件大小
File类是一个与操作系统无关的类,任何操作系统都可以使用这个类中的方法
【注】 记住三个单词:
1. file(文件)
2. directory(目录/文件夹)
3. path(路径)
static String pathSeparator 与系统有关的路径分隔符,为了方便,它被表示为一个字符串。
static char pathSeparatorChar 与系统有关的路径分隔符。
static String separator 与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
static char separatorChar 与系统有关的默认名称分隔符。
操作路径:路径不能写死了
String pathSeparator = File.pathSeparator;
System.out.println(pathSeparator);
String separator = File.separator;
System.out.println(separator);
路径分隔符:
文件名分隔符:
File(File parent, String child) 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String parent, String child) 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
File(String pathname)测试String pathname:字符串是路径名称
创建File对象,只是把字符串路径封装为File对象,不考虑路径的真假情况
private static void show01(){
File f1 = new File("D:\\_develop\\code\\itheima\\shuangyuan\\a.txt");
System.out.println(f1); // D:\_develop\code\itheima\shuangyuan\a.txt (重写了toString方法)
File f2 = new File("D:\\_develop\\code\\itheima\\shuangyuan");
System.out.println(f2);// D:\_develop\code\itheima\shuangyuan
File f3 = new File("b.txt");
System.out.println(f3); // b.txt
}
File(String parent, String child)测试好处: 父路径和子路径可以单独书写,使用起来非常灵活;父路径和子路径都可以变化
private static void show02(String parent, String child){
File file = new File(parent, child);
System.out.println(file); // c:\a.txt
}
File(File parent, String child)测试private static void show03() {
File parent = new File("d:\\");
File file = new File(parent, "hello.java");
System.out.println(file); // d:\hello.java
}
public String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串
public String getPath() 将此抽象路径名转换为一个路径名字符串
public String getName() 返回由此抽象路径名表示的文件或目录的名称
public long length() 返回由此抽象路径名表示的文件的长度
public String getAbsolutePath()返回此File的绝对路径名字符串。private static void show01() {
File f1 = new File("D:\\_develop\\code\\itheima\\shuangyuan\\a.txt");
String absolutePath1 = f1.getAbsolutePath();
System.out.println(absolutePath1);// D:\_develop\code\itheima\shuangyuan\a.txt
File f2 = new File("a.txt");
String absolutePath2 = f2.getAbsolutePath();
System.out.println(absolutePath2);// D:\_develop\code\itheima\shuangyuan\a.txt
}
putlic String getPath() 将此File转换为路径名字符串private static void show02() {
File f1 = new File("D:\\_develop\\code\\itheima\\shuangyuan\\a.txt");
File f2 = new File("a.txt");
String path1 = f1.getPath();
System.out.println(path1); // D:\_develop\code\itheima\shuangyuan\a.txt
String path2 = f2.getPath();
System.out.println(path2); // a.txt
System.out.println(f1); // D:\_develop\code\itheima\shuangyuan\a.txt
System.out.println(f1.toString()); // D:\_develop\code\itheima\shuangyuan\a.txt
System.out.println(f2); // a.txt
System.out.println(f2.toString());// a.txt
}
public String getName() 返回由此抽象路径名表示的文件或目录的名称private static void show03() {
File f1 = new File("D:\\_develop\\code\\itheima\\shuangyuan\\a.txt");
String name1 = f1.getName();
System.out.println(name1); // a.txt
File f2 = new File("D:\\_develop\\code\\itheima\\shuangyuan");
String name2 = f2.getName();
System.out.println(name2); // shuangyuan
}
public long length() 返回由此File表示的文件长度。private static void show04() {
File f1 = new File("D:\\_develop\\code\\itheima\\shuangyuan\\1.png");
long l1 = f1.length();
System.out.println(l1); // 14903字节
File f2 = new File("D:\\_develop\\code\\itheima\\shuangyuan\\2.png");
System.out.println(f2.length()); // 0
File f3 = new File("D:\\_develop\\code\\itheima\\shuangyuan");
System.out.println(f3.length()); // 0 文件夹没有大小概念
}
public boolean exists() 测试此抽象路径名表示的文件或目录是否存在。
public boolean isDirectory() 测试此抽象路径名表示的文件是否是一个目录。
public boolean isFile() 测试此抽象路径名表示的文件是否是一个标准文件。
public boolean exists() 测试此抽象路径名表示的文件或目录是否存在。 private static void show01() {
File f1 = new File("D:\\_develop\\code\\itheima\\shuangyuan");
File f2 = new File("D:\\_develop\\code\\itheima\\shuang");
File f3 = new File("1.png");
File f4 = new File("2.png");
System.out.println(f1.exists()); // true
System.out.println(f2.exists()); // false
System.out.println(f3.exists()); // true
System.out.println(f4.exists()); // false
}
public boolean isDirectory()和public boolean isFile()private static void show02() {
File f1 = new File("D:\\_develop\\code\\itheima\\shuang");
if(f1.exists()){
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
}
File f2 = new File("D:\\_develop\\code\\itheima\\shuangyuan");
if (f2.exists()){
System.out.println(f2.isDirectory()); // true
System.out.println(f2.isFile()); // false
}
File f3 = new File("1.png");
if(f3.exists()){
System.out.println(f3.isDirectory()); // false
System.out.println(f3.isFile()); // true
}
}public boolean createNewFile() throws IOException当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
public boolean delete()删除此抽象路径名表示的文件或目录。
public boolean mkdir()创建此抽象路径名指定的目录。
public boolean mkdirs()创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
public boolean createNewFile() throws IOException 创建文件的路径和名称在构造方法中给出(构造参数)返回值:
private static void show01() throws IOException {
File f1 = new File("D:\\_develop\\code\\itheima\\shuangyuan\\08_FileAndRecurison\\1.txt");
boolean b1 = f1.createNewFile();
System.out.println(b1);
File f2 = new File("08_FileAndRecurison\\2.txt");
System.out.println(f2.createNewFile());
File f3 = new File("08_FileAndRecurison\\新建文件夹");
System.out.println(f3.createNewFile());
File f4 = new File("08_FileAndR\\1.txt");
System.out.println(f4.createNewFile()); // 路径不存在,抛出IOException
}
public boolean mkdir()和public boolean mkdirs()创建文件夹的路径和名称在构造方法中给出(构造参数)
返回值:boolean
注意:
private static void show02() {
File f1 = new File("08_FileAndRecurison\\aaa");
boolean b1 = f1.mkdir();
System.out.println(b1);
File f2 = new File("08_FileAndRecurison\\111\\222\\333\\444");
boolean b2 = f2.mkdirs();
System.out.println(b2);
File f3 = new File("08_FileAndRecurison\\abc.txt");
boolean b3 = f3.mkdir();
System.out.println(b3);
File f4 = new File("08_FileAnd\\ccc");
boolean b4 = f4.mkdir(); // 不会抛出异常,也不会创建文件夹
System.out.println(b4);
}
public boolean delete()删除构造方法路径中给出的文件/文件夹返回值:boolean
注意:
private static void show03() {
File f1 = new File("08_FileAndRecurison\\新建文件夹");
boolean b1 = f1.delete();
System.out.println(b1);
File f2 = new File("08_FileAndRecurison\\abc.txt");
System.out.println(f2.delete());
}
public String[] list()返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录
public File[] listFiles()返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件
注意:
## 7.1 public String[] list()
private static void show01() {
File file = new File("D:\\_develop\\code\\itheima\\shuangyuan\\08_FileAndRecurison");
// File file = new File("D:\\_develop\\code\\itheima\\shuangyuan\\08_FileAn");
// File file = new File("D:\\_develop\\code\\itheima\\shuangyuan\\08_FileAndRecurison\\1.txt");
String[] arr = file.list();
for(String fileName : arr){
System.out.println(fileName);
}
}
private static void show02() {
File file = new File("D:\\_develop\\code\\itheima\\shuangyuan\\08_FileAndRecurison");
File[] files = file.listFiles();
for (File f : files){
System.out.println(f);
}
}原文:https://www.cnblogs.com/Aboy/p/12168673.html