Java实现简易文件资源管理器

这是一个使用Java实现的简易文件管理系统,支持多种文件操作如查看目录、文件查找、复制、重命名等。系统通过命令行界面接收用户输入,提供了一个类似于DOS命令行的交互环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

花了一天时间写的:cd代码哪里有点问题,但是通过读取全地址路径的方式还是没有多大问题的

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.Scanner;

public class Window {

	static int num = 0;

	static StringBuffer sb;

	public static void main(String[] args) throws IOException {

		Scanner input = new Scanner(System.in);
		System.out.println("Welcome to the Windows file management system");
		System.out.println("2019 QLU_fuchen  保留所有权利");
		System.out.println();

		help();
		System.out.println();

		String command = "";
		String strPath = "D:\\"; // 用一个字符串记录当前文件的路径

		boolean flag = true;
		while (flag) {
			System.out.print(strPath + ">");
			command = input.nextLine();
//			System.out.println("command = " + command);
			String[] str = command.split(" ");
//			System.out.println("str.length = " + str.length);
			if (str.length == 1) { // 只有一个的命令 dir exit
				if (str[0].equals("dir")) {
					dir(strPath);
				} else if (str[0].equals("help")) {
					help();
				} else if (str[0].equals("exit")) {
					flag = false;
				}
			} else if (str.length == 2) { // 文件的操作,有两条 count指令
				if (str[0].equals("count")) {
					System.out.println(str[1] + "目录下包含文件:" + countNum(str[1]));
				} else if (str[0].equals("cd")) {
					strPath = str[1];
				} else if (str[0].equals("find")) {

					sb = new StringBuffer();
					File[] roots = File.listRoots(); // 找到电脑里面的所有磁盘
					for (int i = 1; i < roots.length; i++) {
						findFile(roots[i].toString(), str[1]);
					}
					if (sb.toString().equals("")) {
						System.out.println("未搜寻到该文件");
					} else {
						System.out.println("搜寻完毕:" + sb.toString());
					}
				} else if (str[0].equals("read")) {
					readFile(str[1], Integer.MAX_VALUE);
				}
			} else if (str.length == 3) {
				if (str[0].equals("copy")) { // 文件的复制
					System.out.println("文件的复制");
					copyFile(str[1], str[2]);
				} else if (str[0].equals("xcopy")) { // 文件目录的复制
					System.out.println("文件目录的复制");
					copyDir(str[1], str[2]);
				} else if (str[0].equals("rename")) { // 文件的重命名
					fileReameTo(str[1], str[2]);
					System.out.println("文件的重命名");
				} else if (str[0].equals("find")) { // 在指定的磁盘中搜索
					sb = new StringBuffer();
					if (!str[1].endsWith(":")) {
						str[1] += ":\\";
					}

					findFile(str[1], str[2]);
					if (sb.toString().equals("")) {
						System.out.println("未搜寻到该文件");
					} else {
						System.out.println("搜寻完毕:" + sb.toString());
					}
				} else if (str[0].equals("read")) {
					readFile(str[1], Integer.parseInt(str[2]));
				}
			}
			System.out.println();
		}

	}

	public static void help() {
		System.out.println("help 提示操作指令");
		System.out.println("cd 文件路径(跳转到当前路径中)");
		System.out.println("dir 查看当前文件夹下的文件列表");
		System.out.println("find 文件名(在整个电脑中查找文件)");
		System.out.println("find 磁盘: 文件名(在指定磁盘下搜索文件)");
		System.out.println("copy 原文件路径 新文件路径   (复制文件)");
		System.out.println("xcopy 原目录路径 新目录路径(复制目录)");
		System.out.println("read 文件路径 读取行数(第二个参数没有默认读取全部)");
		System.out.println("rename 原文件名字 新文件名字");
		System.out.println("count 文件夹(计算文件夹中文件的数量)");
		System.out.println("exit (退出)");
	}

	public static void dir(String filePath) {
		File file = new File(filePath);
		File[] files = file.listFiles(); // 获取当前路径下的文件
		for (File file2 : files) {
			System.out.println(file2);
		}
	}

	// 文件查找功能,通过文件名查找文件
	public static void findFile(String path, String fileName) {
		File file = new File(path);

		// 如果这个路径是文件夹
		if (file.isDirectory()) {
			// 获取路径下的所有文件
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				// 如果还是文件夹 递归获取里面的文件 文件夹
				if (files[i].isDirectory()) {
//					System.out.println("目录:" + files[i].getPath());
					if (!files[i].getPath().endsWith("System Volume Information")) {
						findFile(files[i].getPath(), fileName);
					}
				} else {
					File tmpFile = new File(files[i].getPath());
					if (tmpFile.getName().equals(fileName)) {
//						System.out.println("路径为:" + tmpFile.getPath());
						sb.append(tmpFile.getPath() + "\n");
					}
				}
			}
		} else {
			File tmpFile = new File(file.getPath());
			if (tmpFile.getName().equals(fileName)) {
//				System.out.println("路径为:" + file.getPath());
				sb.append(file.getPath() + "\n");
			}
		}
	}

	// 遍历文件操作
	public static int countNum(String path) {
		int num = 0;
		File file = new File(path);
		// 如果这个路径是文件夹
		if (file.isDirectory()) {
			// 获取路径下的所有文件
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				// 如果还是文件夹 递归获取里面的文件 文件夹
				if (files[i].isDirectory()) {
//					System.out.println("目录:" + files[i].getPath());
					if (!files[i].getPath().endsWith("System Volume Information")) {
						num += countNum(files[i].getPath());
					}

				} else {
					num++;
//					File tmpFile = new File(files[i].getPath());
//					System.out.println("文件:" + files[i].getPath() + "    文件名:" + tmpFile.getName());
				}
			}
		} else {
//			File tmpFile = new File(file.getPath());
//			System.out.println("文件:" + file.getPath() + "    文件名:" + tmpFile.getName());
			num++;
		}

		return num;
	}

	// 对文件,对文件夹进行重命名
	public static void fileReameTo(String oldName, String newName) {
		// 想命名的原文件的路径
		File file = new File(oldName);
		// 将原文件更改为f:\a\b.xlsx,其中路径是必要的。注意
		file.renameTo(new File(newName));
	}

	public static void readFile(String filePath, int num) { // 需要读取文件的路径、读取的行数
		int n = 0;
		try {
			File f = new File(filePath);
			if (f.isFile() && f.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(f), "GBK");
				BufferedReader reader = new BufferedReader(read);
				String line;
				while (((line = reader.readLine()) != null) && n <= num) {
					System.out.println(line);
					n++;
				}
				read.close();
			}
		} catch (Exception e) {
			System.out.println("读取文件内容操作出错");
			e.printStackTrace();
		}
	}

	/*
	 * 这里出现乱码了, 换一种写法 public static void readFile(String pathName, int num) { //
	 * 需要读取文件的路径、读取的行数 try (FileReader reader = new FileReader(pathName);
	 * BufferedReader br = new BufferedReader(reader)) { // 建立一个对象,它把文件内容转成计算机能读懂的语言
	 * String line; int n = 0; while (((line = br.readLine()) != null) && (n <=
	 * num)) { // 一次读入一行数据 System.out.println(line); n++; } } catch (IOException e)
	 * { e.printStackTrace(); } }
	 */

	// 拷贝文件夹
	public static void copyDir(String sourcePath, String newPath) throws IOException {
		File file = new File(sourcePath);
		String[] filePath = file.list();

		if (!(new File(newPath)).exists()) {
			(new File(newPath)).mkdir();
		}

		for (int i = 0; i < filePath.length; i++) {
			if ((new File(sourcePath + File.separator + filePath[i])).isDirectory()) {
				copyDir(sourcePath + File.separator + filePath[i], newPath + File.separator + filePath[i]);
			}

			if (new File(sourcePath + File.separator + filePath[i]).isFile()) {
				copyFile(sourcePath + File.separator + filePath[i], newPath + File.separator + filePath[i]);
			}

		}
	}

	// 拷贝文件操作,源文件 和 目标文件
	public static void copyFile(String oldPath, String newPath) throws IOException {
		File source = new File(oldPath);
		File dest = new File(newPath);

		FileChannel inputChannel = null;
		FileChannel outputChannel = null;
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream(source);
			inputChannel = fileInputStream.getChannel();

			fileOutputStream = new FileOutputStream(dest);
			outputChannel = fileOutputStream.getChannel();

			outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); // 复制读取
		} finally { // 关闭打开的文件,避免内存泄漏
			fileInputStream.close();
			fileOutputStream.close();
			inputChannel.close();
			outputChannel.close();
		}
	}

}

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值