急求两个有些难度的java编程题!!!

1 编写一个CUI(dos字符界面)的Application。要求如下:
(1)运行程序后,仿照dos窗口模式,显示一个提示符(如:张三:\>),等待用户输入;
(2)如果用户输入常规dos命令,则显示执行结果。
常规命令包括:显示当前目录文件(dir 或者 dir /s)、切换目录(cd 目录)、删除文件(del 文件名)、查看文本文件(type 文件名)
(3)程序接受用户的输入后,要分辨出输入了何种命令,然后利用Java中的功能,实现与dos系统中相同的执行结果。
(4)如果输入了“exit”命令,则程序退出。否则,就等待用户输入并执行。

2 编写GUI的一个文件改名程序。要求:
(1)用户选择改名,弹出文件选择对话框,得到用户选择的文件名;
(2)在要求用户选择是单个文件还是批量改名;
(3)如果是单个文件,则将获得的文件直接改名;
(4)如果选择了批量,则只循环当前目录下的文件(不包括子目录),文件名的固定部分允许用户输入。
如:假如当前目录下都是jpg文件,固定部分为“a”,则将所有文件都改成a1.jpg,a2.jpg,a3.jpg,....,a10.jpg,a11.jpg....

 
 
 
第一题若利用映射的话,程序会蛮简洁而且方便扩展。
接近题目要求的程序如下:

import java.lang.reflect.*;
import java.io.*;
import java.util.*;

class MiniDOS {
    public static void main(String[] args) {
        new MiniDOS().run();
    }

    public void run() {
        Scanner scn = new Scanner(System.in);
        while (true) {
            String prompt = "[ " + cwd + " ] >> ";
            System.out.print(prompt);
            String line = scn.nextLine().trim();
            if (line.length() > 0 && ! line.equals(prompt.trim()))
                processInput(line);
        }
    }

    private void processInput(String input) {
        String[] tokens = input.split("\\s+", 2);
        String cmd = tokens[0].toLowerCase();
        try {
            if (! isSupportedCommand(cmd))
                throw new Exception("'" + cmd + "' is an invalid command");

            Method method = SupportedCommands.class.getMethod(
                    cmd, tokens.length == 2 ? new Class[]{File.class} : null);

            Object[] argumentsForMethodInvocation = null;
            if (tokens.length == 2) {
                File f = new File(tokens[1]);
                f = f.isAbsolute() ? f : new File(cwd, f.getPath());
                if (! f.exists())
                    throw new IOException("'" + f + "' doesn't exist");
                argumentsForMethodInvocation = new Object[]{f.getCanonicalFile()};
            }

            method.invoke(new SupportedCommands(), argumentsForMethodInvocation);
        }
        catch (NoSuchMethodException e) {
            printError("wrong number of argument for '" + cmd + "'");
        }
        catch (Exception e) {
            Throwable cause = e.getCause(); // 为了应付 InvocationTargetException
            printError(cause != null ? cause.getMessage() : e.getMessage());
        }
    }

    private void printError(String message) {
        System.err.println( "Error: " + message);
    }

    private File cwd = new File(System.getProperty("user.dir"));    // 工作目录

    private boolean isSupportedCommand(String cmd) {
        return Arrays.toString(SupportedCommands.class.getMethods())
                .contains("SupportedCommands." + cmd + "(");
    }

    /*
     * SupportedCommands 类里定义的所有方法都是程序支持的常规命令。
     */
    private class SupportedCommands {
        public void dir() {
            for (File f: cwd.listFiles())
                System.out.printf( "%s %9s %9s %s\n",
                    new Date(f.lastModified()),
                    f.isDirectory() ? "<DIR>" : "",
                    f.isDirectory() ? "" : f.length(),
                    f.getName()
                );
        }

        public void cd(File file) throws IOException {
            if (! file.isDirectory())
                throw new IOException("'" + file + "' isn't a directory");
            cwd = file;
        }

        public void del(File file) throws IOException {
            if (! file.delete())
                throw new IOException("deletion failed");
        }

        public void type(File file) throws IOException {
            for (Scanner fs = new Scanner(file); fs.hasNextLine(); )
                System.out.println(fs.nextLine());
        }

        public void exit() {
            System.exit(0);
        }
    }
}
 
 
 
温馨提示:答案为网友推荐,仅供参考
第1个回答  2006-12-27
哪怕你能给一个错误百出的基本框架,也会有人愿意帮你。但是这样的提问令人厌恶,鄙视
相似回答
大家正在搜