Java DirTool

Java DirTool

Java 实验课要求写一个GUI的dirTool 结果别人就一个查看文件的功能,我就二了,写了代码貌似有点长······

这是GUI界面

命令行的使用和cmd中命令行差不多

dir D:\   //这里用绝对路径和相对路径都可以

..    //用于返回父目录

del Xiao  //用于删除文件或者文件夹

open qq.lnk  //用系统方式打开文件 相当于双击

mkdir dong //新建文件夹

命令就这些 如果感兴趣的话 还可以继续增加

下面贴代码吧····

package One;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.security.auth.Subject;

/**
 * ClassName: DirTool
 * ClassDescription:A class for Dir
 * @author VipXD
 * @version 1.0
 */

public class DirTool {
    /**
     * FuntionName: main
     * FuntionDescription:Test DirTool
     * @param
     * @return
     */
    public static void main(String[] args) {
        new DirFrame();
    }

}

/**
 * ClassName: DirFrame
 * ClassDescription:Main Frame of DirTool
 * @author VipXD
 * @version 1.0
 */
class DirFrame extends Frame implements ActionListener{

    TextArea info;
    TextField path;
    TextField commend;
    Button choose;
    GridBagLayout GB;
    FileDialog fd;
    String currentPath;
    String currentInfo;
    /**
     * FuntionName:DirFrame
     * FuntionDescription:construct DirFrame
     * @param
     * @return
     */
    public DirFrame() {
        super("DirTool");
        currentInfo = new String("No File");
        commend = new TextField("命令行:");
        commend.addActionListener(this);
        currentPath = new String("C:\\");
        choose = new Button("浏览");
        choose.addActionListener(this);
        info = new TextArea("文件信息");
        info.setEditable(false);
        path = new TextField("C:\\");
        path.addActionListener(this);
        path.setEditable(false);
        GB = new GridBagLayout();
        this.setLayout(GB);
        GridBagConstraints GBC = new GridBagConstraints();
        GBC.gridx = 0;
        GBC.gridy = 0;
        GBC.gridheight = 1;
        GBC.gridwidth = 4;
        GBC.fill = GridBagConstraints.BOTH;
        GBC.insets = new Insets(10,10,10,10);
        GBC.weightx = 1;
        GBC.weighty = 0;
        this.add(path,GBC);
        GBC.gridx = 5;
        GBC.gridwidth = 1;
        GBC.weightx = 0;
        this.add(choose,GBC);
        GBC.gridx = 0;
        GBC.gridy = 2;
        GBC.gridwidth = 6;
        this.add(commend,GBC);
        GBC.gridx = 0;
        GBC.gridy = 3;
        GBC.gridwidth = 6;
        GBC.gridheight = 8;
        GBC.weightx = 1;
        GBC.weighty = 1;
        this.add(info,GBC);
        this.setSize(500,450);
        //this.setResizable(false);
        addWindowListener(new HandleWin());
        fd = new FileDialog(this);
        show();
    }
    
    /**
     * FuntionName: actionperformed
     * FuntionDescription:respond for commend
     * @param Action Event
     * @return
     */
    @Override
    public void actionPerformed(ActionEvent e){
        try {
            if(e.getSource()==choose){
                fd.show();
                if(fd.getDirectory()!=null && fd.getFile()!=null){
                    currentPath = fd.getDirectory() + fd.getFile();
                    path.setText(currentPath);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    File file = new File(currentPath);
                    currentInfo = "Last Modified\t\t\tType\t\tSize(Bit)\t\tName\n";
                    currentInfo += sdf.format(new Date(file.lastModified()));
                    currentInfo += "\t\t"
                                    + "File\t\t" + file.length() + "\t\t"
                                    + file.getName();
                    currentInfo += "\n";
                    info.setText(currentInfo);
                    
                }
            }
            
            if(e.getSource()==commend){
                if(commend.getText().equals("dir")){
                    info.setText(Info(new File(currentPath)));
                    commend.setText("");
                    
                }
                if(commend.getText().equals("..")){
                    File file = new File(currentPath);
                    currentPath = file.getParent();
                    if(currentPath==null)
                        currentPath = file.getAbsolutePath();
                    currentInfo = Info(new File(currentPath));
                    info.setText(currentInfo);
                    path.setText(currentPath);    
                    commend.setText("");
                }
                if(commend.getText().substring(0, 3).equals("cd ")){
                    File file1 = new File(currentPath+commend.getText().substring(3));
                    File file2 = new File(commend.getText().substring(3));
                    if(!file1.exists() && !file2.exists()){
                        currentInfo = "Dir is Not exisits!!";
    
                    }
                    if(file1.exists()){
                        currentPath = file1.getAbsolutePath();
                        currentInfo = Info(new File(currentPath));
                    }
                    if(file2.exists()){
                        currentPath = file2.getAbsolutePath();
                        currentInfo = Info(new File(currentPath));
                    }
                    path.setText(currentPath);
                    info.setText(currentInfo);
                    commend.setText("");
                }
                if(commend.getText().substring(0, 6).equals("mkdir ")){
                    String D = commend.getText().substring(6);
                    File file = new File(currentPath+"\\"+D);
                    if(file.mkdir()){
                        currentInfo = "Create Dir Successfully!";
                    }
                    else{
                        currentInfo = "Create Dir Failure!";
                    }
                    info.setText(currentInfo);
                    commend.setText("");
                }
                if(commend.getText().substring(0, 4).equals("del ")){
                    String D = commend.getText().substring(4);
                    File file = new File(currentPath+"\\"+D);
                    if(file.delete()){
                        currentInfo = "Delete Dir or File Successfully!";
                    }
                    else{
                        currentInfo = "Delete Dir or File Failure!";
                    }
                    info.setText(currentInfo);
                    commend.setText("");
                }
                if(commend.getText().substring(0,5).equals("open ")){
                    String path1 = currentPath+commend.getText().substring(5);
                    String path2 = commend.getText().substring(5);
                    Runtime.getRuntime().exec("cmd /c "+path1);
                    Runtime.getRuntime().exec("cmd /c "+path2);
                    commend.setText("");
                    
                }
            }
        }catch (Exception e2) {
            
        }
        
    }
    /**
     * FuntionName: Info
     * FuntionDescription:caculate currentInfo
     * @param File file
     * @return String currentInfo
     */
    public String Info(File file){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        currentInfo = "Last Modified\t\t\tType\t\tSize(Bit)\t\tName\n";
        if(file.isFile()){
            currentInfo += sdf.format(new Date(file.lastModified()));
            currentInfo += "\t\t"
                            + "File\t\t" + file.length() + "\t\t"
                            + file.getName();
            currentInfo += "\n";
        }
        else{
            String fs[] = file.list();
            for(int i=0; i<fs.length; i++){
                File Temp = new File(file.getAbsoluteFile()+"\\"+fs[i]);
                String TT = new String("File");
                if(Temp.isDirectory())
                    TT = "Dir";
                currentInfo += sdf.format(new Date(Temp.lastModified()));
                currentInfo += "\t\t"
                                + TT+ "\t\t" + Temp.length() + "\t\t"
                                + Temp.getName();
                currentInfo += "\n";
            }
        }
        return currentInfo;
    }
}


/**
 * ClassName: HandleWin
 * ClassDescription:Handle Windows mesg;
 * @author VipXD
 * @version 1.0
 */

class HandleWin extends WindowAdapter{
    /**
     * FuntionName: windowClosing
     * FuntionDescription:close window
     * @param WindowEvent e
     * @return
     */
    public void windowClosing(WindowEvent e){
        e.getWindow().dispose();
        System.exit(0);
    }
}
原文地址:https://www.cnblogs.com/dongxiao/p/2540073.html