1. File类
1.1 概述
java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。
1.2 构造方法
public File(String pathname) :通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
public File(String parent, String child) :从父路径名字符串和子路径名字符串创建新的 File实例。
public File(File parent, String child) :从父抽象路径名和子路径名字符串创建新的 File实例。
构造举例,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| String pathname = "D:\\aaa.txt"; File file1 = new File(pathname);
String pathname2 = "D:\\aaa\\bbb.txt"; File file2 = new File(pathname2);
String parent = "d:\\aaa"; String child = "bbb.txt"; File file3 = new File(parent, child);
File parentDir = new File("d:\\aaa"); String child = "bbb.txt"; File file4 = new File(parentDir, child);
|
小贴士:
- 一个File对象代表硬盘中实际存在的一个文件或者目录。
- 无论该路径下是否存在文件或者目录,都不影响File对象的创建。
1.3 常用方法
获取功能的方法
public String getAbsolutePath() :返回此File的绝对路径名字符串。
public String getPath() :将此File转换为路径名字符串。
public String getName() :返回由此File表示的文件或目录的名称。
public long length() :返回由此File表示的文件的长度。
方法演示,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class FileGet { public static void main(String[] args) { File f = new File("d:/aaa/bbb.java"); System.out.println("文件绝对路径:"+f.getAbsolutePath()); System.out.println("文件构造路径:"+f.getPath()); System.out.println("文件名称:"+f.getName()); System.out.println("文件长度:"+f.length()+"字节");
File f2 = new File("d:/aaa"); System.out.println("目录绝对路径:"+f2.getAbsolutePath()); System.out.println("目录构造路径:"+f2.getPath()); System.out.println("目录名称:"+f2.getName()); System.out.println("目录长度:"+f2.length()); } } 输出结果: 文件绝对路径:d:\aaa\bbb.java 文件构造路径:d:\aaa\bbb.java 文件名称:bbb.java 文件长度:636字节
目录绝对路径:d:\aaa 目录构造路径:d:\aaa 目录名称:aaa 目录长度:4096
|
API中说明:length(),表示文件的长度。但是File对象表示目录,则返回值未指定。
绝对路径和相对路径
- 绝对路径:从盘符开始的路径,这是一个完整的路径。
- 相对路径:相对于项目目录的路径,这是一个便捷的路径,开发中经常使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class FilePath { public static void main(String[] args) { File f = new File("D:\\bbb.java"); System.out.println(f.getAbsolutePath()); File f2 = new File("bbb.java"); System.out.println(f2.getAbsolutePath()); } } 输出结果: D:\bbb.java D:\idea_project_test4\bbb.java
|
判断功能的方法
public boolean exists() :此File表示的文件或目录是否实际存在。
public boolean isDirectory() :此File表示的是否为目录。
public boolean isFile() :此File表示的是否为文件。
方法演示,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class FileIs { public static void main(String[] args) { File f = new File("d:\\aaa\\bbb.java"); File f2 = new File("d:\\aaa"); System.out.println("d:\\aaa\\bbb.java 是否存在:"+f.exists()); System.out.println("d:\\aaa 是否存在:"+f2.exists()); System.out.println("d:\\aaa 文件?:"+f2.isFile()); System.out.println("d:\\aaa 目录?:"+f2.isDirectory()); } } 输出结果: d:\aaa\bbb.java 是否存在:true d:\aaa 是否存在:true d:\aaa 文件?:false d:\aaa 目录?:true
|
创建删除功能的方法
public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。
public boolean delete() :删除由此File表示的文件或目录。
public boolean mkdir() :创建由此File表示的目录。
public boolean mkdirs() :创建由此File表示的目录,包括任何必需但不存在的父目录。
方法演示,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class FileCreateDelete { public static void main(String[] args) throws IOException { File f = new File("aaa.txt"); System.out.println("是否存在:"+f.exists()); System.out.println("是否创建:"+f.createNewFile()); System.out.println("是否存在:"+f.exists()); File f2= new File("newDir"); System.out.println("是否存在:"+f2.exists()); System.out.println("是否创建:"+f2.mkdir()); System.out.println("是否存在:"+f2.exists());
File f3= new File("newDira\\newDirb"); System.out.println(f3.mkdir()); File f4= new File("newDira\\newDirb"); System.out.println(f4.mkdirs()); System.out.println(f.delete()); System.out.println(f2.delete()); System.out.println(f4.delete()); } }
|
API中说明:delete方法,如果此File表示目录,则目录必须为空才能删除。
1.4 目录的遍历
public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录。
public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class FileFor { public static void main(String[] args) { File dir = new File("d:\\java_code"); String[] names = dir.list(); for(String name : names){ System.out.println(name); } File[] files = dir.listFiles(); for (File file : files) { System.out.println(file); } } }
|
小贴士:
调用listFiles方法的File对象,表示的必须是实际存在的目录,否则返回null,无法进行遍历。
1.5 综合练习
练习1:创建文件夹
在当前模块下的aaa文件夹中创建一个a.txt文件
代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class Test1 { public static void main(String[] args) throws IOException {
File file = new File("myfile\\aaa"); file.mkdirs(); File src = new File(file,"a.txt"); boolean b = src.createNewFile(); if(b){ System.out.println("创建成功"); }else{ System.out.println("创建失败"); } } }
|
练习2:查找文件(不考虑子文件夹)
定义一个方法找某一个文件夹中,是否有以avi结尾的电影(暂时不需要考虑子文件夹)
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Test2 { public static void main(String[] args) {
File file = new File("D:\\aaa\\bbb"); boolean b = haveAVI(file); System.out.println(b); }
public static boolean haveAVI(File file){ File[] files = file.listFiles(); for (File f : files) { if(f.isFile() && f.getName().endsWith(".avi")){ return true; } } return false; } }
|
练习3:(考虑子文件夹)
找到电脑中所有以avi结尾的电影。(需要考虑子文件夹)
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public class Test3 { public static void main(String[] args) {
findAVI();
}
public static void findAVI(){ File[] arr = File.listRoots(); for (File f : arr) { findAVI(f); } }
public static void findAVI(File src){ File[] files = src.listFiles(); if(files != null){ for (File file : files) { if(file.isFile()){ String name = file.getName(); if(name.endsWith(".avi")){ System.out.println(file); } }else{ findAVI(file); } } } } }
|
练习4:删除多级文件夹
需求: 如果我们要删除一个有内容的文件夹
1.先删除文件夹里面所有的内容
2.再删除自己
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class Test4 { public static void main(String[] args) {
File file = new File("D:\\aaa\\src"); delete(file);
}
public static void delete(File src){ File[] files = src.listFiles(); for (File file : files) { if(file.isFile()){ file.delete(); }else { delete(file); } } src.delete(); } }
|
练习5:统计大小
需求:统计一个文件夹的总大小
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public class Test5 { public static void main(String[] args) {
File file = new File("D:\\aaa\\src");
long len = getLen(file); System.out.println(len); }
public static long getLen(File src){ long len = 0; File[] files = src.listFiles(); for (File file : files) { if(file.isFile()){ len = len + file.length(); }else{ len = len + getLen(file); } } return len; } }
|
练习6:统计文件个数
需求:统计一个文件夹中每种文件的个数并打印。(考虑子文件夹)
打印格式如下:
txt:3个
doc:4个
jpg:6个
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| public class Test6 { public static void main(String[] args) throws IOException {
File file = new File("D:\\aaa\\src"); HashMap<String, Integer> hm = getCount(file); System.out.println(hm); }
public static HashMap<String,Integer> getCount(File src){ HashMap<String,Integer> hm = new HashMap<>(); File[] files = src.listFiles(); for (File file : files) { if(file.isFile()){ String name = file.getName(); String[] arr = name.split("\\."); if(arr.length >= 2){ String endName = arr[arr.length - 1]; if(hm.containsKey(endName)){ int count = hm.get(endName); count++; hm.put(endName,count); }else{ hm.put(endName,1); } } }else{ HashMap<String, Integer> sonMap = getCount(file); Set<Map.Entry<String, Integer>> entries = sonMap.entrySet(); for (Map.Entry<String, Integer> entry : entries) { String key = entry.getKey(); int value = entry.getValue(); if(hm.containsKey(key)){ int count = hm.get(key); count = count + value; hm.put(key,count); }else{ hm.put(key,value); } } } } return hm; } }
|