显示命令提示符“$”,并根据输入命令完成相应的文件操作: MD(创建子目录):创建目录文件,并在父目录文件中增加目录项。 RD(删除子目录):搜索所要删除的目录是否为空目录,若是则删除。 MK(创建空文件):创建指定大小的文件,并在父目录中添加文件名称;还应对FAT表进行适当修改。 DEL(删除文件):如果所要删除的文件存在,则删除,同时修改父目录内容;还应对FAT表进行适当修改。Java代码实
时间: 2023-12-06 13:44:13 浏览: 120
目录和文件操作命令——md-DOS操作系统
下面是一个基于Java的简单命令行文件管理系统,支持MD、RD、MK、DEL等基本文件操作:
```java
import java.util.*;
public class FileManager {
private static final int BLOCK_SIZE = 16; // 每个磁盘块的大小
private static final int BLOCK_NUM = 1024; // 磁盘块的总数
private static final int FAT_SIZE = BLOCK_NUM; // FAT表的大小与磁盘块总数相同
private static final Map<String, Integer> commands = new HashMap<>(); // 命令列表
private static String currentDir = "/"; // 当前目录
private static int[] fat = new int[FAT_SIZE]; // FAT表
private static List<FileEntry> directory = new ArrayList<>(); // 目录
static {
commands.put("MD", 1);
commands.put("RD", 1);
commands.put("MK", 2);
commands.put("DEL", 1);
commands.put("DIR", 0);
commands.put("CD", 1);
commands.put("EXIT", 0);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
init();
while (true) {
System.out.print("$");
String input = scanner.nextLine().trim();
String[] tokens = input.split(" ");
String command = tokens[0].toUpperCase();
if (!commands.containsKey(command)) {
System.out.println("Invalid command.");
continue;
}
int argNum = commands.get(command);
if (tokens.length - 1 != argNum) {
System.out.println("Invalid arguments.");
continue;
}
switch (command) {
case "MD":
createDir(tokens[1]);
break;
case "RD":
deleteDir(tokens[1]);
break;
case "MK":
createFile(tokens[1], Integer.parseInt(tokens[2]));
break;
case "DEL":
deleteFile(tokens[1]);
break;
case "DIR":
showDir();
break;
case "CD":
changeDir(tokens[1]);
break;
case "EXIT":
return;
}
}
}
// 初始化文件系统,包括FAT表和根目录
private static void init() {
Arrays.fill(fat, -1);
directory.add(new FileEntry("/", true, 0, 0));
}
// 创建子目录
private static void createDir(String name) {
if (directory.stream().anyMatch(f -> f.getName().equals(name))) {
System.out.println("Directory already exists.");
return;
}
int block = findFreeBlock();
if (block == -1) {
System.out.println("No free space left on disk.");
return;
}
directory.add(new FileEntry(name, true, block, directory.size() - 1));
fat[block] = -2; // -2表示目录文件
}
// 删除子目录
private static void deleteDir(String name) {
Optional<FileEntry> entry = directory.stream().filter(f -> !f.isDeleted() && f.getName().equals(name)).findFirst();
if (!entry.isPresent()) {
System.out.println("Directory not found.");
return;
}
FileEntry dir = entry.get();
if (!dir.isDirectory()) {
System.out.println("Not a directory.");
return;
}
if (dir.getSize() > 0) {
System.out.println("Directory not empty.");
return;
}
for (int i = 0; i < BLOCK_SIZE; i++) {
fat[dir.getFirstBlock() + i] = -1; // 释放磁盘块
}
dir.setDeleted(true);
}
// 创建空文件
private static void createFile(String name, int size) {
if (directory.stream().anyMatch(f -> f.getName().equals(name))) {
System.out.println("File already exists.");
return;
}
if (size <= 0 || size > BLOCK_SIZE) {
System.out.println("Invalid file size.");
return;
}
int[] blocks = new int[size];
int prevBlock = -1;
for (int i = 0; i < size; i++) {
int block = findFreeBlock();
if (block == -1) {
System.out.println("No free space left on disk.");
// 回滚之前的分配
for (int j = 0; j < i; j++) {
fat[blocks[j]] = -1;
}
return;
}
if (prevBlock != -1) {
fat[prevBlock] = block;
}
blocks[i] = block;
prevBlock = block;
}
directory.add(new FileEntry(name, false, blocks[0], directory.size() - 1));
for (int block : blocks) {
fat[block] = -3; // -3表示数据块
}
}
// 删除文件
private static void deleteFile(String name) {
Optional<FileEntry> entry = directory.stream().filter(f -> !f.isDeleted() && f.getName().equals(name)).findFirst();
if (!entry.isPresent()) {
System.out.println("File not found.");
return;
}
FileEntry file = entry.get();
if (file.isDirectory()) {
System.out.println("Not a file.");
return;
}
for (int i = 0; i < BLOCK_SIZE; i++) {
fat[file.getFirstBlock() + i] = -1; // 释放磁盘块
}
file.setDeleted(true);
}
// 显示目录内容
private static void showDir() {
System.out.println("Directory of " + currentDir);
for (FileEntry entry : directory) {
if (!entry.isDeleted() && entry.getParent().equals(currentDir)) {
System.out.print(entry.isDirectory() ? "<DIR>" : " ");
System.out.printf(" %s\n", entry.getName());
}
}
}
// 切换当前目录
private static void changeDir(String name) {
if (name.equals("..")) {
int index = currentDir.lastIndexOf("/");
currentDir = currentDir.substring(0, index);
} else {
Optional<FileEntry> entry = directory.stream().filter(f -> !f.isDeleted() && f.isDirectory() && f.getName().equals(name) && f.getParent().equals(currentDir)).findFirst();
if (!entry.isPresent()) {
System.out.println("Directory not found.");
return;
}
currentDir += "/" + name;
}
}
// 查找空闲的磁盘块
private static int findFreeBlock() {
for (int i = 0; i < BLOCK_NUM; i++) {
if (fat[i] == -1) {
return i;
}
}
return -1;
}
// 文件目录项
private static class FileEntry {
private String name;
private boolean isDirectory;
private boolean deleted;
private int firstBlock;
private int parent;
public FileEntry(String name, boolean isDirectory, int firstBlock, int parent) {
this.name = name;
this.isDirectory = isDirectory;
this.firstBlock = firstBlock;
this.parent = parent;
}
public String getName() {
return name;
}
public boolean isDirectory() {
return isDirectory;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
public int getFirstBlock() {
return firstBlock;
}
public String getParent() {
return directory.get(parent).getName();
}
public int getSize() {
int size = 0;
if (!isDeleted()) {
if (isDirectory()) {
for (FileEntry entry : directory) {
if (!entry.isDeleted() && entry.isDirectory() && entry.getParent().equals(getName())) {
size++;
}
}
} else {
size = 1;
}
}
return size;
}
}
}
```
该文件管理系统采用FAT表管理磁盘空间,目录和文件信息保存在一个列表中。每个目录项包括文件名、是否为目录、第一个磁盘块的编号和父目录的索引。删除文件或目录时,只是将对应的目录项标记为已删除,实际的数据块并没有被清空,这样可以避免频繁地修改FAT表。每个文件的数据块根据大小动态分配,如果无法分配足够的空间,则分配失败。
阅读全文