当前位置:文档之家› 操作系统课程设计--为Linux系统设计一个简单的二级文件系统

操作系统课程设计--为Linux系统设计一个简单的二级文件系统

操作系统课程设计--为Linux系统设计一个简单的二级文件系统操作系统课程设计报告题目: 为Linux系统设计一个简单的二级文件系统指导老师:时间:2012.8.30一课程设计的目的课程设计目的使学生熟悉文件管理系统的设计方法;加深对所学各种文件操作的了解及其操作方法的特点。

通过模拟文件系统的实现,深入理解操作系统中文件系统的理论知识, 加深对教材中的重要算法的理解。

同时通过编程实现这些算法,更好地掌握操作系统的原理及实现方法,提高综合运用各专业课知识的能力。

二课程设计的要求1.可以实现下列几条命令:login用户登录dir 列目录create创建文件delete 删除文件open 打开文件close 关闭文件read 读文件write 写文件2.列目录时要列出文件名,物理地址,保护码和文件长度3.源文件可以进行读写保护三算法设计本次二级文件系统主要分为五大模块,分别是用户登录模块、新建目录模块、新建文件模块、删除文件模块和读取文件模块。

用户登录成功后才可以进行其他模块的操作。

1 用户登录模块用户登录模块要求用户输入用户,当输入正确后才能进行其他模块操作,否则提示用户名不存在并询问用户是否用此名进行注册。

若用户名未满,则提示注册成功,否则提示用现有注册用户,进行登录,并返回到登录界面。

用户登录模块流程图如图1所示。

否是 否 是图1 用户登录模块流程图2新建文件模块新建文件模块是在用户出入create 指令后进行的,进入后会要求用户输入文件名,并判断文件名是否存在,若没有则在要求用户输入文件读写权限,否则重新输入新的文件名。

新建文件模块流程图如图2所示。

开输入login 输入用户用户是否存在进行其是否注册输入create 命令 开是否图2 新建文件流程图 3 删除文件模块 删除文件模块是根据用户鼠标右击时选择到的节点来确定要删除节点的名字与路径,然后判断该节点是目录还是文件。

若是文件则直接删除文件,若是目录则进入该目录再删除其全部文件。

删除文件模块流程图如图4所示。

否 输入文件名 文件名是否存在输入权开输如open 文件名 文件名是否存在提示无是图 4 删除文件模块流程图4读取文件模块 读取文件模块,要求用户要在文件打开的前提下,将磁盘中的内容读取到内存中。

读取文件流程图如图5所示。

是否输入权输如read 文件名 文件是否已打开 显示文提示文开Open文件是否存在图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;ername = 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(){for(int i = 0;i<7;i++)for(int j = 0;j<100;j++){data[i][j] = new FilePro("",null,"",1,0);}}public Object[][] readData(){try {in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));data = (Object[][]) in.readObject();} catch(EOFException e){} catch (Exception e) {}return data;}public void writeData(Object[][] data){try {out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));out.writeObject(data);out.flush();} catch (Exception e) {e.printStackTrace();}}}3.用户名操作public class UserCon {List<String> list = new ArrayList<String>();ObjectInputStream in = null;ObjectOutputStream out = null;String path = "D:\\user";public List<String> readUser(){try {in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));list = (List<String>) in.readObject();} catch(EOFException e){} catch (Exception e) {}return list;}public void writeUser(List<String> list){try {out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));out.writeObject(list);out.flush();} catch (Exception e) {e.printStackTrace();}}}4.主程序package com.file;import java.util.*;public class FileSystem {Object[][] data = new Object[7][100];FileCon fc = new FileCon();List<String> user = new ArrayList<String>();UserCon uc = new UserCon();String[] cmd = new String[2];int currentuser = 0;public FileSystem() {data = fc.readData();user = uc.readUser();}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 {user.add(username);uc.writeUser(user);System.out.println("注册成功!请重新登录");}login();}}command();}// 目录public void dir() {System.out.println("文件名\t" + "用户名\t" + "物理地址\t" + "保护码\t" + "文件长度");for (int i = 0; i < 100; i++) {FilePro fp1 = (FilePro) data[currentuser][i];if (!fp1.filename.equals(""))System.out.println(fp1.filename + "\t" + ername + "\t"+ currentuser + i + "\t" + fp1.protect + "\t"+ fp1.content.length());}command();}// 创建文件public void create() {Scanner input = input = new Scanner(System.in);boolean f = true;boolean fl = false;String filename = null;do{fl = false;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) {System.out.println("磁盘已满");}command();}// 删除文件public void delete(String file) {boolean f = true;for (int i = 0; i < 100; i++) {FilePro fp1 = (FilePro) data[currentuser][i];if (fp1.filename.equals(file)) {fp1.filename = "";fp1.content = null;fp1.flag = 1;ername = null;fc.writeData(data);System.out.println("删除成功!!");f = false;break;}}if (f) {System.out.println("无此文件");}command();}// 打开文件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 {fp1.flag = 0;System.out.println("文件打开成功!!");}f = false;break;}if (f) {System.out.println("无此文件");}command();}// 关闭文件public void close(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 {fp1.flag = 1;System.out.println("文件关闭成功!!");}f = false;break;}}if (f) {System.out.println("无此文件");}command();}// 读文件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 {System.out.println(fp1.content);}f = false;break;}}if (f) {System.out.println("无此文件");}command();}// 写文件public void write(String file) {Scanner input = input = new Scanner(System.in);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 {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("写入成功");}}f = false;break;}}if (f) {System.out.println("无此文件");}command();}}五程序运行截图六心得体会对于本次操作系统课程设,由于对二级文件的内容比较陌生,刚起步阶段花了很大时间去思考,但是仍然还是比较模糊。

相关主题