File的静态方法
java.io.File类
文件和目录路径名的抽象表示形式。
java把电脑中的文件和文件夹(目录)封装为了一个File类,我们可以使用File类对文件和文件夹进行操作
我们可以使用File类的方法
创建一个文件/文件夹
删除文件/文件夹
获取文件/文件夹
判断文件/文件夹是否存在
对文件夹进行遍历
获取文件的大小
File类是一个与系统无关的类,任何的操作系统都可以使用这个类中的方法
重点:记住这三个单词
file:文件
directory:文件夹/目录
path:路径
static String pathSeparator 与系统有关的路径分隔符,为了方便,它被表示为一个字符串。
static char pathSeparatorChar 与系统有关的路径分隔符。
static String separator 与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
static char separatorChar 与系统有关的默认名称分隔符。
操作路径:路径不能写死了
C:\develop\a\a.txt windows
C:/develop/a/a.txt linux
"C:"+File.separator+"develop"+File.separator+"a"+File.separator+"a.txt"
package com.itheima.demo01.File;
import java.io.File;
public class Demo01File {
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:【正斜杠/】
}
}
绝对路径和相对路径
绝对路径是一个完整的路径。
以盘符(c:,D:)开始的路径
c:\\a.txt
C:\\Users\itcast\\IdeaProjects\\shungyuan\\123.txt
D:\\demo\\b.txt
相对路径是一个简化的路径。
相对指的是相对于当前项目的根目录(C:\\Users\itcast\\IdeaProjects\\shungyuan)
如果使用当前项目的根目录,路径可以简化书写
C:\\Users\itcast\\IdeaProjects\\shungyuan\\123.txt-->简化为: 123.txt(可以省略项目的根目录)
注意:
1.路径是不区分大小写
2.路径中的文件名称分隔符windows使用反斜杠,反斜杠是转义字符,两个反斜杠代表一个普通的反斜杠
File的构造方法
1.【File(String pathname)】
通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
参数:
String pathname:字符串的路径名称
路径可以是以文件结尾,也可以是以文件夹结尾
路径可以是相对路径,也可以是绝对路径
路径可以是存在,也可以是不存在
创建File对象,只是把字符串路径封装为File对象,不考虑路径的真假情况
package cn.itcast.day02.demo09;
import java.io.File;
public class Filedemo {
public static void main(String[] args) {
show1();
}
public static void show1(){
File f1=new File ("H:\\matlab\\daima1");
System.out.println(f1);
File f2=new File ("H:\\matlab\\daima2");
System.out.println(f2);
File f3=new File ("a.txt");
System.out.println(f3);
}
}
2.File(String parent, String child)
根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
参数:把路径分成了两部分
String parent:父路径
String child:子路径
好处:
父路径和子路径,可以单独书写,使用起来非常灵活;父路径和子路径都可以变化
package cn.itcast.day02.demo09;
import java.io.File;
public class Filedemo {
public static void main(String[] args) {
// show1();
show2("c:\\","a.txt");
show2("d:\\","a.txt");
}
public static void show2(String parent, String child){
File file=new File(parent,child);
System.out.println(file);
}
}
3.【File(File parent, String child) 】
根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
参数:把路径分成了两部分
File parent:父路径
String child:子路径
好处:
父路径和子路径,可以单独书写,使用起来非常灵活;父路径和子路径都可以变化
父路径是File类型,可以使用【File的方法】对路径进行一些操作,再使用路径创建对象
public static void show3(){
File parent=new File("c:\\");
File file=new File(parent,"hello.java");
System.out.println(file);
}
File的常用的方法
-
public String getAbsolutePath() :返回此File的绝对路径名字符串。【绝对路径】
-
public String getPath() :将此File转换为路径名字符串。【传入路径】
-
public String getName() :返回由此File表示的文件或目录的名称。【结尾部分// 文件or文件夹】
-
public long length() :返回由此File表示的文件的长度。【文件的长度】
【1.getAbsolutePath()】public String getAbsolutePath() :返回此File的【绝对路径】名字符串。 获取的构造方法中传递的路径 无论路径是绝对的还是相对的,getAbsolutePath方法返回的都是绝对路径
private static void show01() {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");
String absolutePath1 = f1.getAbsolutePath();
System.out.println(absolutePath1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
File f2 = new File("a.txt");
String absolutePath2 = f2.getAbsolutePath();
System.out.println(absolutePath2);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
}
【2.getPath()】
public String getPath() :将此File转换为路径名字符串。
获取的构造方法中【传递的路径】
【 toString方法调用的就是getPath方法】
源码:
public String toString() {
return getPath();
}
private static void show02() {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");
File f2 = new File("a.txt");
String path1 = f1.getPath();
System.out.println(path1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
String path2 = f2.getPath();
System.out.println(path2);//a.txt
System.out.println(f1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
System.out.println(f1.toString());//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
【底层就是getPath()方法】
}
【3.getName()】
public String getName() :返回由此File表示的文件或目录的名称。
获取的就是构造方法传递路径的【结尾部分】(文件/文件夹)
private static void show03() {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");
String name1 = f1.getName();
System.out.println(name1);//a.txt 文件
File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");
String name2 = f2.getName();
System.out.println(name2);//shungyuan 文件夹
}
【4. length()】
public long length() :返回由此File表示的【文件的长度】。
获取的是构造方法指定的文件的大小,以字节为单位
注意:
文件夹是没有大小概念的,不能获取文件夹的大小
如果构造方法中给出的路径不存在,那么length方法返回0
private static void show04() {
File f1 = new File("C:\\develop\\a\\1.jpg");
long l1 = f1.length();
System.out.println(l1);//780831字节
File f2 = new File("C:\\develop\\a\\2.jpg");//不存在的
System.out.println(f2.length());//0
File f3 = new File("C:\\develop\\a");//文件夹
System.out.println(f3.length());//0 文件夹没有大小概念的
}
File类判断功能的方法
- public boolean exists() :此File表示的文件或目录是否实际存在。
- public boolean isDirectory() :此File表示的是否为目录。
- public boolean isFile() :此File表示的是否为文件。
public boolean exists() :此File表示的文件或目录是否实际存在。
用于判断构造方法中的路径是否存在
存在:true
不存在:false
private static void show01() {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");
System.out.println(f1.exists());//true
File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shung");
System.out.println(f2.exists());//false
File f3 = new File("shungyuan.iml");//相对路径 C:\Users\itcast\IdeaProjects\shungyuan\shungyuan.iml
System.out.println(f3.exists());//true
File f4 = new File("a.txt");
System.out.println(f4.exists());//false
}
public boolean isDirectory() :此File表示的是否为目录。
用于判断构造方法中给定的路径是否以文件夹结尾
是:true
否:false
public boolean isFile() :此File表示的是否为文件。
用于判断构造方法中给定的路径是否以文件结尾
是:true
否:false
注意:
电脑的硬盘中只有文件/文件夹,两个方法是【互斥】
这两个方法使用前提,路径必须是存在的,否则都返回false
private static void show02() {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shung");
//不存在,就没有必要获取
if(f1.exists()){
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
}
File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");
if(f2.exists()){
System.out.println(f2.isDirectory());//true
System.out.println(f2.isFile());//false
}
File f3 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\shungyuan.iml");
if(f3.exists()){
System.out.println(f3.isDirectory());//false
System.out.println(f3.isFile());//true
}
}
File文件的创建和删除
File类创建删除功能的方法
-
public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。
-
public boolean delete() :删除由此File表示的文件或目录。
-
public boolean mkdir() :创建由此File表示的目录。
-
public boolean mkdirs() :创建由此File表示的目录,包括任何必需但不存在的父目录。
public boolean createNewFile()public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。 创建文件的路径和名称在构造方法中给出(构造方法的参数) 返回值:布尔值 true:文件不存在,创建文件,返回true false:文件存在,不会创建,返回false 注意: 1.此方法只能创建【文件】,不能创建文件夹 2.创建文件的【路径必须存在】,否则会抛出异常 public boolean createNewFile() throws IOException createNewFile声明抛出了IOException,我们调用这个方法,就必须的处理这个异常,要么throws,要么trycatch
private static void show01() throws IOException {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\08_FileAndRecursion\\1.txt");
boolean b1 = f1.createNewFile();
System.out.println("b1:"+b1);
File f2 = new File("08_FileAndRecursion\\2.txt");
System.out.println(f2.createNewFile());
File f3 = new File("08_FileAndRecursion\\新建文件夹");
System.out.println(f3.createNewFile());//不要被名称迷糊,要看类型
File f4 = new File("08_FileAndRecursi\\3.txt");
System.out.println(f4.createNewFile());//路径不存在,抛出IOException
}
public boolean delete()
public boolean delete() :删除由此File表示的文件或目录。
此方法,可以删除构造方法路径中给出的【文件/文件夹】
返回值:布尔值
true:文件/文件夹删除成功,返回true
false:文件夹中有内容,不会删除返回false;构造方法中路径不存在false
注意:
delete方法是直接在硬盘删除文件/文件夹,不走回收站,删除要谨慎
public static void show03(){
//单级文件夹
File f1=new File("DATE_1\\aaa");
System.out.println(f1.delete());
//多级文件夹
File f2=new File("DATE_1\\bbb\\111");
System.out.println(f2.delete());
//多级文件夹
File f3=new File("DATE_1\\ccc\\1.txt");
System.out.println(f3.delete());
//仍然是是个1.txt的文件夹,不能看名称
}
public boolean mkdir() and public boolean mkdirs()
public boolean mkdir() :创建单级空文件夹
public boolean mkdirs() :既可以创建单级空文件夹,也可以创建多级文件夹
创建文件夹的路径和名称在构造方法中给出(构造方法的参数)
返回值:布尔值
true:文件夹不存在,创建文件夹,返回true
false:文件夹存在,不会创建,返回false;构造方法中给出的路径不存在返回false
注意:
1.此方法只能创建文件夹,不能创建文件
public static void show02(){
//单级文件夹
File f1=new File("DATE_1\\aaa");
System.out.println(f1.mkdir());
//多级文件夹
File f2=new File("DATE_1\\bbb\\111");
System.out.println(f2.mkdirs());
//多级文件夹
File f3=new File("DATE_1\\ccc\\1.txt");
System.out.println(f3.mkdirs());
//仍然是是个1.txt的文件夹,不能看名称
}
遍历File文件
File类遍历(文件夹)目录功能
- public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录。
- public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。
注意:
list方法和listFiles方法遍历的是构造方法中给出的目录
如果构造方法中给出的目录的路径不存在,会抛出空指针异常
如果构造方法中给出的路径不是一个目录,也会抛出空指针异常
【1. public String[] list()】
public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录。
遍历构造方法中给出的目录,会获取目录中所有文件/文件夹的名称,把获取到的多个名称存储到一个String类型的数组中
private static void show01() {
//File file = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\08_FileAndRecursion\\1.txt");//NullPointerException
//File file = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\08_Fi");//NullPointerException
File file = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\08_FileAndRecursion");
String[] arr = file.list();
for (String fileName : arr) {
System.out.println(fileName);
}
}
【2. public File[] listFiles() 】
public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。
遍历构造方法中给出的目录,会获取目录中所有的文件/文件夹,把文件/文件夹封装为【File对象】,多个File对象存储到【File数组】中
private static void show02() {
File file = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\08_FileAndRecursion");
File[] files = file.listFiles();
for (File f : files) {
System.out.println(f);
}
}
综合案例遍历多级目录
package cn.itcast.day02.demo10;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
//获取的一个路径
File file=new File("c:\\abc");
getAllFile(file);
}
public static void getAllFile(File dir){
System.out.println(dir);
//通过listFiles()方法,把该路径的文件遍历出来存在file类型的数组里
File[] files=dir.listFiles();
//遍历File类型的数组
for(File f:files){
if(f.isDirectory()){
getAllFile(f);
//如果是个文件夹,则遍历这个文件夹,递归算法
}
else {
System.out.println(f);
}
}
/* c:\abc\a
c:\abc\abc.java
c:\abc\abc.txt
c:\abc\b*/
}
}