package 文件管理;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/**
* @Author: yxc
* @date: 2019/11/29
*/
public class Demo {
public static void main(String[] args) throws IOException {
//初始化用户目录
MFD mfd = new MFD();
mfd.setSize(4);//初始化用户目录
while (mfd.getSize()<4){
System.out.print("新增用户:");//新增第一个用户
Scanner scanner = new Scanner(System.in);
String username = scanner.next();
User user1 = new User(username);//创建用户目录
mfd.addUser(user1); //放入MDF主目录
}
boolean flag1 = true;//定义逻辑控制标记
while (flag1) {
System.out.print("请输入用户名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
if ((mfd.Contains(name) == null)) {
System.out.println("用户名不存在");
continue;
}
User user = mfd.Contains(name);
System.out.println("当前目录:");
for(int i = 0; i<user.getFilefold().size(); i++){
System.out.println(user.getFilefold().get(i).getFile().getName()+'\t'+"权限"+user.getFilefold().get(i).getRwx()+'\t'
+"长度"+user.getFilefold().get(i).getFile().length()+"字节");
}
boolean flag = true;//定义逻辑控制标记
while(flag){
System.out.printf("%-14s%-15s%-12s\n","1)新建文件","2)删除文件","3)打开文件");
System.out.printf("%-12s%-15s%-12s\n","4)修改文件权限","5)读出文件","6)写入文件");
System.out.printf("%-14s%-13s%-12s\n","7)关闭文件","8)保存打印并退出","0)不保存退出");
System.out.print("请输入指令: ");
int target = Integer.parseInt(scanner.next());
if(target<0||target>8){
System.out.println("输入有误(0-8):");
continue;
}
//根据指令运行相应功能
switch(target){
//直接退出不保存
case 0:{
System.out.print("确定退出?(Y/N):");
String exit = scanner.next();
if(exit.equals("Y")||exit.equals("y")){
flag = false;
System.out.println("已退出");
System.out.println();
user.remove();//删除用户目录里所有文件
break;
}else{
break;
}
}
//新建文件
case 1:{
if(user.getFilefold().size()==user.getFoldsize()){
System.out.println("文件数目达到上限!");
break;
}else{
System.out.print("请输入文件名:");
String filename = scanner.next();
System.out.print("请输入文件保护码(读写执行)1表示允许,0表示不允许:");
String rwx = scanner.next();
java.io.File filetemp = new java.io.File("E:\\IDEACode\\算法\\src\\文件管理\\文件\\" +name+"\\"+filename);
filetemp.
Java版-文件管理实验(操作系统)
最新推荐文章于 2024-07-04 16:41:05 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)