1. File类概述
java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。
java把电脑中的文件和文件夹(目录)封装为了一个File类,我们可以使用File类对文件和文件夹进行操作
我们可以使用File类的方法
- 创建一个文件/文件夹
- 删除文件/文件夹
- 获取文件/文件夹
- 判断文件/文件夹是否存在
- 对文件夹进行遍历
- 获取文件的大小
File类是一个与系统无关的类 , 任何的操作系统都可以使用这个类中的方法
重点:记住这三个单词
- file:文件
- directory:文件夹/目录
- path:路径
2. File类的静态成员变量
- static String pathSeparator与系统有关的路径分隔符,为了方便,它被表示为一个字符串。
- static char pathSeparatorChar 与系统有关的路径分隔符。
- static String separator与系统有关的默认名称分隔符,为了方便,它被表示为-个字符串。
- static char separatorChar与系统有关的默认名称分隔符。
package cn.itcast.day13.demo01;
import java.io.File;
public class File01 {
public static void main(String[] args) {
String pathSeparator = File.pathSeparator;
System.out.println(pathSeparator);//路径分隔符 windows:分号; linux:冒号:
String separator = File.separator;
System.out.println(separator); //文件名称分隔符, windows:反斜杠\ Linux:正斜杠/
}
}
执行结果
;
\
3. 绝对路径和相对路径
绝对路径:是一个完整的路径以盘符(c:,D: )开始的路径
- C: \\a.txt
- C: \\userslitcast\\IdeaProjectsl\shungyuan\\123. txt
- D: \\demollb. txt
相对路径:是一个简化的路径
相对指的是相对于当前项目的根目录,如果使用当前项目的根目录,路径可以简化书写
C:\\Users\\itcastl\\IdeaProjectsllshungyuan\\123. txt简化为: 123. txt(可以省略项目的根目录)
注意:
- 路径是不区分大小写
- 路径中的文件名称分隔符windows使用反斜杠,反斜杠是转义字符,两个反斜杠代表一个普通的反斜杠
4. File类的构造方法
- File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新File 实例。
参数:
- String pathname :字符串的路径名称
- 路径可以是以文件结尾,也可以是以文件夹结尾,
- 路径可以是相对路径,也可以是绝对路径
- 路径可以是存在,也可以是不存在
创建File对象,只是把字符串路径封装为File对象,不考虑路径的真假情况
package cn.itcast.day13.demo01;
import java.io.File;
public class File02 {
public static void main(String[] args) {
show01();
}
private static void show01() {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");
System.out.println(f1);//重写了object类的方法
File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");
System.out.println(f2);
File f3 = new File("b.txt");
System.out.println(f3);
}
}
执行结果
C:\Users\itcast\IdeaProjects\shungyuan\a.txt
C:\Users\itcast\IdeaProjects\shungyuan
b.txt
- File(String parent, String child) 根据parent 路径名字符串和child 路径名字符串创建一个新File 实例。
参数:
- 把路径分成了两部分
- String parent :父路径
- String child:子路径
好处:
父路径和子路径,可以单独书写,使用起来非常灵活;父路径和子路径都可以变化
import java.io.File;
public class File02 {
public static void main(String[] args) {
show02("c:\\","a.txt");
}
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) 根据parent 抽象路径名和child 路径名字符串创建一个新File 实例。
参数:
- 把路径分成了两部分
- File parent:父路径
- String child:子路径
好处: - 父路径和子路径,可以单独书写,使用起来非常灵活;父路径和子路径都可以变化
- 父路径是File类型,可以使用File的方法对路径进行一-些操作,再使用路径创建对象
import java.io.File;
public class File02 {
public static void main(String[] args) {
show03();
}
private static void show03() {
File parent = new File("c:\\");
File file = new File(parent,"hello.java");
System.out.println(file);
}
}
执行结果
c:\hello.java
5. File类的常用方法
5.1 获取功能的方法
- public String getAbsolutePath() : 返回此File的绝对路径名字符串。
- public String getPath() : 将此File转换为路径名字符串。
- public String getName(): 返回由此File表示的文件或目录的名称。
- public long lerdgth() : 返回由此File表示的文件的长度。
package cn.itcast.day13.demo01;
import java.io.File;
public class File03 {
public static void main(String[] args) {
show01();
System.out.println("---------------");
show02();
System.out.println("---------------");
show03();
System.out.println("---------------");
show04();
}
/*
public long length() : 返回由此File表示的文件的长度。
获取的是构造方法指定的文件的大小,以字节为单位
注意:
文件夹是没有大小概念的,不能获取文件夹的大小
如果构造方法中给出的路径不存在,那么length方法返回0
*/
private static void show04() {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\7.jpg");
long l1 = f1.length();
System.out.println(l1);
File f2 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\8.jpg");
System.out.println(f2.length());//0
File f3 = new File("C:\\Users\\x\\Desktop\\All of D");
System.out.println(f3.length());
}
/*
public String getName() : 返回由此File表示的文件或目录的名称。
获取的就是构造方法传递路径的结尾部分(文件/文件夹)
*/
private static void show03() {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\7.jpg");
String name1 = f1.getName();
System.out.println(name1);
File f2 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding");
String name2 = f2.getName();
System.out.println(name2);
}
/*
public String getPath() :将此File转换为路径名字符串。
获取的构造方法中传递的路径
tostring方法调用的就是getPath方法
源码:
public String toString() {
return getPath();
*/
private static void show02() {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\7.jpg");
File f2 = new File("7.jpg");
String path1 = f1.getPath();
System.out.println(path1);//C:\Users\x\Desktop\All of D\java_coding\7.jpg
String path2 = f2.getPath();
System.out.println(path2);//7.jpg
System.out.println(f1);//C:\Users\x\Desktop\All of D\java_coding\7.jpg
System.out.println(f1.toString());//C:\Users\x\Desktop\All of D\java_coding\7.jpg
}
/*
public string getAbsolutePath() : 返回此File的绝对路径名字符串。
获取的构造方法中传递的路径
无论路径是绝对的还是相对的,getAbsolutePath方法返回的都是绝对路径
*/
private static void show01() {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\7.jpg");
String absolutePath1 = f1.getAbsolutePath();
System.out.println(absolutePath1);
File f2 = new File("7.jpg");
String absolutePath2 = f2.getAbsolutePath();
System.out.println(absolutePath2);
}
}
执行结果
C:\Users\x\Desktop\All of D\java_coding\7.jpg
C:\Users\x\Desktop\All of D\java_coding\7.jpg
---------------
C:\Users\x\Desktop\All of D\java_coding\7.jpg
7.jpg
C:\Users\x\Desktop\All of D\java_coding\7.jpg
C:\Users\x\Desktop\All of D\java_coding\7.jpg
---------------
7.jpg
java_coding
---------------
10272
0
4096
5.2 判断功能的方法
File判断功能的方法
- public boolean exists() :此File表示的文件或目录是否实际存在。
- public boolean isDirectory() :此File表示的是否为目录。
- public boolean isFile() :此File表示的是否为文件。
package cn.itcast.day13.demo01;
import java.io.File;
public class File04 {
public static void main(String[] args) {
show01();
System.out.println("----------");
show02();
}
private static void show02() {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\7.jpg");
if(f1.exists()){
System.out.println(f1.isFile());
System.out.println(f1.isDirectory());
}else System.out.println("000000000");
}
private static void show01() {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\7.jpg");
System.out.println(f1.exists());//true
File f2 = new File("C:\\Users\\x\\Desktop\\All of D");
System.out.println(f2.exists());//true
File f3 = new File("7.jpg");
System.out.println(f3.exists());//true
File f4 = new File("C:\\Users\\x\\Desktop\\All of D\\java_code");
System.out.println(f4.exists());//false
}
}
执行结果
true
true
true
false
----------
true
false
5.3 File类创建和删除功能的方法
- public boolean createNewFile(): 当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。
- public boolean delgte() :删除由此File表示的文件或目录。
- public boolean mkdir() : 创建由此File表示的目录。
- public boolean mkdirs() :创建由此File表示的目录,包括任何必需但不存在的父目录。
public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,创建一个新的空文件 。
创建文件的路径和名称在构造方法中给出(构造方法的参数)
- 返回值:布尔值
- true :文件不存在,创建文件,返回true
- false:文件存在, 不会创建,返回false
注意:
- 此方法只能创建文件,不能创建文件夹
- 创建文件的路径必须存在,否则会抛出异常
public boolean createNewFile() throws IOException
createNewFile声明抛出了IOExcept ion,我们调用这个方法,就必须的处理这个异常,要么throws ,要么trycatch
mport java.io.File;
import java.io.IOException;
public class File05 {
public static void main(String[] args) throws IOException {
show01();
}
private static void show01() throws IOException {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\aa.txt");
boolean b1 = f1.createNewFile();
System.out.println(b1);
}
}
执行结果
true
public boolean mkdir() :创建单级空文件夹
public boolean mkdirs() :既可以创建单级空文件夹,也可以创建多级文件夹
创建文件夹的路径和名称在构造方法中给出(构造方法的参数)
- 返回值:布尔值
- true:文件夹不存在,创建文件夹,返回true
- false:文件夹存在,不会创建,返回false;构造方法中给出的路径不存在返回false
注意: 此方法只能创建文件夹,不能创建文件
import java.io.File;
public class File05 {
public static void main(String[] args) throws IOException {
show02();
}
private static void show02() {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\aaa");
boolean b1 = f1.mkdir();
System.out.println(b1);
File f2 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\aaaa\\bbbb\\ccc");
boolean b2 = f2.mkdirs();
System.out.println(b2);
}
执行结果
true
true
public boolean delete() :删除由此File表示的文件或目录。
此方法可以删除构造方法路径中给出的文件/文件夹
- 返回值:布尔值
- true :文件/文件夹删除成功,返回true
- false:文件夹中有内容,不会删除返回false;构造方法中路径不存在false
注意:delete方法是直接在硬盘删除文件/文件夹,不走回收站,删除要谨慎
import java.io.File;
public class File05 {
public static void main(String[] args) throws IOException {
show03();
}
private static void show03() {
File f1 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\aaaa");
boolean b1= f1.delete();
System.out.println(b1);
File f2 = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding\\aaa");
boolean b2 = f2.delete();
System.out.println(b2);
}
}
执行结果
true
true
6. 目录的遍历
File类遍历(文件夹)目录功能
- public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录。
- public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。
注意:
- list方法和listFiles方法遍历的是构造方法中给出的目录
- 如果构造方法中给出的目录的路径不存在,会抛出空指针异常
- 如果构造方法中给出的路径不是一个目录, 也会抛出空指针异常
package cn.itcast.day13.demo01;
import java.io.File;
public class File06 {
public static void main(String[] args) {
show01();
System.out.println("-----------------");
show02();
}
/*
public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。
遍历构造方法中给出的目录,会获取目录中所有的文件/文件夹,把文件/文件夹封装为File对象,多个File对象存储到File数组中
*/
private static void show02() {
File file = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding");
File[] files = file.listFiles();
for(File f : files){
System.out.println(f);
}
}
/*
public String[] list() :返回一个String数组, 表示该File目录中的所有子文件或目录。
遍历构造方法中给出的目录,会获取目录中所有文件/文件夹的名称,把获取到的多个名称存储到一个String类型的数组中
*/
private static void show01() {
File file = new File("C:\\Users\\x\\Desktop\\All of D\\java_coding");
String[] arr = file.list();
for(String fileName : arr){
System.out.println(fileName);
}
}
}
执行结果
.idea
.metadata
7.jpg
aa.txt
day04-code
notepad++_code
out
src
-----------------
C:\Users\x\Desktop\All of D\java_coding\.idea
C:\Users\x\Desktop\All of D\java_coding\.metadata
C:\Users\x\Desktop\All of D\java_coding\7.jpg
C:\Users\x\Desktop\All of D\java_coding\aa.txt
C:\Users\x\Desktop\All of D\java_coding\day04-code
C:\Users\x\Desktop\All of D\java_coding\notepad++_code
C:\Users\x\Desktop\All of D\java_coding\out
C:\Users\x\Desktop\All of D\java_coding\src
qiqi