`
lxjchengcu
  • 浏览: 23496 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

命令行交互的一种Java实现

阅读更多

    现在服务器远程控制一般都是web方式实现的,这种方式体现的最明显的是J2EE应用服务器;当然为了操作方便,有些服务器也提供GUI客户端,
不过笔者比较喜欢字符终端,我想有很多人同笔者一样,在这里,向大家介绍一个Java实现的命令行程序,这是笔者当时阅读OBE(一个Workflow Engine)
的源代码时的收获,把代码调整了一下,版权就不属于Adrian.Price的了,哈哈!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class CommandClient {

    //返回菜单
    protected static final Object[] BACK = {"Q", "Back to previous menu", null};
    protected static final String UNDERLINE = "-----------------------------------------------------------------------------";
   
    //设置菜单
    private static final Object[][] SET_MENU = {
        // mnemonic, description, method
        {"1", "Load Default value", "loadDefault"},
        {"2", "Set Remote Host and Port(example,localhost:8191)", "setHost"},
        {"3", "Set Welcome message", "setMessage"},
        BACK
    };
  //主菜单 
 private static final Object[][] MAIN_MENU = {
        // mnemonic, description, method
        {"1", "Set", SET_MENU},
        {"2", "Check Server Status", "checkServerStatus"},
        {"3", "Shutdown Server", "shutdownServer"},
        {"Q", "Quit", "quit"}
    };
 
 protected BufferedReader _stdin;
    protected FormattingPrintWriter _stdout;
    protected String title = null;

   
    public void setHost()
     throws IOException
    {
     System.out.println("please enter connection string:");
     BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     System.out.println("The host you set is " + stdin.readLine());
    }
    public void shutdownServer()
    {
     System.out.println("server has been shut down");
    }
    public void checkServerStatus()
    {
     System.out.println("server is OK");
    }
    public CommandClient()
    {
     _stdin = new BufferedReader(new InputStreamReader(System.in));
        _stdout = new FormattingPrintWriter(System.out, true);
        title = "Command Client";
        _stdout.println("\n--- "+title+"  ---");
    }
    protected void quit() {
        System.exit(0);
    }
    private Method findMethod(String name) {
        Class cl = getClass();
        Method method = null;
        while (method == null) {
            try {
                method = cl.getDeclaredMethod(name, null);
            } catch (NoSuchMethodException e) {
                System.out.println("no method define");
                cl = cl.getSuperclass();
                if (cl == null) {
                    e.printStackTrace();
                    System.exit(1);
                }
             /*
                try
             {
              return cl.getDeclaredMethod("done", null);
             }
                catch (NoSuchMethodException ex) {ex.printStackTrace();}
                */
            }
        }
        return method;
    }
    protected String getChoice(String choices)
     throws IOException {
        while (true) {
            _stdout.print("> ");
            _stdout.flush();
            String line = _stdin.readLine().trim();
            if (line.length() == 1) {
                int choice = Character.toUpperCase(line.charAt(0));
                int index = choices.indexOf(choice);
                if (index != -1)
                    return choices.substring(index, index + 1);
            }
            _stdout.println("\007*** Choice must be one of: " + choices);
        }
    }
    //选择菜单选项,动态调用某个方法
    public void doMenu(Object[][] menu, boolean main)
    {
     this.doMenu(this.title,menu,main);
    }
    public void doMenu(String pTitle,Object[][] menu, boolean main)
    {
        synchronized (System.in) {
            Map actions = new HashMap();
            StringBuffer sb = new StringBuffer(menu.length);
            for (int i = 0; i < menu.length; i++) {
                Object mnemonic = menu[i][0];
                sb.append(mnemonic);
                Object action = menu[i][2];
                if (action instanceof String)
                    action = findMethod((String)action);
                actions.put(mnemonic, action);
            }
            String choices = sb.toString();

            while (true) {/////////////////////////////////////////////////////////////
                try {
                    String mnemonic;

                    _stdout.clearTabs();
                    _stdout.println("\n---   " + pTitle + "   ---");
                    _stdout.println("\nEnter choice:");
                    for (int i = 0; i < menu.length; i++)
                        _stdout.println(menu[i][0] + ") " + menu[i][1]);

                    // Get the user's selection.
                    mnemonic = getChoice(choices);
                    //System.out.println("mnemonic="+mnemonic);
                    for (int i = 0; i < menu.length; i++) {
                        Object[] entry = menu[i];
                        if (entry[0].equals(mnemonic)) {
                            Object action = actions.get(mnemonic);
                            if (action == null) {
                                return;
                            } else if (action instanceof Method) {
                                //System.out.println("selected,will do");
                                // Cast required to suppress JDK1.5 varargs compiler warning.
                                ((Method)action).invoke(this, (Object[])null);
                            } else {
                                doMenu((String)entry[1], (Object[][])action,
                                    false);
                            }
                        }
                    }
                } catch (Exception e) {
                    Throwable t = e;
                    if (e instanceof InvocationTargetException)
                        t = ((InvocationTargetException)e).getTargetException();
                    _stdout.println("\007*** Caught exception: " + t);
                }
            }////////////////////////////////////////////////////////////////////////////
        }
    }
 /**
  * @param args
  */
 public static void main(String[] args)
 {
  new CommandClient().doMenu(MAIN_MENU,true);
 }
 public void setMessage()
  throws IOException
 {
  System.out.println("please enter welcome message:");
     BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     System.out.println("welcome message  is " + stdin.readLine());
 }
 public void loadDefault()
 {
  System.out.println("default setting is loaded");
 }

}

附另外一个类(javaeye的blog有点令我失望,加个附件也很痛苦)

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Stack;

/**
 * A PrintWriter class that supports tab stops and programmable indentation.
 * The writer interprets tab characters in the usual way, by emitting spaces to
 * align the following characters at the next tab stop to the right of the
 * current print position. The default tab stops are at every 8 characters,
 * that is, in columns 9, 17, 25, ... 81. The {@link #setTabs} method allows
 * custom tab stops to be specified. Indentation can be controlled via the
 * {@link #pushIndent()}, {@link #pushIndent(int)} and {@link #popIndent()}
 * methods.
 * <p/>
 * The writer scans all arguments for newline characters, and achieves the
 * desired indentation level by inserting a corresponding number of spaces into
 * the output before continuing with the characters following the newline.
 * Indentation settings are held internally on a stack so that cancelling the
 * current setting automatically restores the previous one.
 *
 * @author Adrian Price
 */
public class FormattingPrintWriter extends PrintWriter {
    private static final String NEWLINE =
        System.getProperty("line.separator", "\r\n");
    // Standard tab settings
    private static final int[] STD_TABS =
        {9, 17, 25, 33, 41, 49, 57, 65, 73, 81};

    private boolean _autoFlush;
    private int[] _tabs = STD_TABS;
    private Stack _stack = new Stack();
    private int _indent;
    private int _pos;

    /**
     * Returns a string consisting of the specified number of spaces.
     *
     * @return The requested whitespace string.
     */
    private static String spaces(int n) {
        char[] ca = new char[n];
        for (int i = 0; i < n; i++)
            ca[i] = ' ';
        return new String(ca, 0, ca.length);
    }

    /**
     * Constructs a new FormattingPrintWriter, without automatic line flushing.
     *
     * @param out A character-output stream.
     */
    public FormattingPrintWriter(Writer out) {
        super(out);
    }

    /**
     * Constructs a new FormattingPrintWriter.
     *
     * @param out       A character-output stream.
     * @param autoFlush If <code>true</code>, the println() methods will flush
     *                  the output buffer.
     */
    public FormattingPrintWriter(Writer out, boolean autoFlush) {
        super(out, autoFlush);
        _autoFlush = autoFlush;
    }

    /**
     * Constructs a new PrintWriter, without automatic line flushing, from an
     * existing OutputStream. This convenience constructor creates the necessary
     * intermediate OutputStreamWriter, which will convert characters into bytes
     * using the default character encoding.
     *
     * @param out An output stream.
     */
    public FormattingPrintWriter(OutputStream out) {
        super(out);
    }

    /**
     * Constructs a new PrintWriter from an existing OutputStream. This
     * convenience constructor creates the necessary intermediate
     * OutputStreamWriter, which will convert characters into bytes using the
     * default character encoding.
     *
     * @param out       An output stream.
     * @param autoFlush if <code>true</code>, the println() methods will flush
     *                  the output buffer.
     */
    public FormattingPrintWriter(OutputStream out, boolean autoFlush) {
        super(out, autoFlush);
        _autoFlush = autoFlush;
    }

    /**
     * Restores the default tab stops.
     */
    public void clearTabs() {
        setTabs(null);
    }

    /**
     * Sets custom tab stops. At output positions past the rightmost tab stop,
     * tab characters are converted into single spaces.
     *
     * @param tabs Unity-based tab stop positions, as an ascending sequence of
     *             positive integers.
     */
    public void setTabs(int[] tabs) {
        synchronized (lock) {
            if (tabs == null) {
                _tabs = STD_TABS;
            } else {
                for (int i = 0, n = tabs.length - 1; i < n; i++) {
                    if (tabs[i] <= 0 || tabs[i] >= tabs[i + 1])
                        throw new IllegalArgumentException(
                            "Tab stops must be an ascending sequence of positive integers.");
                }
                _tabs = new int[tabs.length];
                System.arraycopy(tabs, 0, _tabs, 0, tabs.length);
            }
            if (_pos != 0)
                println();
        }
    }

    /**
     * Returns unity-based tab stop positions, as an ascending sequence of
     * positive integers.
     *
     * @return Current tab stops.
     */
    public int[] getTabs() {
        return (int[])_tabs.clone();
    }

    /**
     * Increases the indentation level by the specified amount.
     *
     * @param i Number of columns by which to increase indentation.
     */
    public void pushIndent(int i) {
        if (i <= 0) {
            throw new IllegalArgumentException(
                "Indentation must be a positive integer");
        }
        synchronized (lock) {
            _stack.push(new Integer(i));
            _indent += i;
        }
    }

    /**
     * Increases the indentation level to the next tab stop past the current
     * output position.
     */
    public void pushIndent() {
        // Indent to the nearest tab stop to the right of the current
        // indentation level, if such a tab stop exists.
        for (int i = 0, n = _tabs.length; i < n; i++)
            if (_tabs[i] > _indent) {
                pushIndent(_tabs[i] - _indent);
                return;
            }
        // Past the rightmost tab stop, indentation is one space.
        pushIndent(1);
    }

    /**
     * Restores the previous indentation level.
     *
     * @throws IllegalStateException if the current indentation level is 0.
     */
    public void popIndent() {
        if (_stack.empty())
            throw new IllegalStateException();
        _indent -= ((Integer)_stack.pop()).intValue();
    }

    /**
     * Returns the current indentation level.
     *
     * @return Indentation level as a character count.
     */
    public int getIndent() {
        return _indent;
    }

    /**
     * Returns the current output position (zero-based).
     *
     * @return The output position.
     */
    public int getPosition() {
        return _pos;
    }

    /**
     * Expands a tab character by setting the output position to the next tab
     * stop past the current output position.
     *
     * @return Space-filled string.
     */
    private String expandTab() {
        // If pos is after the last tab stop, translate tab characters to spaces.
        String s = " ";
        int curpos = _indent + _pos;
        for (int i = 0; i < _tabs.length; i++) {
            // Tab stops use 1-based column numbers,
            if (_tabs[i] - 1 > curpos) {
                // curpos is a 0-based column index.
                s = spaces(_tabs[i] - curpos - 1);
                break;
            }
        }
        _pos += s.length();
        return s;
    }

    /**
     * Expands embedded tab and newline escape sequences, adjusting the output
     * position accordingly. The method recognizes 'C'/Java-style '\t', '\r' and
     * '\n' escape sequences.
     *
     * @param ch Character to expand.
     * @return String containing (expanded) input character.
     */
    private String expandEscapes(char ch) {
        StringBuffer result = new StringBuffer();
        switch (ch) {
            case '\t':
                if (_pos == 0 && _indent > 0)
                    result.append(spaces(_indent));
                result.append(expandTab());
                break;
            case '\n':
                _pos = 0;
            case '\r':
                result.append(ch);
                break;
            default:
                if (_pos == 0 && _indent > 0)
                    result.append(spaces(_indent));
                result.append(ch);
                _pos++;
        }
        return result.toString();
    }

    /**
     * Expands embedded tab and newline escape sequences, adjusting the output
     * position accordingly. The method recognizes 'C'/Java-style '\t', '\r' and
     * '\n' escape sequences.
     *
     * @param s   Source string.
     * @param off Offset at which to start copying.
     * @param len Number of source characters to process.
     * @return Copy of the source string where all escape sequences have been
     *         replaced by their equivalent characters.
     */
    private String expandEscapes(String s, int off, int len) {
        StringBuffer result = new StringBuffer(len);
        for (int i = off, end = off + len; i < end; i++) {
            char ch = s.charAt(i);
            switch (ch) {
                case '\t':
                    if (_pos == 0 && _indent > 0)
                        result.append(spaces(_indent));
                    result.append(expandTab());
                    break;
                case '\n':
                    _pos = 0;
                case '\r':
                    result.append(ch);
                    break;
                default:
                    if (_pos == 0 && _indent > 0)
                        result.append(spaces(_indent));
                    result.append(ch);
                    _pos++;
            }
        }
        return result.toString();
    }

    /**
     * Writes a character, which may be a tab or newline.
     *
     * @param c Character to write.
     */
    private void _writeEx(int c) {
        String s = expandEscapes((char)c);
        super.write(s, 0, s.length());
    }

    /**
     * Writes a string which may contain tab or newline characters.
     *
     * @param s   Source string.
     * @param off Offset at which to start writing.
     * @param len Number of source characters to process.
     */
    private void _writeEx(String s, int off, int len) {
        s = expandEscapes(s, off, len);
        super.write(s, 0, s.length());
    }

    /**
     * Writes a string that does not contain embedded tabs or newlines.
     *
     * @param s   Source string.
     * @param off Offset at which to start writing.
     * @param len Number of source characters to process.
     */
    private void _write(String s, int off, int len) {
        _pos += len;
        super.write(s, off, len);
    }

    public void print(boolean b) {
        String s = String.valueOf(b);
        _write(s, 0, s.length());
    }

    public void print(char c) {
        _writeEx(c);
    }

    public void print(int i) {
        String s = String.valueOf(i);
        _write(s, 0, s.length());
    }

    public void print(long l) {
        String s = String.valueOf(l);
        _write(s, 0, s.length());
    }

    public void print(float f) {
        String s = String.valueOf(f);
        _write(s, 0, s.length());
    }

    public void print(double d) {
        String s = String.valueOf(d);
        _write(s, 0, s.length());
    }

    public void print(char[] ca) {
        _writeEx(new String(ca), 0, ca.length);
    }

    public void print(String s) {
        _writeEx(s, 0, s.length());
    }

    public void print(Object obj) {
        String s = String.valueOf(obj);
        _writeEx(s, 0, s.length());
    }

    private void newLine() {
        _write(NEWLINE, 0, NEWLINE.length());
        _pos = 0;
        if (_autoFlush)
            flush();
    }

    public void println() {
        synchronized (lock) {
            newLine();
        }
    }

    public void println(boolean b) {
        synchronized (lock) {
            print(b);
            newLine();
        }
    }

    public void println(char c) {
        synchronized (lock) {
            print(c);
            newLine();
        }
    }

    public void println(int i) {
        synchronized (lock) {
            print(i);
            newLine();
        }
    }

    public void println(long l) {
        synchronized (lock) {
            print(l);
            newLine();
        }
    }

    public void println(float f) {
        synchronized (lock) {
            print(f);
            newLine();
        }
    }

    public void println(double d) {
        synchronized (lock) {
            print(d);
            newLine();
        }
    }

    public void println(char[] c) {
        synchronized (lock) {
            print(c);
            newLine();
        }
    }

    public void println(String s) {
        synchronized (lock) {
            print(s);
            newLine();
        }
    }

    public void println(Object obj) {
        synchronized (lock) {
            print(obj);
            newLine();
        }
    }

    public void write(int c) {
        _writeEx(c);
    }

    public void write(char[] buf, int off, int len) {
        _writeEx(new String(buf, off, len), 0, len);
    }

    public void write(char[] buf) {
        _writeEx(new String(buf), 0, buf.length);
    }

    public void write(String s, int off, int len) {
        _writeEx(s, off, len);
    }

    public void write(String s) {
        _writeEx(s, 0, s.length());
    }
    public static void main(String[] args)
     throws Exception
    {
     System.out.println("please enter file path");
     BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     String line = null;
     while ((line = stdin.readLine()) != null)
     {
      File dir = new File(line.trim());
      if (dir.exists())
      {
       System.out.println(dir.getAbsolutePath());
       break;
      }
      else
      {
       System.out.println("dir does not exist,please re-enter:");
      }
     }
    }
}


分享到:
评论

相关推荐

    Java的命令行进度条 JCmdLineProgess

    Java的命令行进度条JCmdLineProgress是一种在控制台或者...总之,JCmdLineProgress为Java开发者提供了一种简单而有效的方式来在命令行界面中实现进度条,提高了用户对长时间运行任务的感知,提升了命令行应用的交互性。

    Java调用命令行!

    总的来说,Java调用命令行是通过`Runtime`和`ProcessBuilder`类实现的,它们提供了一种与操作系统交互的方式。理解并熟练掌握这些知识点对于开发需要系统级操作的Java应用至关重要。在实际项目中,务必注意处理好...

    命令行中的java五子棋小游戏

    Java是一种面向对象的编程语言,由Sun Microsystems(现为Oracle公司)于1995年推出。它的设计目标是“一次编写,到处运行”,这意味着Java程序可以在任何支持Java虚拟机(JVM)的平台上运行。对于本游戏而言,我们...

    Java cmd命令行模拟工具

    Java cmd命令行模拟工具是一种用于在Java环境中模拟操作系统命令行工具的实用程序。它使得开发者能够在Java代码中执行系统命令,获取命令输出,并对命令行交互进行控制,这对于自动化测试、脚本编写以及需要与系统...

    java 聊天工具 命令行版

    总结来说,这个“Java聊天工具命令行版”项目是一个学习网络编程、多线程以及命令行交互的好案例。通过分析和重构这样的代码,开发者可以深化对Java核心特性的理解,同时也能掌握如何利用这些知识来构建实际的软件...

    Java简单实现调用命令行并获取执行结果示例

    Java是一种广泛应用于软件开发的高级编程语言,随着计算机网络技术的发展,Java语言在网络编程、Socket编程、进程与线程操作、数据结构与算法、DOM节点操作、缓存操作等领域发挥着重要作用。其中,命令行调用及执行...

    聊天系统 java编写的基于applet的聊天系统

    Java Applet通过Java Virtual Machine (JVM) 运行,可以跨平台运行,为用户提供了一种在浏览器上实现复杂功能的方式。 【聊天系统设计】 聊天系统的实现通常包括以下核心组件: 1. **用户界面**:这是用户与聊天...

    基于Java语言实现的命令行工具-everything.zip

    Java作为一种跨平台的编程语言,以其“一次编写,到处运行”的特性广受欢迎。在创建命令行工具时,Java提供了丰富的类库,如`java.util.Scanner`用于用户输入处理,`java.io`包下的类用于文件操作,`java.lang....

    基于命令行的绘图程序的实现-Java-version1.0

    在本项目"基于命令行的绘图程序的实现-Java-version1.0"中,我们看到的是一个使用Java编程语言构建的图形用户界面(GUI)应用程序的早期版本,该程序利用命令行进行交互。这个程序的核心是实现了MVC(Model-View-...

    Java命令行classpath的设置

    首先,我们来看一下JavaDBTest.java这个文件,它可能是一个包含了测试代码的Java源文件,可能用于测试Java数据库连接或者其他与数据库交互的功能。在没有具体代码的情况下,我们假设它是用来演示如何通过Java命令行...

    java命令行大富翁

    这款游戏虽然没有华丽的图形界面,但通过简单的字符输出和用户输入,模拟了经典的大富翁游戏规则,为玩家提供了一种寓教于乐的学习体验。 在Java中,命令行游戏的开发主要依赖于`System.out.println()`方法来显示...

    Java 命令行应用开发

    【Java 命令行应用开发】是一种基础且重要的编程实践,它涉及到使用Java语言编写程序,通过命令行界面(CLI)运行和交互。在Java世界中,命令行应用通常是开发的第一个步骤,也是理解编程概念和Java核心特性的基石。...

    JAVA数据输入方式(命令行输入和控制台输入)

    命令行输入是一种基本的数据输入方式,在Java中可以通过主函数中的`args`数组来接收。这种方式适用于简单的数据输入场景,但其灵活性较差,不适合复杂的交互式操作。 ##### 实现方式: 在Java程序中,主函数通常...

    使用Java编写基于命令行的程序

    在Java编程中,创建基于命令行的程序是一种基础但实用的技术。这主要涉及到对系统输入输出的处理,以及可能的参数解析。以下是一些关于如何使用Java编写命令行程序的关键知识点: 1. **主方法(Main Method)**: ...

    java实现的在dos下的MIS系统

    Java 实现的在 DOS 下的 MIS(Management Information System)系统是一种命令行界面的应用程序,它利用 Java 语言的强大功能在 DOS 操作系统环境下运行。这种系统通常用于数据管理和信息处理,例如图书管理系统,...

    java编写的命令行文件/文件夹管理程序

    这个Java编写的命令行文件管理程序综合运用了Java的多种特性,提供了一种高效、便捷的文件管理方式,尤其适合开发者和系统管理员。通过学习和理解这些知识点,不仅可以更好地理解和使用这个程序,也能提升Java编程和...

    java画图程序,java编程

    Java画图程序是一种基于Java语言实现的图形用户界面(GUI)应用,它允许开发者通过编程方式在屏幕上绘制各种图形,如线条、圆形、矩形、曲线等。在Java中,我们主要利用Java AWT(Abstract Window Toolkit)和Swing...

    java实现rtsp/rtmp转m3u8/flv/hls/mp4等实现web直播和回放

    RTSP是一种控制协议,允许客户端对流媒体服务器进行播放、暂停、快进等操作。RTMP则由Adobe开发,主要用于Flash应用中的实时数据传输,包括音频、视频和控制信息。 2. **M3U8**:M3U8是HLS(HTTP Live Streaming)...

    Java实现模拟银行储蓄系统

    1. **Java编程**:Java是一种广泛使用的面向对象的编程语言,具有跨平台的特性,适用于开发各种类型的应用程序,包括桌面应用、Web应用和移动应用。在这个银行储蓄系统中,Java被用来实现业务逻辑、用户界面以及...

    java实现wav文件转成mp3文件

    另一种可能的方法是使用JLayer,这是一个Java实现的MP3解码库。虽然JLayer主要用于解码MP3,但可以结合其他工具(如ffmpeg)实现编码功能。 在压缩包文件列表中,我们看到`wav2mp3.jar`可能是一个已经封装好的转换...

Global site tag (gtag.js) - Google Analytics