帮我写一java语言实现的简单文件管理系统
时间: 2023-12-18 07:04:05 浏览: 46
好的,下面是一个简单的Java文件管理系统示例代码,可以实现文件的创建、删除、修改、查看等基本操作。请注意,这个示例只是一个简单的文件管理系统,可能还需要进行扩展以满足更多的需求。
```java
import java.io.File;
import java.util.Scanner;
public class SimpleFileManager {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎使用文件管理系统,请输入命令:");
while (true) {
String command = scanner.nextLine();
String[] params = command.split(" ");
if (params.length == 0) {
System.out.println("命令为空,请重新输入!");
continue;
}
String action = params[0];
if ("exit".equals(action)) {
System.out.println("退出文件管理系统!");
break;
} else if ("list".equals(action)) {
String path = ".";
if (params.length > 1) {
path = params[1];
}
File directory = new File(path);
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
System.out.println(file.getName());
}
}
} else if ("create".equals(action)) {
if (params.length < 2) {
System.out.println("文件名不能为空,请重新输入!");
continue;
}
String fileName = params[1];
File file = new File(fileName);
try {
boolean result = file.createNewFile();
if (result) {
System.out.println("文件创建成功!");
} else {
System.out.println("文件创建失败!");
}
} catch (Exception e) {
System.out.println("文件创建失败:" + e.getMessage());
}
} else if ("delete".equals(action)) {
if (params.length < 2) {
System.out.println("文件名不能为空,请重新输入!");
continue;
}
String fileName = params[1];
File file = new File(fileName);
if (file.exists()) {
boolean result = file.delete();
if (result) {
System.out.println("文件删除成功!");
} else {
System.out.println("文件删除失败!");
}
} else {
System.out.println("文件不存在!");
}
} else if ("rename".equals(action)) {
if (params.length < 3) {
System.out.println("文件名不能为空,请重新输入!");
continue;
}
String oldName = params[1];
String newName = params[2];
File oldFile = new File(oldName);
File newFile = new File(newName);
if (oldFile.exists()) {
boolean result = oldFile.renameTo(newFile);
if (result) {
System.out.println("文件重命名成功!");
} else {
System.out.println("文件重命名失败!");
}
} else {
System.out.println("文件不存在!");
}
} else {
System.out.println("无效的命令,请重新输入!");
}
}
}
}
```
该示例包含以下命令:
- `list [path]`:列出指定路径下的文件列表,默认是当前目录。
- `create filename`:在当前目录下创建一个指定名称的文件。
- `delete filename`:删除当前目录下指定名称的文件。
- `rename oldname newname`:将当前目录下指定名称的文件重命名为新的名称。
- `exit`:退出文件管理系统。
您可以根据需要进行修改和扩展,例如添加文件内容读取、文件夹操作等功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)