filesystem.java~473~

来自「操作系统课程设计」· JAVA~473~ 代码 · 共 866 行 · 第 1/3 页

JAVA~473~
866
字号
                    data.addElement(f_att);
                    tableModel1.addRow(data);
                }
            }
        });
//////////////////////////////////////////////////////////////////////////////////////

        jPopupMenu1 = new JPopupMenu();
        jPopupMenu1.add(jMenu1);
        jPopupMenu1.addSeparator();
        jPopupMenu1.add(jMenuItem3);
        jPopupMenu1.add(jMenuItem4);
        jPopupMenu1.add(jMenuItem5);
        jPopupMenu1.add(jMenuItem6);
        jPopupMenu1.addSeparator();
        jPopupMenu1.add(jMenuItem7);
        jPopupMenu1.add(jMenuItem8);
        jPopupMenu1.addSeparator();
        jPopupMenu1.add(jMenu2);
        jTree1.add(jPopupMenu1);
        jMenu1.add(jMenuItem1);
        jMenu1.add(jMenuItem2);
        jMenu2.add(jMenuItem9);
        jMenu2.add(jMenuItem10);
        jSplitPane1.add(jScrollPane2, JSplitPane.RIGHT);
        jSplitPane1.add(jScrollPane1, JSplitPane.LEFT);
        jScrollPane1.getViewport().add(jTree1);
        jScrollPane2.getViewport().add(jTable1);
        jPanel3.add(jSplitPane1, java.awt.BorderLayout.CENTER);
        jPanel1.add(jPanel3, new XYConstraints(0, 0, 441, 300));
        jPanel1.add(jPanel4, new XYConstraints(440, 0, 335, 300));
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
        this.getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
        jTree1.addMouseListener(new MouseAdapter()
        {
            public void mouseReleased(MouseEvent e)                  //右键响应
            {
                if (e.isPopupTrigger())
                {
                    jPopupMenu1.show(e.getComponent(), e.getX(), e.getY()); //弹出右键菜单
                    x = e.getX(); y = e.getY();
                }
                String path = ".\\";
                Object treepath[] = jTree1.getPathForLocation(x, y).getPath();
                for (int i = 1; i < treepath.length; i++)
                {
                    path += treepath[i].toString() + "\\";
                }
                File f = new File(path);
                if(f.isDirectory())
                {
                    jMenuItem7.setEnabled(false);
                    jMenuItem8.setEnabled(false);
                }
                else
                {
                    jMenuItem7.setEnabled(true);
                    jMenuItem8.setEnabled(true);
                }
            }

            public void mouseClicked(MouseEvent e2)       //双击响应
            {
                if (e2.getClickCount() == 2)
                {
                    x2 = e2.getX();
                    y2 = e2.getY();
                    String path = ".\\";
                    Object treepath[] = jTree1.getPathForLocation(x2, y2).getPath();
                    for (int i = 1; i < treepath.length; i++)
                    {
                        path += treepath[i].toString() + "\\";
                    }
                    File f = new File(path);
                    if (f.isDirectory())
                        return;
                    String fileName = f.getName();
                    String lastname = LastName.getLast(fileName);
                    if (lastname.equals(".job"))
                    {
                        int size = (int) f.length();
                        int r[] = mm.load_job(path);
                        if (r[1] == 1)
                        {
                            proc.Create_Ready(f.getName(), size, r);
                        }
                        else if (r[1] == 2)
                        {
                            proc.Create_Wait(f.getName(), size, r);
                        }
                    }
                    else
                    {
                        new Textbook(path);
                    }
                }
            }
        });
        this.validate();
        this.setVisible(true);
    }

////////////////////////////////////////////////////////////////////////////////////////////////

//新增加一个结点
    public void NewNode(String file_URL, DefaultMutableTreeNode dmtn)
    {
        File f = new File(file_URL);
        if (f.isDirectory())
        {
            File dir[] = f.listFiles();
            for (int i = 0; i < dir.length; i++)
            {
                if (dir[i].isDirectory())
                {
                    if(dir[i].listFiles().length==0)
                    {
                        DefaultMutableTreeNode null_node = new DefaultMutableTreeNode("");
                        DefaultMutableTreeNode dir_node = new DefaultMutableTreeNode(dir[i].getName());
                        dir_node.add(null_node);
                        dmtn.add(dir_node);
                        this.NewNode(dir[i].getPath(), dir_node );
                    }
                    else
                    {
                        DefaultMutableTreeNode dir_node = new DefaultMutableTreeNode(dir[i].getName());
                        dmtn.add(dir_node);
                        this.NewNode(dir[i].getPath(), dir_node);
                    }
                }
                else
                {
                    dmtn.add(new DefaultMutableTreeNode(dir[i].getName()));
                }
            }
        }
    }

////////////////////////////////////////////////////////////////////////////////////////////

//生成只含表头的空表
    public void init_table()
    {
        header = new Vector();
        header.addElement("文件名");
        header.addElement("大小");
        header.addElement("类型");
        header.addElement("属性");
        tableModel1 = new DefaultTableModel(header, 0);
        jTable1.setModel(tableModel1);
    }
//////////////////////////////////////////////////////////////////////////////////////////////

//获取所选节点的路径,并用字符串表示
    public String getPath()
    {
        String path = ".\\";
        Object treepath[] = jTree1.getPathForLocation(x, y).getPath();
        for (int i = 1; i < treepath.length; i++)
        {
            path += treepath[i].toString() + "\\";
        }
        return path;
    }

///////////////////////////////////////////////////////////////////////////////////////////
//删除文件夹
    public  void  delFolder(String  folderPath)
    {
        try
        {
            delAllFile(folderPath);  //删除里面所有内容
            File folder  =  new File(folderPath);
            folder.delete();  //删除空文件夹
        }
        catch  (Exception  e)
        {
            System.out.println("删除文件夹操作出错");
            e.printStackTrace();
        }
    }

//删除文件夹内文件
    public  void  delAllFile(String  path)
    {
        File  file  =  new  File(path);
        if (!file.isDirectory())
        {
            return;
        }
        int length = (int)file.list().length;
        String[]  tempList  =  file.list();
        File  temp  =  null;
        for (int i = 0; i < length; i++)
        {
            if  (path.endsWith(File.separator))
            {
                temp  =  new  File(path  +  tempList[i]);
            }
            else  {
                temp  =  new  File(path  +  File.separator  +  tempList[i]);
            }

            if (temp.isFile())
            {
                temp.delete();
            }
            else
            {
                delAllFile(path+"\\"+file.list()[i]);
                delFolder(path+"\\"+ file.list()[i]);
            }
        }
    }

//////////////////////////////////////////////////////////////////////////////////////////
//删除文件
    public  void  delFile(String  fileurl)
    {
       try
       {
           File myDelFile  =  new File(fileurl);
           myDelFile.delete();
       }
       catch(Exception  e)
       {
           System.out.println("删除文件操作出错");
           e.printStackTrace();
       }
   }

////////////////////////////////////////////////////////////////////////////////////////////
//复制文件
    public  void  copyFile(String  oldPath,  String  newPath)
    {
       try
       {
           int bytesum  =  0;
           int byteread  =  0;
           InputStream in  =  new  FileInputStream(oldPath);  //读入原文件
           FileOutputStream out =  new  FileOutputStream(newPath);
           byte[] buffer = new  byte[1024];
           while((byteread = in.read(buffer)) != -1)
           {
               bytesum  +=  byteread;  //字节数  文件大小
               out.write(buffer,  0,  byteread);
           }
           in.close();
       }
       catch  (Exception  e)
       {
           System.out.println("复制单个文件操作出错");
           e.printStackTrace();
       }
   }

//复制文件夹
   public  void  copyFolder(String  oldPath,  String  newPath)
   {
       try
       {
           File  f = new  File(oldPath);
           File  temp = null;
           for (int i = 0; i < f.list().length; i++)
           {
               temp = new  File( oldPath + "\\" + f.list()[i]);
               if(temp.isFile())
               {
                   FileInputStream  input  =  new  FileInputStream(temp);
                   FileOutputStream  output  =  new  FileOutputStream(newPath + "\\" + temp.getName());
                   byte[]  b  =  new  byte[1024 * 5];
                   int  len;
                   while ((len = input.read(b)) != -1)
                   {
                       output.write(b,  0,  len);
                   }
                   output.flush();
                   output.close();
                   input.close();
               }
               if(temp.isDirectory())
               {
                   //如果是子文件夹
                   copyFolder( oldPath+"\\"+f.list()[i] , newPath+"\\"+f.list()[i]);
               }
           }
       }
       catch  (Exception  e)
       {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?