一、Flie类
1、定义
-
可以通过Flie的去找到一个文件或者文件夹,然后对它新建一个对象。
-
这个文件或文件夹的路径可以为空,并不会报错,只是找不到。
- f4.exists()可以判断一个文件是否存在。
-
路径建议:
- 路径分割符可以用
Flie.separator
来代替,他可以根据当前系统自动转换成对应系统路径分割符 - 如果文件在项目下,要采用相对路径。
Flie f1 = new Flie("D:/resource/ad.txt") Flie f2 = new Flie("D:\\resource\\ad.txt") Flie f3 = new Flie("D:"+Flie.separator+"resource"+Flie.separator+"ad.txt") System.out.println(f3.length()) //文件大小 Flie f4 = new Flie("D:"+Flie.separator+"resource"+Flie.separator+"aaasda.txt") System.out.println(f4.length()) //若文件不存在,则输出0 System.out.println(f4.exists()) //false //定位模块中的文件:相对路径 Flie f5 = new Flie("file-io-app"+Flie.separator+"src"+Flie.separator+"ad.txt")
- 路径分割符可以用
2、判断信息的相关方法
2.1、exists()
-
定义:判断对应文件路径是否存在,存在则返回true
Flie f1 = new Flie("D:"+Flie.separator+"resource"+Flie.separator+"ad.txt") System.out.println(f1.exists()) //存在true,不存在false
2.2、isFlie()
-
定义:判断当前文件对象指代的是否为文件,是返回true
Flie f1 = new Flie("D:"+Flie.separator+"resource"+Flie.separator+"ad.txt"); System.out.println(f1.isFlie()) //是文件true,非文件false
2.3、isDirectory()
-
定义:判断当前文件对象指代的是否为文件夹,是返回true
File f1 = new File("D:"+Flie.separator+"resource"+Flie.separator+"ad.txt"); System.out.println(f1.isDirectory()) //是文件夹true,非文件夹false
2.4、getName()
-
定义:获取文件的名称,包括后缀
File f1 = new File("D:"+Flie.separator+"resource"+Flie.separator+"ad.txt"); System.out.println(f1.getName()) //ad.txt
2.5、length()
-
定义:获取文件的大小,返回字节个数
File f1 = new File("D:"+Flie.separator+"resource"+Flie.separator+"ad.txt"); System.out.println(f1.length()) //20
2.6、lastModified()
-
定义:获取文件最后的修改时间
File f1 = new File("D:"+Flie.separator+"resource"+Flie.separator+"ad.txt"); long time = f1.lastModified(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置格式化样式 System.out.println(sdf.format(time)) // 2024-08-02 18:04:45
2.7、getPath()
- 定义:获取创建文件对象时使用的路径。
- 就是我们用程序创建的文件,new File(“D:”+Flie.separator+“resource”+Flie.separator+“hahhha.txt”),中的路径。
2.8、getAbsolutePath()
-
定义:获取绝对路径
File f1 = new File("D:"+Flie.separator+"resource"+Flie.separator+"ad.txt"); System.out.println(f1.getAbsolutePath()) //获取文件的绝对路径
3、创建和删除文件
3.1、createNewFile()
-
定义:创建文件,如果文件不存在,返回true,否则false
File file = new File(File.separator + "Users" + File.separator + "hhc" + File.separator + "Desktop" + File.separator + "day01-MybatisPlus" + File.separator + "liHua.txt"); System.out.println(file.createNewFile()); //true
3.2、mkdir()
-
定义:创建一级文件夹,只能创建一级文件夹
File file = new File(File.separator + "Users" + File.separator + "hhc" + File.separator + "Desktop" + File.separator + "day01-MybatisPlus" + File.separator + "liHua"); System.out.println(file.mkdir()); //true
3.3、mkdirs()
-
定义:创建多级文件夹,只能创建多级文件夹
File file = new File(File.separator + "Users" + File.separator + "hhc" + File.separator + "Desktop" + File.separator + "day01-MybatisPlus" + File.separator + "aaa"+ File.separator + "bbb"+ File.separator + "ccc"); System.out.println(file.mkdirs());
3.4、delete()
-
定义:可以删除
文件
和空文件夹
-
注意:不能删除
非空文件夹
,删除的文件不会进入回收站。File file = new File(File.separator + "Users" + File.separator + "hhc" + File.separator + "Desktop" + File.separator + "day01-MybatisPlus" + File.separator + "aaa"+ File.separator + "bbb"+ File.separator + "ccc"); System.out.println(file.delete());
4、遍历文件夹的方法
4.1、list()
-
定义:返回一个string类型的数组,将所有一级文件或文件夹的名称返回。
File file = new File(File.separator + "Users" + File.separator + "hhc" + File.separator + "Desktop" + File.separator + "day01-MybatisPlus"); String[] list = file.list(); for (String s : list) { System.out.println(s); } /* 资料 aaa liHua PPT liHua.txt */
4.2、listFiles() ★★★
-
定义:返回一个File类型的对象数组,将所有的一级文件或者文件夹的file对象返回。(隐藏文件也会返回)
-
注意事项:
- 如果当前对象是
一个文件
、路径不存在
或者无权访问文件夹
,则会返回null
。 - 如果当前对象是
空文件夹
,则返回一个长度为0的数组
。
File file = new File(File.separator + "Users" + File.separator + "hhc" + File.separator + "Desktop" + File.separator + "day01-MybatisPlus"); File[] files = file.listFiles(); for (File file1 : files) { System.out.println(file1.getAbsolutePath()); //获取文件的绝对路径并输出 } /* /Users/hhc/Desktop/day01-MybatisPlus/资料 /Users/hhc/Desktop/day01-MybatisPlus/aaa /Users/hhc/Desktop/day01-MybatisPlus/liHua /Users/hhc/Desktop/day01-MybatisPlus/PPT /Users/hhc/Desktop/day01-MybatisPlus/liHua.txt */
- 如果当前对象是
5、文件重命名
- renameTo
- 使用:file.renameTo( new File(父路径 , 新名字) );
二、递归
- 定义:自己调用自己,给一个终结操作。
1、递归三要素
-
以阶成举例:
-
递归公式:f(n) = f(n-1) * n
-
递归的终结点:f(1)
-
递归的方向必须走向终结点。
2、文件搜索
public static void fileSearchPath(String fileName,File dir){
//1、如果文件夹为空,路径不存在,是文件,代表无法搜索
if (dir == null || !dir.exists() || dir.isFile()){
return;
}
//2、dir不是null,存在,一定是目录对象。
//获取当前目录下的全部一级对象
File[] files = dir.listFiles();
//3、判断当前目录下是否存在一级文件对象,以及是否可以拿到以及文件对象
if (files != null){
//4、遍历全部一级文件对象。
for (File file : files) {
//5、判断文件是文件,还是文件夹
if (file.isFile()){
//是文件,判断文件名是否是我们要找的
if (file.getName().equals(fileName)){
System.out.println("找到了:"+file.getAbsolutePath());
}
} else {
//是文件夹,继续重复这个过程(递归)
fileSearchPath(fileName,file);
}
}
}
}
3、删除非空文件夹
public static void delete(File file){
if (file.isFile()){ //首先判断该对象是不是文件,如果是文件可以直接删除
file.delete();
}else {
File[] files = file.listFiles(); //如果不是文件,那就将该对象转换成文件对象数组
for (int i = 0; files!=null && i < files.length; i++) { //(1)接下来对数组进行遍历
if (files[i].isFile()){ //再次判断文件是否为文件,如果是还是直接删除
files[i].delete();
}else {
delete(file); //(2)如果不是文件就调用自己,将数组中的对象作为参数传进delete()
}
}
}
file.delete(); //(3)这里是回调结束以后返回时,需要将文件夹给删除
}
- 将文件夹中的子文件转换成文件对象数组进行遍历再判断子文件是否为文件,如果是文件,那就可以直接删除
- if不是文件,那就是文件夹,前面for已经判断文件夹不为空了,所以直接将文件夹进行递归
- 当递归结束,往回走时,需要删除自身文件夹
三、字符集UTF-8
- 定义:是Unicode字符集的一种编码方案,采取可变长编码方案,共分四个长度区:1个字节,2个字节,3个字节,四个字节
- 英文字符、数字等只占一个字节(兼容标准ASCII编码),汉字字符占用3个字节。
- 注意事项:
- 编码和解码的字符集要一样,否则汉字会乱码。
- 英文和数字一般不乱码,原因是很多字符集都兼容了ASCII编码
1、GBK编码方式(二进制)
- 0 \textcolor{red}{0} 0xxxxxxx
- 1 \textcolor{red}{1} 1xxxxxxx xxxxxxxx
2、UTF-8编码方式(二进制)
- 0 \textcolor{red}{0} 0xxxxxxx
- 110 \textcolor{red}{110} 110xxxxx 10 \textcolor{red}{10} 10xxxxxx
- 1110 \textcolor{red}{1110} 1110xxxx 10 \textcolor{red}{10} 10xxxxxx 10 \textcolor{red}{10} 10xxxxxx
- 11110 \textcolor{red}{11110} 11110xxx 10 \textcolor{red}{10} 10xxxxxx 10 \textcolor{red}{10} 10xxxxxx 10 \textcolor{red}{10} 10xxxxxx
3、重点掌握
- ASCII字符集:只有英、数字、符号等,占一个字节。
- GBK字符集:汉字占2个字节,英文、数字占一个字节。
- UTF-8字符集:汉字占3个字节,英文、数字占一个字节。
4、编码和解码的常用方法:
4.1、编码:
- getBytes():使用平台默认的字符集,将String编码成一系列字节,存储到新的字节数组中。
- getBytes(String charsetName):使用指定的字符集,…
4.2、解码:
- String(byte[] bytes):通过平台默认的字符集解码自定的字节数组来构建新的String
- String(byte[] bytes, String charsetName):通过指定的的字符集解码自定的字节数组来构建新的String