File
File对象表示一个路径,可以是文件的路径,也可以是文件夹的路径
这个路径可以是存在的,也允许不存在
常见的构造方法
图来自黑马程序员网课
package com.lazyGirl.filedemo;
import java.io.File;
public class Demo1 {
public static void main(String[] args) {
String str = "E:\\bishe\\paper1\\SignalToNoisy\\a.txt";
File file = new File(str);
System.out.println(file);
String parent = "E:\\bishe\\paper1\\SignalToNoisy";
String child = "a.txt";
File file1 = new File(parent, child);
System.out.println(file1);
File file2 = new File(parent + "\\" + child);
System.out.println(file2);
File parent2 = new File(parent);
String child2 = "a.txt";
File file3 = new File(parent, child2);
System.out.println(file3);
}
}
输出:
绝对路径是带盘符的,相对路径是不带盘符的,默认到当前项目下去找
常见成员方法
图来自黑马程序员网课
package com.lazyGirl.filedemo;
import java.io.File;
public class Demo2 {
public static void main(String[] args) {
File file1 = new File("E:\\bishe\\paper1\\SignalToNoisy");
System.out.println(file1.exists());
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
System.out.println();
File file2 = new File("E:\\bishe\\paper1\\SignalToNoisy");
System.out.println(file2.exists());
System.out.println(file2.isDirectory());
System.out.println(file2.isFile());
System.out.println();
File file3 = new File("E:\\bishe\\paper1\\SignalToNoisy\\10x_lymph_node_group_14_normalize.jpg");
System.out.println(file3.exists());
long len = file3.length();
System.out.println(len);//单位字节
System.out.println();
File file4 = new File("E:\\bishe\\paper1\\SignalToNoisy");
System.out.println(file4.exists());
long len2 = file4.length();//无法获取文件夹大小
System.out.println(len2 / 1024 / 1024);
System.out.println();
File file5 = new File("E:\\bishe\\paper1\\SignalToNoisy\\10x_lymph_node_group_14_normalize.jpg");
String path1 = file5.getAbsolutePath();
System.out.println(path1);
System.out.println();
File file6 = new File(path1);
System.out.println(file6.getPath());
System.out.println();
File file7 = new File("E:\\bishe\\paper1\\SignalToNoisy\\a.txt");
System.out.println(file7.getName());
System.out.println();
File file8 = new File("E:\\bishe\\paper1\\SignalToNoisy\\");
System.out.println(file8.getName());
System.out.println();
File file9 = new File(path1);
System.out.println(file9.lastModified());
}
}
输出:
图来自黑马程序员网课
package com.lazyGirl.filedemo;
import java.io.File;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
File file = new File("a.txt");
//该方法创建的是文件,如果路径中不包括后缀名,则创建一个没有后缀的文件
System.out.println(file.createNewFile());
File file2 = new File("\\aaa");
//既可以创建单级文件也可以创建多级文件
System.out.println(file2.mkdirs());
//若删除的是文件,则直接删除
//若删除的是空文件夹,则直接删除,不走回收站,若是有内容的文件夹,则删除失败
file.delete();
file2.delete();
}
}
输出:
图来自黑马程序员网课
注意:
当调用者File表示的路径不存在时,返回null;
当调用者File表示的路径是文件时,返回null
当调用者File表示的路径是一个空文件夹时,返回一个长度为0的数组
当调用者File表示的路径是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回,若包含隐藏文件,也返回
当调用者File表示的路径是需要权限才能访问的文件夹时,返回null
package com.lazyGirl.filedemo;
import java.io.File;
public class Demo4 {
public static void main(String[] args) {
File file = new File("E:\\bishe\\paper1\\SignalToNoisy");
File[] files = file.listFiles();
for (File f : files) {
System.out.println(f);
}
}
}
输出:
图来自黑马程序员网课
package com.lazyGirl.filedemo;
import java.io.File;
import java.io.FilenameFilter;
public class Demo5 {
public static void main(String[] args) {
// File[] arr = File.listRoots();
// for (File f : arr) {
// System.out.print(f + " ");
// }
// System.out.println();
// System.out.println();
//
// File file = new File("D:\\");
// String[] files = file.list();
// for (String s : files) {
// System.out.println(s + " ");
// }
// System.out.println();
File file1 = new File("D:\\");
//文件名过滤器
String[] files1 = file1.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return f.isFile() && name.endsWith(".txt");
}
});
for (String s : files1) {
System.out.println(s + " ");
}
}
}
package com.lazyGirl.filedemo;
import java.io.File;
public class Demo6 {
public static void main(String[] args) {
File f = new File("D:\\");
File[] files = f.listFiles();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")){
System.out.println(file);
}
}
}
}
删除级联文件夹
package com.lazyGirl.filedemo;
import java.io.File;
public class Demo7 {
public static void main(String[] args) {
File file = new File("immutable\\aaa");
deleteFile(file);
}
public static void deleteFile(File f) {
File[] files = f.listFiles();
for (File file : files) {
if (file.isFile()) {
file.delete();
}else {
deleteFile(file);
}
}
f.delete();
}
}
求特定文件夹下文件的个数:
package com.lazyGirl.filedemo;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class Test3 {
public static void main(String[] args) {
File file = new File("E:\\bishe");
HashMap<String,Integer> hm = getCount(file);
System.out.println(hm);
}
public static HashMap<String, Integer> getCount(File file){
HashMap<String,Integer> map = new HashMap<>();
File[] files = file.listFiles();
if(files != null){
for (File file1 : files) {
if (file1.isFile()){
String[] arr = file1.getName().split("\\.");
if (arr.length == 2){
String endName = arr[arr.length-1];
if (map.containsKey(endName)){
map.put(endName, map.get(endName)+1);
}else {
map.put(endName, 1);
}
}
}else {
HashMap<String,Integer> sonMap = getCount(file1);
for (String s : sonMap.keySet()) {
if (map.containsKey(s)){
map.put(s, map.get(s)+sonMap.get(s));
}else {
map.put(s, sonMap.get(s));
}
}
}
}
}
return map;
}
}
输出: