操作系统课程设计-文件管理实验报告

 

操作系统课程实验报告

2013~2014年度  1学期

院系:  

学号:   

姓名:    

任课教师:            成绩评定:

实验一题目:文件管理

完成日期:年 月 日

1、实验目的

了解文件管理的功能和任务,理解文件系统组成和特点,熟悉文件系统的访问和操作。实验要求用高级语言编写和调试一个简单的模拟文件管理程序。加深理解有关盘块的分配与回收、目录管理等的具体实施策略。

2.、实验内容

   模拟一个资源管理器进行文件操作,包括建立和删除目录、建立和删除文件等基本文件操作。建立相应的数据结构(如:位示图等),模拟盘块管理。可以参照图6界面进行设计。

3、算法设计

1)、定义主面板MainFrame,布局好各个控件,并初始化

         /*

        * 往node节点下添加一个子节点obj;

        */

       publicvoid addChild(Object obj, DefaultMutableTreeNode node) {

              if (obj != null && node != null) {

                     DefaultMutableTreeNode temp = new DefaultMutableTreeNode(obj);

                     if (node.getAllowsChildren())

                            node.add(temp);

                     if (!((String) obj).equals("A:\\") && ((String) obj).length() <= 3)// 防止读取A软驱,会出现异常;用于初始用的;

                            addChildren(cmd.listAll((String) obj), temp);

              }

       }

       /*

        * 在node节点下添加数组children;

        */

       publicvoid addChildren(String[] children, DefaultMutableTreeNode node) {

              if (children != null && node != null) {

                     for (int i = 0; i < children.length; i++) {

                            addChild(children[i], node);

                     }

              }

       }

       /*

        * 对树的节点进行预提取;

        */

       publicvoid addPrefetchChildren(String path, DefaultMutableTreeNode node) {

              addChildren(cmd.listDirectory(path), node);

       }

       /*

        * 对路径路径进行连接;(已经获得了所有的整个路径,需要量转化)

        */

       public String toFilePath(String str) {

              // 先去掉头尾的[];

              String pa = str.substring(1, str.length() - 1);

              String[] temp = pa.split(", ");

              String path = "";

              for (int i = 1; i < temp.length; i++) {

                     if (!path.endsWith("\\") && !path.equals(""))// 不为空是为去根节点;

                            path += "\\";

                     path += temp[i];

              }

              return path;

       }

       public String toPFilePath(String str) {

              // 先去掉头尾的[];

              String pa = str.substring(1, str.length() - 1);

              String[] temp = pa.split(", ");

              String path = "";

              for (int i = 1; i < temp.length - 1; i++) {

                     if (!path.endsWith("\\") && !path.equals(""))// 不为空是为去根节点;

                            path += "\\";

                     path += temp[i];

              }

              return path;

       }

       publicclass ExpandListener implements TreeWillExpandListener {

              /*

               * 树展开及收缩监听;

               */

              private MainFrame mainFrame = null;

              public ExpandListener(MainFrame mainFrame) {

                     this.mainFrame = mainFrame;

              }

              publicvoid treeWillExpand(TreeExpansionEvent event) {

                     // 对节点的路径进行转化

                     String path = toFilePath(event.getPath().toString());

                     TreePath treePath = event.getPath();

                     DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath

                                   .getLastPathComponent();

                     // System.out.println("所展开节点的路径:" + path);

                     // System.out.println(treePath);

                     if (node.getDepth() < 2) {

                            Enumeration children = node.children();

                            String filePath = "";

                            while (children.hasMoreElements()) {

                                   DefaultMutableTreeNode temp = (DefaultMutableTreeNode) children

                                                 .nextElement();

                                   filePath = "";

                                   filePath = path;

                                   if (!filePath.endsWith("\\"))

                                          filePath += "\\";

                                   filePath += temp.toString();

                                   // System.out.println("temp=" +filePath);

                                   mainFrame.addPrefetchChildren(filePath, temp);

                            }

                     }

              }

2)、添加功能“添加文件(夹)addframe()”、“修改文件(夹)mvframe()”

     publicvoid addframe() {

              JFrame addFrame = new JFrame();

              JLabel jlbl = new JLabel("请输入要添加的文件(夹)名:");

              addrs = new JLabel("");

              addrs.setBounds(180, 10, 100, 25);

              jlbl.setBounds(10, 10, 170, 25);

              addfile = new JTextField();

              addfile.setBounds(10, 40, 260, 25);

              btnaddf = new JButton("添加文件");

              btnaddd = new JButton("添加文件夹");

              btnaddf.setBounds(20, 80, 100, 25);

              btnaddd.setBounds(160, 80, 100, 25);

              btnaddf.addActionListener(this);

              btnaddd.addActionListener(this);

              addFrame.add(jlbl);

              addFrame.add(addrs);

              addFrame.add(addfile);

              addFrame.add(btnaddf);

              addFrame.add(btnaddd);

              addFrame.setBounds(400, 350, 300, 150);

              addFrame.setTitle("添加文件(夹)");

              addFrame.setLayout(null);

              addFrame.setVisible(true);

       }

       publicvoid mvframe() {

              JFrame mvFrame = new JFrame();

              JLabel jlbl = new JLabel("请输入修改后的文件名:");

              mvrs = new JLabel("");

              mvrs.setBounds(160, 10, 140, 25);

              jlbl.setBounds(10, 10, 170, 25);

              mvfile = new JTextField();

              mvfile.setBounds(10, 40, 260, 25);

              btnmvf = new JButton("修改文件名");

              btnmvd = new JButton(" 修改文件夹名");

              btnmvf.setBounds(10, 80, 120, 25);

              btnmvd.setBounds(150, 80, 120, 25);

              btnmvf.addActionListener(this);

              btnmvd.addActionListener(this);

              mvFrame.add(jlbl);

              mvFrame.add(mvrs);

              mvFrame.add(mvfile);

              mvFrame.add(btnmvf);

              mvFrame.add(btnmvd);

              mvFrame.setBounds(400, 350, 300, 150);

              mvFrame.setTitle("修改文件(夹)名");

              mvFrame.setLayout(null);

              mvFrame.setVisible(true);

       }

}

3)显示文件

        * 显示系统中的所有盘符;

       public String[] ListDisks() {

              File roots[] = File.listRoots();// 根盘符;

              String disks[] = new String[roots.length];

              for (int i = 0; i < roots.length; i++) {

                     disks[i] = roots[i].toString();

              }

              return disks;

       }

        * 获得路径path下的文件;

       public String[] listAll(String path) {

              try {

                     File f = new File(path);

                     String[] fileName;

                     String tmp = null;

                     mainFrame.fileshow.setText(null);

                     mainFrame.filestyle.setText(null);

                     if (f.isDirectory()) {

                            fileName = f.list();

                            // System.out.println("共有" + fileName.length + "个文件");

                            for (int i = 0; i < fileName.length; i++) {

                                   mainFrame.fileshow.append(fileName[i] + '\n');

                                   tmp = path + '\\' + fileName[i];

                                   // System.out.println(tmp);

                                   if (listDirectory(tmp) != null) {

                                          mainFrame.filestyle.append("文件夹\n");

                                   } else {

                                          mainFrame.filestyle.append("文件\n");

                                   }

                            }

                            return fileName;

                     } elseif (f.isFile()) {

                            System.out.println("这是一个文件");

                            returnnull;

                     } else {

                            // System.out.println(path);

                            returnnull;

                     }

              } catch (Exception e) {

                     returnnull;

              }

       }

       public String[] listDirectory(String path) {

              File f = new File(path);

              String[] fileName;

              if (f.isDirectory()) {

                     fileName = f.list();

                     return fileName;

              } else {

                     // System.out.println(path + "是文件");

                     returnnull;

              }

       }

        * 进行md操作;md <目录名> 功能: 创建新目录

       publicvoid md(String directory) {

              if (!mainFrame.currentPath.equals("")) {

                     String temp = mainFrame.currentPath + "\\" + directory;

                     File newFile = new File(temp);

                     if (!newFile.exists()) {

                            try {

                                   if (newFile.isDirectory() == false) {

                                          newFile.mkdirs();

                                          System.out.println("文件夹创建成功!");

                                   } else {

                                          System.out.println("文件夹创建出错!");

                                   }

                            } catch (Exception e) {

                                   System.out.println("出错信息:" + e.getMessage());

                            }

                     } else {

                            System.out.println("文件夹已经存在");

                     }

              }

       }

        * 进行rd操作;rd <目录名> 功能: 删除目录;

       publicvoid del() {

              String temp = mainFrame.currentPath;

              File file = new File(temp);

              if (file.exists()) {

                     if (file.delete()) {

                            mainFrame.fileshow.setText("文件(夹)删除成功!");

                     } else {

                            mainFrame.fileshow.setText("文件(夹)删除操作出错!");

                     }

              } else {

                     mainFrame.fileshow.setText("文件(夹)不存在");

              }

       }

       /*

        * 进行edit操作:edit <文件名> 功能: 新建文件

        */

       publicvoid edit(String file) {

              if (!mainFrame.currentPath.equals("")) {

                     String temp = mainFrame.currentPath + "\\" + file;

                     File newFile = new File(temp);

                     if (newFile.exists()) {

                            mainFrame.addrs.setText("文件已经存在!");

                            System.out.println("文件已经存在!");

                     } else {

                            try {

                                   newFile.createNewFile();

                                   mainFrame.addrs.setText("文件创建成功!");

                                   System.out.println("文件创建成功!");

                            } catch (Exception e) {

                                   System.out.println("文件创建失败:" + e.getMessage());

                            }

                     }

              }

       }

      

       publicvoid mvf(String file){

              if (!mainFrame.PPath.equals("")) {

                     String temp = mainFrame.PPath + "\\" + file;

                     File newFile = new File(mainFrame.currentPath);

                     if (newFile.exists()) {

                             if(newFile.renameTo(new File(temp))==true){

                              mainFrame.mvrs.setText("修改文件名成功!");

                        }else{

                               mainFrame.mvrs.setText("存在同名文件!");

                             }

                     }

              }

       }

       publicvoid mvd(String dir){

              if (!mainFrame.PPath.equals("")) {

                     String temp = mainFrame.PPath + "\\" + dir;

                     File newFile = new File(mainFrame.currentPath);

                     if (newFile.exists()) {

                             if(newFile.renameTo(new File(temp))==true){

                              mainFrame.mvrs.setText("修改文件夹名成功!");

                        }else{

                               mainFrame.mvrs.setText("存在同名文件夹!");

                             }

                     }

              }

       }

}

4、运行与测试

   运行时,弹出主界面

双击文件盘C

在E盘路径添加文件夹操作系统,再添加文件操作系统

如果文件已存在会显示

可以更改文件名称

删除文件,不过有个BUG,要文件一层层删,才可以删除。

可以显示目录路径

5、总结与心得

     我们每天都在用文件系统,已经习惯了有文件路径寻找的方便,每次找文件时,感觉好容易,可实际去设计这个程序时,却很难。用树形结构图去设计,要有较好的全局思想,而且一些节点的分配。在设计此系统时,发觉自己对文件系统理解得不够透彻。不知道如何来具体设计这个实验,后来耐心回到本学期学习的课本内容上,再次翻看了大二第一学期所学的数据结构的书,才觉悟对文件系统的理解,总而言之,操作系统的设计,不仅提升了自己的程序设计及编写技巧,更大大地加深了对操作系统的文件管理的了解。

相关推荐