filesystem.java~473~

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

JAVA~473~
866
字号
           System.out.println("复制整个文件夹内容操作出错");
           e.printStackTrace();
       }
   }

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

//“新建文件夹”菜单响应
    public void jMenuItem1_actionPerformed(ActionEvent e)
    {
        if(jTree1.getLastSelectedPathComponent().toString().equals("我的电脑"))
        {
           JOptionPane.showMessageDialog(null, " 不能在“我的电脑”上新建文件!", "",
                                         JOptionPane.ERROR_MESSAGE);
           return;
       }
       String temp_path = this.getPath();
       File temp_file = new File(temp_path);
       if(temp_file.isFile())
       {
           JOptionPane.showMessageDialog(null, " 不能在“文件”上新建目录!", "",
                                         JOptionPane.ERROR_MESSAGE);
           return;
       }
       String new_name = JOptionPane.showInputDialog(null,"请为新文件命名", "请输入文件名",JOptionPane.INFORMATION_MESSAGE);
       File t_file = new File(this.getPath()+"\\"+new_name);
       if (new_name == null)
           return;
       else if(new_name.equals(""))
       {
           JOptionPane.showMessageDialog(null," 文件名不能为空,本次操作不予以执行! ","",JOptionPane.ERROR_MESSAGE);
           return;
       }
       else if(t_file.exists())
       {
           JOptionPane.showMessageDialog(null," 此文件已经存在!请重新命名 ","",JOptionPane.ERROR_MESSAGE);
           return;
       }
       else
       {
           String file_path = this.getPath();
           File new_file = new File(file_path+ new_name);
           if(!new_file.exists())
           {
               new_file.mkdir();
               JOptionPane.showMessageDialog(null, "新建文件夹成功", "",
                                             JOptionPane.INFORMATION_MESSAGE);
           }
           else
           {
               JOptionPane.showMessageDialog(null,"名为“" + new_name + "”的文件已经存在,操作失败","",JOptionPane.INFORMATION_MESSAGE);
               return;
           }
           DefaultMutableTreeNode file_node = new DefaultMutableTreeNode(new_name);
           DefaultMutableTreeNode father_node = (DefaultMutableTreeNode)jTree1.getPathForLocation(x,y).getLastPathComponent();//父结点
           model.insertNodeInto(file_node,father_node, father_node.getChildCount());
           if(file_node.getChildCount() == 0)
           {
               DefaultMutableTreeNode null_node = new DefaultMutableTreeNode("");
               model.insertNodeInto(null_node,file_node,0);
           }
       }
   }

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

//“新建文件”菜单响应
    public void jMenuItem2_actionPerformed(ActionEvent e)
    {
        if(jTree1.getLastSelectedPathComponent().toString().equals("我的电脑"))
        {
           JOptionPane.showMessageDialog(null," 不能在“我的电脑”上新建文件! ","",JOptionPane.ERROR_MESSAGE);
           return;
        }
        String temp_path = this.getPath();
        File temp_file = new File(temp_path);
        if(temp_file.isFile())
        {
            JOptionPane.showMessageDialog(null," 不能在文件上新建文件! ","",JOptionPane.ERROR_MESSAGE);
            return;
        }
        String new_name = JOptionPane.showInputDialog(null,"请为新文件命名", "请输入文件名",JOptionPane.INFORMATION_MESSAGE);
        if(new_name.equals(""))
        {
            JOptionPane.showMessageDialog(null," 文件名不能为空,本次操作不予以执行! ","",JOptionPane.ERROR_MESSAGE);
            return;
        }
        String path = this.getPath() + "\\" + new_name;
        File f = new File(path);
        try
        {
            if  (!f.exists())
            {
                f.createNewFile();
                FileWriter myFile = new FileWriter(f);
                PrintWriter newFile = new PrintWriter(myFile);
                newFile.println("");
                newFile.close();
                myFile.close();
                JOptionPane.showMessageDialog(null, "新建文件成功", "",
                                              JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(null,
                                              "名为“" + new_name + "”的文件已经存在,操作失败",
                                              "",
                                              JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        }
        catch(Exception  ex)
        {
           System.out.println("新建目录操作出错");
        }
        DefaultMutableTreeNode file_node = new DefaultMutableTreeNode(new_name);
        DefaultMutableTreeNode father_node = (DefaultMutableTreeNode)jTree1.getPathForLocation(x,y).getLastPathComponent();
        model.insertNodeInto(file_node,father_node, father_node.getChildCount());
    }

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

//"删除"菜单响应
    public void jMenuItem6_actionPerformed(ActionEvent e)
    {
        int choice = JOptionPane.showConfirmDialog(null, " 真的要删除该文件? "," 请慎重考虑! ",JOptionPane.YES_NO_OPTION);
        if(choice == JOptionPane.NO_OPTION)
            return;
        DefaultMutableTreeNode f_node = (DefaultMutableTreeNode)jTree1.getPathForLocation(x,y).
                                             getParentPath().getLastPathComponent();
        if(f_node.getChildCount() == 1)
        {
            DefaultMutableTreeNode null_node = new DefaultMutableTreeNode("");
            model.insertNodeInto(null_node,f_node, f_node.getChildCount());
        }
        String del_path = this.getPath();
        this.delFolder(del_path);
        MutableTreeNode del_node = (DefaultMutableTreeNode) jTree1.
                                   getLastSelectedPathComponent();
        model.removeNodeFromParent(del_node);
    }

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

//“复制”菜单响应
    public void jMenuItem4_actionPerformed(ActionEvent e)
    {
        source_add = this.getPath();
        jMenuItem5.setEnabled(true);
        flag = false;
    }

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

//"粘贴"菜单响应
    public void jMenuItem5_actionPerformed(ActionEvent e)
    {
        if(flag == true)
        {
            pastepath = this.getPath();
            if (isf == true)
            {
                copyFile(oldpath, pastepath + "\\" + c_name);
                delFile(oldpath);
            }
            else
            {
                new File(pastepath+"\\"+c_name).mkdirs();
                copyFolder(oldpath, pastepath+"\\" + c_name);
                delFolder(oldpath);
            }
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(c_name);
            NewNode(pastepath+"\\"+c_name,node);
            DefaultMutableTreeNode f_node = (DefaultMutableTreeNode)jTree1.getPathForLocation(x,y).getLastPathComponent();
            model.insertNodeInto(node,f_node, f_node.getChildCount());
            model.removeNodeFromParent(cut_node);
            jMenuItem5.setEnabled(false);
        }
        else
        {
            if (jTree1.getLastSelectedPathComponent().toString().equals("我的电脑"))
            {
                JOptionPane.showMessageDialog(null, "不能在此粘贴文件", "",
                                              JOptionPane.ERROR_MESSAGE);
                return;
            }
            target_add = this.getPath();
            File s = new File(source_add);
            File t = new File(target_add);
            File temp = new File(target_add + "\\" + s.getName());
            if (temp.exists())
            {
                JOptionPane.showMessageDialog(null, " 文件已经存在! ", "",
                                              JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (t.isFile())
            {
                JOptionPane.showMessageDialog(null, " 不能在文件上粘贴文件! ", "",
                                              JOptionPane.ERROR_MESSAGE);
                return;
            }
            else
            {
                if (s.isFile())
                {
                    target_add = this.getPath() + "\\" + s.getName();
                    copyFile(source_add, target_add);
                }
                else
                {
                    new File(target_add + "\\" + s.getName()).mkdirs();
                    copyFolder(source_add, target_add + "\\" + s.getName());
                }
            }
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(s.getName());
            NewNode(target_add+"\\" + s.getName(),node);
            DefaultMutableTreeNode f_node = (DefaultMutableTreeNode) jTree1.getPathForLocation(x, y).getLastPathComponent();
            model.insertNodeInto(node, f_node, f_node.getChildCount());
        }
    }

///////////////////////////////////////////////////////////////////////////////////////////////
//“剪切”菜单响应
    public void jMenuItem3_actionPerformed(ActionEvent e)
    {
        cut_node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        oldpath = this.getPath();
        File f = new File(oldpath);
        c_name = f.getName();
        if (f.isFile())
            isf = true;
        flag = true;
        jMenuItem5.setEnabled(true);
    }

///////////////////////////////////////////////////////////////////////////////////////////
//打开作业

    public void jMenuItem7_actionPerformed(ActionEvent e)
    {
        open_path = this.getPath();
        File f = new File(open_path);
        if (f.isFile())
            new Textbook(open_path);
        else return ;
    }

//////////////////////////////////////////////////////////////////////////////////////////////
//设置文件属性

    public void jMenuItem9_actionPerformed(ActionEvent e)         //只读
    {
        File myfile = new File(this.getPath());
        myfile.setReadOnly();
    }

    public void jMenuItem10_actionPerformed(ActionEvent e)        //可写
    {
        File myfile = new File(this.getPath());
        myfile.setWritable(true);
    }

/////////////////////////////////////////////////////////////////////////////////////////////
//运行作业事件响应
    public void jMenuItem8_actionPerformed(ActionEvent e)
    {
        String path = this.getPath();
        File f = new File(path);
        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);
        }
    }
////////////////////////////////////////////////////////////////////////////////////////////////

    public static void main(String[] args)
    {
        new FileSystem();
    }

}

⌨️ 快捷键说明

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