JavaFileExercise

  1. 在根路径下执行。
    ① 使用Scanner创建菜单页,功能1查看,功能2新建,功能3删除。
    ② 输入数字1键,查看跟路径下的所有数据,显示格式 序号 名称 类型 大小。
    ③ 输入数字2键,显示新建页,1. 新建文件夹,2. 新建文件,输入相应的数字后,需要提示输入文件名称,接收键盘中的参数,作为新文件的文件名,并相应创建文件。
    ④ 输入数字3键,实现查询所有当前数据的方法,并提示通过输入指定序号删除对应的文件操作。
    ⑤ 判断指定目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称。
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Main {

    // 路径
    public static String path = "D:\\exercise";

    public static void main(String[] args) {
        // 功能1查看,功能2新建,功能3删除。功能4退出
        while (true) {
            System.out.println("请输入功能编号:功能1查看,功能2新建,功能3删除。功能4退出");
            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    look(path);
                    break;
                case 2:
                    newFile(path);
                    System.out.println("新建");
                    break;
                case 3:
                    look(path);
                    deleteFile(path);
                    System.out.println("删除");
                    break;
                case 4:
                    System.out.println("退出");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    // 功能1查看
    public static void look(String path) {
        // 定义一个序号
        int index = 1;
        File directory = new File(path);
        File[] files = directory.listFiles();
        if (files == null) {
            System.out.println("没有文件");
            return;
        }
        for (File file : files) {
            String type = file.isDirectory() ? "文件夹" : "文件";
            if (!file.isDirectory()){
                if (file.getName().endsWith(".txt")) {
                    type = "文本文件";
                } else if (file.getName().endsWith(".md")) {
                    type = "markdown文件";
                } else if (file.getName().endsWith(".docx")) {
                    type = "docx文件";
                }
            }
            long size = file.isFile() ? file.length() : 0;
            System.out.println(index + " 文件名称:" + file.getName() + " 类型:" + type + " 大小:" + size + "字节");
            index++;
        }
    }

    // 功能2新建
    public static void newFile(String path) {
        System.out.println("请选择新建类型:");
        System.out.println("1. 新建文件夹");
        System.out.println("2. 新建文件");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        scanner.nextLine(); // 吸收换行符

        System.out.println("请输入名称:");
        String name = scanner.nextLine();

        File newFile = new File(path + File.separator + name);
        try {
            boolean created = false;
            if (choice == 1) {
                created = newFile.mkdirs();
            } else if (choice == 2) {
                created = newFile.createNewFile();
            }

            if (created) {
                System.out.println("成功创建: " + newFile.getName());
            } else {
                System.out.println("创建失败,可能文件或文件夹已存在。");
            }
        } catch (IOException e) {
            System.out.println("发生错误: " + e.getMessage());
        }
    }

    // 功能3删除
    // 根据序号删除文件或文件夹
    public static void deleteFile(String path) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的文件序号:");
        int index = scanner.nextInt();

        File directory = new File(path);
        File[] files = directory.listFiles();
        if (files == null || index < 1 || index > files.length) {
            System.out.println("无效的序号。");
            return;
        }

        File fileDelete = files[index - 1];
        if (deleteRecursively(fileDelete)) {
            System.out.println("已删除: " + fileDelete.getName());
        } else {
            System.out.println("删除失败。");
        }
    }

    public static boolean deleteRecursively(File file) {
        if (file.isDirectory()) {
            File[] allContents = file.listFiles();
            if (allContents != null) {
                for (File child : allContents) {
                    deleteRecursively(child);
                }
            }
        }
        return file.delete();
    }

}

Java展现树形结构

import java.io.File;

public class TreeText {
    public static void main(String[] args) {
        File path = new File("D:\\");
        printTree(path, 0, true);
    }

    public static void printTree(File path, int level, boolean isLast) {
        if (!path.isDirectory()) {
            return;
        }

        for (int i = 0; i < level; i++) {
            System.out.print("│  ");
        }

        System.out.print(isLast ? "└─" : "├─");
        System.out.println(path.getName());

        File[] files = path.listFiles();
        if (files != null) {
            int count = 0;
            for (File file : files) {
                if (file.isDirectory()) {
                    count++;
                }
            }

            int count1 = 0;
            for (File file : files) {
                if (file.isDirectory()) {
                    count1++;
                    printTree(file, level + 1, count == count1);
                }
            }
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值