前文:对File相关知识的简单整理,主要用于自我复习.
一、File类的概述
1.java.io.File 类是文件和目录路径名的抽象表示形式,主要用于文件和目录的创建、查找和删除等操作.
二、构造方法
1).通过将给定的路径名字字符串转换为抽象路径名来创建新的File实例.
public class Demo1 {
public static void main(String[] args) {
String pathname = "D:\\aaa.txt";
File file1 = new File(pathname);
System.out.println(file1.getPath());//获取文件路径,结果就是"d:\aaa.txt".
}
}
2).从父路径名字符串和子路径名字符串创建新的File实例.
public class Demo1 {
public static void main(String[] args) {
String parent = "d:\\aaa";
String child = "bbb.txt";
File file2 = new File(parent, child);
System.out.println(file2.getPath());//输出路径是"d:\aaa\bbb.txt"
}
}
3).从父抽象路径名和子路径名字符串创建新的File实例.
public class Demo1 {
public static void main(String[] args) {
File parentDir = new File("d:\\aaa");
String child = "bbb.txt";
File file3 = new File(parentDir, child);
System.out.println(file3.getPath());//输出路劲是"d:\aaa\bbb.txt"
}
}
Tips:
1. ⼀个File对象代表硬盘中实际存在的⼀个文件或者目录。
2. 无论该路径下是否存在文件或者目录,都不影响 File 对象的创建。
三、常用方法
以该文件为例
1).返回文件的绝对路径名字符串
public String getAbsolutePath();
public class Demo1 {
public static void main(String[] args) {
File file = new File("d:\\aaa\\bbb.txt");
System.out.println(file3.getAbsolutePath());//结果为"d:\aaa\bbb.txt"
}
}
2).将此文件转换为路径字符串
public String getPath();
public class Demo1 {
public static void main(String[] args) {
File file = new File("d:\\aaa\\bbb.txt");
file.getPath();
System.out.println(file);//结果为"d:\aaa\bbb.txt"
}
}
3).返回由此File表示的文件或目录的名称
public String getName();
public class Demo1 {
public static void main(String[] args) {
File file = new File("d:\\aaa\\bbb.txt");
System.out.println(file.getName());//结果为"bbb.txt".
}
}
4).返回由此File表示的文件的长度
public long length();
public class Demo1 {
public static void main(String[] args) {
File file = new File("d:\\aaa\\bbb.txt");
System.out.println(file.length());//结果为"12".
}
}
4).判断此File表示的文件或目录是否实际存在
public boolean exists();
public class Demo1 {
public static void main(String[] args) {
File file = new File("d:\\aaa\\bbb.txt");
System.out.println(file.exists());//结果为"true".
}
}
5).判断此File表示的是否为目录
public boolean isDirectory();
public class Demo1 {
public static void main(String[] args) {
File file = new File("d:\\aaa\\bbb.txt");
System.out.println(file.isDirectory());//结果为"false".
}
}
6).判断此File表示的是否为文件
public boolean isFile();
public class Demo1 {
public static void main(String[] args) {
File file = new File("d:\\aaa\\bbb.txt");
System.out.println(file.isFile());//结果为"true".
}
}
7).当且仅当具有该名称的文件尚不存在时,创建⼀个新的空文件,若文件已存在不会做任何操作.
public boolean createNewFile();
public class Demo1 {
public static void main(String[] args) throws IOException {
File file = new File("d:\\aaa\\bbb.txt");
File file1= new File("d:\\aaa\\bbb1.txt");
file1.createNewFile();//目录下成功生成bbb1.txt
}
}
8).删除由此File表示的文件或目录
public void delete();
public class Demo1 {
public static void main(String[] args) throws IOException {
File file = new File("d:\\aaa\\bbb1.txt");
file.delete();//成功删除bbb1.txt
}
}
9).1.返回一个String数组,表示该File目录中的所有子文件或目录
public String[] list();
2.返回一个File数组,表示该File目录中的所有的子文件或目录
public File[] listFiles();
public class Demo1 {
public static void main(String[] args) {
File dir = new File("d:\\aaa");
// 获取当前⽬录下的⽂件以及⽂件夹的名称。
String[] names = dir.list();
for (String name : names) {
System.out.println(name);//输出结果为bbb.txt
}
// 获取当前⽬录下的⽂件以及⽂件夹对象,只要拿到了⽂件对象,那么就可以获取更多信息
File[] files = dir.listFiles();
for (File file : files) {
System.out.println(file);//输出结果为d:\aaa\bbb.txt
}
}
}