操作系统课程设计报告
题目: 为Linux系统设计一个简单的二级文件系统
指导老师:
时间:2012.8.30
一 课程设计的目的
课程设计目的使学生熟悉文件管理系统的设计方法;加深对所学各种文件操作的了解及其操作方法的特点。通过模拟文件系统的实现,深入理解操作系统中文件系统的理论知识, 加深对教材中的重要算法的理解。同时通过编程实现这些算法,更好地掌握操作系统的原理及实现方法,提高综合运用各专业课知识的能力。
二 课程设计的要求
1.可以实现下列几条命令: login 用户登录 dir 列目录 create 创建文件 delete 删除文件 open 打开文件 close 关闭文件 read 读文件 write 写文件
2.列目录时要列出文件名,物理地址,保护码和文件长度 3.源文件可以进行读写保护
三 算法设计
本次二级文件系统主要分为五大模块,分别是用户登录模块、新建目录模块、新建文件
模块、删除文件模块和读取文件模块。用户登录成功后才可以进行其他模块的操作。 1 用户登录模块
用户登录模块要求用户输入用户,当输入正确后才能进行其他模块操作,否则提示
用户名不存在并询问用户是否用此名进行注册。若用户名未满,则提示注册成功,否则提示用现有注册用户,进行登录,并返回到登录界面。用户登录模块流程图如图1所示。 开始 输入login命 否 输入用户 是 用户是否存在 否 是否注册 是 进行其他模块
图1 用户登录模块流程图
2新建文件模块
新建文件模块是在用户出入create指令后进行的,进入后会要求用户输入文件名,并
判断文件名是否存在,若没有则在要求用户输入文件读写权限,否则重新输入新的文件名。新建文件模块流程图如图2所示。 开始 输入create命令 输入文件名 是 文件名是否存在 否 输入权限 图2 新建文件流程图
3 删除文件模块
删除文件模块是根据用户鼠标右击时选择到的节点来确定要删除节点的名字与路径,然
后判断该节点是目录还是文件。若是文件则直接删除文件,若是目录则进入该目录再删除其全部文件。删除文件模块流程图如图4所示。 开始 输如open 文件名 文件名是否存在 否 提示无此文件 是 输入权限
图4 删除文件模块流程图
4读取文件模块
读取文件模块,要求用户要在文件打开的前提下,将磁盘中的内容读取到内存中。读取
文件流程图如图5所示。
开始 Open 文件 输如read 文件名 文件是否存在 是 文件是否已打开 否 显示文件内容 图5 读取文件模块流程图
提示文件未打开 5 写入文件模块
写入文件模块,思路与读取文件模块将本相同,只是添加了对读写权限的判断。 6 遍历文件
遍历文件,根据在用户登陆时,记录的值,在二维数组中,找到用户的所有文件对象,将相应的必须属性全部打印出来。
四 程序源代码
1. 文件对象相关代码 package com.file;
import java.io.Serializable;
public class FilePro implements Serializable{
String filename; String content; String username;
}
int flag; int protect;
public FilePro(String filename,String username,String content,int flag,int protect){ }
this.filename = filename; this.username = username; this.content = content; this.flag = flag; this.protect = protect;
2. 文件读写操作
package com.file; import java.io.*; import java.util.*;
public class FileCon {
Object [][] data = new Object[7][100]; ObjectInputStream in = null; ObjectOutputStream out = null; String path = \"D:\\\\file\"; public FileCon(){ }
public Object[][] readData(){ }
public void writeData(Object[][] data){
try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path))); out.writeObject(data); out.flush(); try {
in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path))); data = (Object[][]) in.readObject(); for(int i = 0;i<7;i++)
for(int j = 0;j<100;j++){
data[i][j] = new FilePro(\"\",null,\"\",1,0); }
} catch(EOFException e){ } catch (Exception e) { }
return data;
} catch (Exception e) { }
e.printStackTrace();
}
}
3.用户名操作
public class UserCon { }
List public void writeUser(List try { out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path))); out.writeObject(list); out.flush(); try { in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path))); list = (List return list; } catch (Exception e) { } e.printStackTrace(); 4.主程序 package com.file; import java.util.*; public class FileSystem { public FileSystem() { data = fc.readData(); user = uc.readUser(); int currentuser = 0; Object[][] data = new Object[7][100]; FileCon fc = new FileCon(); List String[] cmd = new String[2]; } public static void main(String[] args) { } FileSystem fs = new FileSystem(); fs.help(); public void help() { System.out.println(\"欢迎使用该文件系统\"); System.out.print(\"create \"); System.out.println(\"创建文件\"); System.out.print(\"dir \"); System.out.println(\"列目录文件\"); System.out.println(\"exit 退出系统\"); } System.out.println(\"以下命令需加文件名\"); System.out.println(\"eg:open ***\"); System.out.print(\"open \"); System.out.println(\"打开文件\"); System.out.print(\"close \"); System.out.println(\"关闭文件\"); System.out.print(\"read \"); System.out.println(\"读文件\"); System.out.print(\"write \"); System.out.println(\"写文件\"); System.out.print(\"delete \"); System.out.println(\"删除文件\"); command(); public void command() { System.out.print(\"root:>\"); String comd = null; Scanner input = input = new Scanner(System.in); comd = input.nextLine(); String[] cmd = new String[2]; cmd = comd.split(\" \"); if (cmd[0].equals(\"login\")) login(); else if (cmd[0].equals(\"create\")) create(); else if (cmd[0].equals(\"dir\")) dir(); } else if (cmd[0].equals(\"delete\")) delete(cmd[1]); else if (cmd[0].equals(\"open\")) open(cmd[1]); else if (cmd[0].equals(\"close\")) close(cmd[1]); else if (cmd[0].equals(\"read\")) read(cmd[1]); else if (cmd[0].equals(\"write\")) write(cmd[1]); else if (cmd[0].equals(\"exit\")){ System.out.println(\"退出系统!\"); System.exit(0);} else System.out.println(\"指令错误!!\"); command(); public void login() { boolean f = false; System.out.println(\"请输入用户名:\"); Scanner input = input = new Scanner(System.in); String username = input.next(); for (int i = 0; i < user.size(); i++) { } if (user.get(i).equals(username)) { } System.out.println(\"登陆成功!!\"); currentuser = i; f = true; break; if (!f) { System.out.println(\"该用户不存在,是否以此用户名注册?y注册,其他返回\"); String cho = input.next(); if (cho.equals(\"y\")) { } if (user.size() == 7) System.out.println(\"对不起用户已满,请利用其他已注册账户登录\"); else { } login(); user.add(username); uc.writeUser(user); System.out.println(\"注册成功!请重新登录\"); // 创建文件 public void create() { do{ fl = false; Scanner input = input = new Scanner(System.in); boolean f = true; boolean fl = false; String filename = null; // 目录 public void dir() { } System.out.println(\"文件名\\" + \"用户名\\" + \"物理地址\\" + \"保护码\\" + \"文件长度\"); for (int i = 0; i < 100; i++) { } command(); FilePro fp1 = (FilePro) data[currentuser][i]; if (!fp1.filename.equals(\"\")) System.out.println(fp1.filename + \"\\" + fp1.username + \"\\" + currentuser + i + \"\\" + fp1.protect + \"\\" + fp1.content.length()); } } command(); System.out.print(\"请输入文加名:\"); filename = input.next(); for (int i = 0; i < 100; i++) { } FilePro fp1 = (FilePro) data[currentuser][i]; if (fp1.filename.equals(filename)) { } System.out.println(\"文件已存在!!\"); fl = true; break; }while(fl); System.out.print(\"请输入权限:\"); int protect = input.nextInt(); FilePro fp = new FilePro(filename, user.get(currentuser), \"\", 1, protect); for (int i = 0; i < 100; i++) { FilePro fp1 = (FilePro) data[currentuser][i]; } } if (fp1.filename.equals(\"\")) { } data[currentuser][i] = fp; fc.writeData(data); System.out.println(\"创建成功!!\"); f = false; break; if (f) { } command(); System.out.println(\"磁盘已满\"); // 删除文件 public void delete(String file) { } boolean f = true; for (int i = 0; i < 100; i++) { } if (f) { } command(); System.out.println(\"无此文件\"); FilePro fp1 = (FilePro) data[currentuser][i]; if (fp1.filename.equals(file)) { } fp1.filename = \"\"; fp1.content = null; fp1.flag = 1; fp1.username = null; fc.writeData(data); System.out.println(\"删除成功!!\"); f = false; break; // 打开文件 public void open(String file) { boolean f = true; for (int i = 0; i < 100; i++) { FilePro fp1 = (FilePro) data[currentuser][i]; if (fp1.filename.equals(file)) { if (fp1.flag == 0) System.out.println(\"文件已打开!\"); } } } else { } f = false; break; fp1.flag = 0; System.out.println(\"文件打开成功!!\"); if (f) { } command(); System.out.println(\"无此文件\"); // 关闭文件 public void close(String file) { } boolean f = true; for (int i = 0; i < 100; i++) { } if (f) { } command(); System.out.println(\"无此文件\"); FilePro fp1 = (FilePro) data[currentuser][i]; if (fp1.filename.equals(file)) { } if (fp1.flag == 1) System.out.println(\"文件未打开!\"); else { } f = false; break; fp1.flag = 1; System.out.println(\"文件关闭成功!!\"); // 读文件 public void read(String file) { boolean f = true; for (int i = 0; i < 100; i++) { FilePro fp1 = (FilePro) data[currentuser][i]; if (fp1.filename.equals(file)) { if (fp1.flag == 1) System.out.println(\"文件未打开!请先将文件打开!\"); } } } } else { } f = false; break; System.out.println(fp1.content); if (f) { } command(); System.out.println(\"无此文件\"); // 写文件 public void write(String file) { } Scanner input = input = new Scanner(System.in); boolean f = true; for (int i = 0; i < 100; i++) { } if (f) { } command(); System.out.println(\"无此文件\"); FilePro fp1 = (FilePro) data[currentuser][i]; if (fp1.filename.equals(file)) { } if (fp1.flag == 1) System.out.println(\"文件未打开!请先将文件打开!\"); else { } f = false; break; if (fp1.protect != 0) System.out.println(\"对不起,您没有写入的权限!\"); else { } System.out.println(\"请输入要写入的内容:\"); String ss = input.next(); fp1.content = fp1.content + ss; fc.writeData(data); System.out.println(\"写入成功\"); 五 程序运行截图 六 心得体会 对于本次操作系统课程设,由于对二级文件的内容比较陌生,刚起步阶段花了很大时间去思考,但是仍然还是比较模糊。当完成设计时,感觉还是不是太了解,总感觉得到的和要求的有些差别,不过对二级文件系统也有了进一步的了解,并且还对操作系统应用有了更深入的认识。总体来说,这次课程下来,发现自己的,语言基础掌握的一点也不好,让我遇到了许多的问题。
因篇幅问题不能全部显示,请点此查看更多更全内容