0
点赞
收藏
分享

微信扫一扫

Java File类详解(下)练习部分

德州spark 2023-12-07 阅读 44


练习

第一题

需求:在当前模块下的aaa文件夹中创建一个a.txt文件

import java.io.File;
import java.io.IOException;

public class FileExer01 {
 public static void main(String[] args) throws IOException {
 File f1 = new File("AllInOne\\aaa");
 f1.mkdirs();

 File src = new File(f1, "a.txt");
 boolean flag = src.createNewFile();
 if (flag) {
 System.out.println("创建成功");
 }else {
 System.out.println("创建失败");
 }
 }
}


第二题

需求:定义一个方法找某一个文件夹中,是否有以avi结尾的电影。(暂时不需要考虑子文件夹)

D:\\aaa

D:\\aaa\\bbb

D:\\aaa\\ccc

import java.io.File;
import java.io.FileFilter;

public class AVIFinder {
 public static void main(String[] args) {
 // 需要查找的文件夹数组
 String[] paths = {"G:/", "G:\\虎胆龙威", "G:\\终结者2"};
 // 遍历路径数组
 for (String path : paths) {
 // 创建路径的File对象
 File f = new File(path);
 // 调用listFiles方法查找相应后缀名的文件
 File[] mkvFiles = f.listFiles(new FileFilter() {
 @Override
 public boolean accept(File pathname) {
 return pathname.isFile() && pathname.getName().endsWith(".mp4");
 }
 });
 System.out.println("=============路径:" + path + "下的文件:===================");
 // 输出文件夹下的mp4文件的路径
 for (File mkvFile : mkvFiles) {
 System.out.println(mkvFile.getAbsolutePath());
 }
 }
 }
}


第三题

需求:找到电脑中所有以avi结尾的电影。(需要考虑子文件夹)

C:\\

C:\\aaa

C:\\bbb

考虑递归

import java.io.File;

public class FileRecursion {

 public static void main(String[] args) {
 // 指定一个盘符
 File h = new File("h:/");
 listMKV(h);
 }

 /**
 * 列出文件夹中所有的MKV文件
 * @param dir 目录
 */
 public static void listMKV(File dir){
 // 如果是mkv文件就输出它的路径并结束
 if(dir.isFile()){
 if (dir.getName().endsWith(".mkv")){
 System.out.println(dir.getAbsolutePath());
 }
 }else {
 // 如果是目录,则列出所有文件
 File[] files = dir.listFiles();
 if (files != null) {
 for (File file : files) {
 // 递归
 listMKV(file);
 }
 }
 }
 }
}


第四题

需求:删除一个多级文件夹

import java.io.File;

/**
 * 删除一个多级目录
 */
public class FileDeletion {
 public static void main(String[] args) {
 File f = new File("H:\\test\\aaa");
 deleteDir(f);
 }

 public static void deleteDir(File dir){
 // 进入目录
 File[] files = dir.listFiles();
 if (files != null) {
 for (File file : files) {
 // 如果是文件,则删除
 if (file.isFile()){
 file.delete();
 }else {
 // 如果是文件夹,则递归
 deleteDir(file);
 }
 }
 // 最后,删除目录本身
 dir.delete();
 }
 }
}



第五题

需求:统计一个文件夹中每种文件的个数并打印。

①打印格式如下:

②txt:3个

③doc:4个

④jpg:6个


先导题:需求:统计一个文件夹的总大小

参考代码如下:

import java.io.File;

public class FileLenCount {
 public static void main(String[] args) {
 long len = 0;
 String dir = "G:\\JavaCode\\PuzzleGame";
 File f = new File(dir);
 len = countLen(f);
 System.out.println(len);
 }

 /**
 * 统计文件夹的总大小
 * @param dir 文件夹
 * @return 文件加大小(字节)
 */
 public static long countLen(File dir){
 long len = 0;
 // 列出目录中所有内容
 File[] files = dir.listFiles();
 if (files != null){
 for (File file : files) {
 // 1.如果是文件,统计其大小
 if (file.isFile()){
 len += file.length();
 }else {
 // 2. 如果是文件夹,则递归
 len += countLen(file);
 }
 }
 }
 return len;
 }
}

然后是统计各种文件类型数量的实现,使用Map存储,key为String类型表示文件扩展名,value为Integer类型,表示对应扩展名文件的数量。参考代码如下:

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class FileTypeCount {
 public static void main(String[] args) {
 long len = 0;
 String dir = "G:\\JavaCode\\PuzzleGame";
 File f = new File(dir);
 HashMap<String, Integer> hm = countTypeCount(f);
 System.out.println(hm);
 }

 /**
 * 统计文件夹的总大小
 * @param dir 文件夹
 * @return 文件加大小(字节)
 */
 public static long countLen(File dir){
 long len = 0;
 // 列出目录中所有内容
 File[] files = dir.listFiles();
 if (files != null){
 for (File file : files) {
 // 1.如果是文件,统计其大小
 if (file.isFile()){
 len += file.length();
 }else {
 // 2. 如果是文件夹,则递归
 len += countLen(file);
 }
 }
 }
 return len;
 }

 /**
 * 文件夹下文件类型的统计
 * @param dir 文件夹目录
 * @return
 */
 public static HashMap<String, Integer> countTypeCount(File dir){
 HashMap<String, Integer> hm = new HashMap<>();
 // 列出目录中所有内容
 File[] files = dir.listFiles();
 if (files != null){
 for (File file : files) {
 // 1.如果是文件,则统计其大小
 if (file.isFile()){
 // 获取后缀名。注意点.前面要加双斜线,因为split方法参数是regex正则,
 // 在正则中点号代码任意一个字符,所以需要转义
 String[] split = file.getName().split("\\.");
 if (split.length > 1){
 String typeName = split[split.length - 1];
 // 如果后缀名已有统计,则更新
 if (hm.containsKey(typeName)){
 hm.put(typeName, hm.get(typeName) + 1);
 }else {
 // 如果后缀名没有统计,则添加
 hm.put(typeName, 1);
 }
 }
 }else {
 // 2. 如果是文件夹,则递归
 HashMap<String, Integer> subMap = countTypeCount(file);
 // 获取子文件夹的统计,更新到hm中
 Set<Map.Entry<String, Integer>> entries = subMap.entrySet();
 for (Map.Entry<String, Integer> entry : entries) {
 // 如果hm中已含文件类型统计,则更新
 if (hm.containsKey(entry.getKey())){
 hm.put(entry.getKey(), hm.get(entry.getKey()) + entry.getValue());
 }
 // 如果hm中不含文件类型统计,则添加
 hm.put(entry.getKey(), entry.getValue());
 }
 }
 }
 }
 return hm;
 }
}







举报

相关推荐

0 条评论