鹏北海,凤朝阳,又携书剑路茫茫
概述
java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。
- File对象就表示一个路径,可以是文件的路径,也可以是文件夹的路径
- 这个路径是可以存在的,也是不允许存在的
构造方法
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);
|
成员方法
判断、获取方法

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
| package file;
import java.io.File;
public class FileDemo1 { public static void main(String[] args) {
File file1 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\小er图.png"); System.out.println(file1.isFile()); System.out.println(file1.isDirectory()); System.out.println(file1.exists());
File file2 = new File("C:\\Users\\任俊杰\\Desktop\\课设2"); System.out.println(file2.isFile()); System.out.println(file2.isDirectory()); System.out.println(file2.exists());
File file3 = new File("C:\\Users\\任俊杰\\Desktop\\课设1"); System.out.println(file3.isFile()); System.out.println(file3.isDirectory()); System.out.println(file3.exists()); } }
|
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| package file;
import java.io.File; import java.text.SimpleDateFormat; import java.util.Date;
public class FileDemo2 { public static void main(String[] args) {
File f1 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\小er图.png"); long l = f1.length(); System.out.println(l);
System.out.println("===============================");
File f2 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\小er图.png"); String path1 = f2.getAbsolutePath(); System.out.println(path1);
File f3 = new File("function\\Student.java"); String path2 = f3.getAbsolutePath(); System.out.println(path2);
System.out.println("===============================");
File f4 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\小er图.png"); String path3 = f4.getPath(); System.out.println(path3);
File f5 = new File("function\\Student.java"); String path4 = f5.getPath(); System.out.println(path4);
System.out.println("===============================");
File f6 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\小er图.png"); String path5 = f6.getPath(); System.out.println(path5);
File f7 = new File("function\\Student.java"); String path6 = f7.getPath(); System.out.println(path6);
System.out.println("===============================");
File f8 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\小er图.png"); long time = f8.lastModified(); Date date = new Date(time); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String s = sdf.format(date); System.out.println(s);
} }
|
创建、删除方法

注意:delete方法默认只能删除文件和空文件夹,delete方法直接删除步走回收站
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
| package file;
import java.io.File; import java.io.IOException;
public class FileDemo3 { public static void main(String[] args) throws IOException {
File f1 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\a.txt"); boolean b = f1.createNewFile(); System.out.println(b);
File f2 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\aaa"); boolean mkdir = f2.mkdir(); System.out.println(mkdir);
File f3 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\b\\c\\d"); boolean mkdirs = f3.mkdirs(); System.out.println(mkdirs); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package file;
import java.io.File;
public class FileDemo4 { public static void main(String[] args) {
File f1 = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\a.txt"); boolean delete = f1.delete(); System.out.println(delete);
} }
|
获取并遍历方法


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package file;
import java.io.File;
public class FileDemo5 { public static void main(String[] args) {
File f = new File("C:\\Users\\任俊杰\\Desktop\\课设2\\a.txt"); File[] files = f.listFiles(); for (File file : files) { System.out.println(file); } } }
|
了解:
listRoots

list

list(FilenameFilter filter)

listFiles(FileFilter filter)

listFiles(FilenameFilter filter)

综合练习
练习1:在当前模块下aaa文件夹中创建一个a.txt文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package file;
import java.io.File; import java.io.IOException;
public class FilePractice1 { public static void main(String[] args) throws IOException {
File file = new File("myTest\\aaa");
file.mkdirs();
File file1 = new File(file,"a.txt");
boolean newFile = file1.createNewFile(); if (newFile){ System.out.println("succeed"); }else System.out.println("failed"); } }
|
练习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
| package file;
import java.io.File;
public class FilePractice2 { public static void main(String[] args) { File file = new File("myTest\\aaa"); boolean avi = FilePractice2.isAVI(file); System.out.println(avi);
}
public static boolean isAVI(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
| package file;
import java.io.File; import java.util.Arrays;
public class FilePractice3 { public static void main(String[] args) { findAVI(); }
public static void findAVI(){
File[] roots = File.listRoots();
for (File root : roots) { findAVI(root); } }
public static void findAVI(File file) {
File[] files = file.listFiles();
if (files != null) { for (File f : files) {
if (f.isFile()){ if (f.getName().endsWith(".avi")){ System.out.println(f); } }else{
findAVI(f); }
}
}
} }
|
练习4:删除一个多级文件夹
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
| package file;
import java.io.File;
public class FilePractice4 { public static void main(String[] args) {
File file = new File("C:\\Users\\任俊杰\\Desktop\\新建文件夹"); 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
| package file;
import java.io.File;
public class FilePractice5 {
public static void main(String[] args) { File file = new File("C:\\Users\\任俊杰\\Desktop\\课设2"); long l = getLen(file); System.out.println((double) l / 1024 / 1024 + "MB"); }
public static long getLen(File src) {
long len = 0;
File[] files = src.listFiles();
for (File file : files) {
if (file.isFile()) { len += file.length(); } else {
len += getLen(file); } } return len; }
}
|
练习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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package file;
import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Set;
public class FilePractice6 {
public static void main(String[] args) {
File file = new File("C:\\Users\\任俊杰\\Desktop\\课设2"); HashMap<String, Integer> count = getCount(file); System.out.println(count);
}
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 fileName = file.getName(); String[] arr = fileName.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(); Integer value = entry.getValue(); if (hm.containsKey(key)){
Integer count = hm.get(key); count += value; hm.put(key,value); }else{
hm.put(key,value); } } } } return hm; } }
|