Java读取命令行参数并允许管道

时间:2016-10-25 14:52:43

标签: java parameter-passing pipeline

1。我想知道如何在控制台中执行我的程序中执行的参数,并对它们采取一些操作。例如,我执行命令myProg -i someFile -o someOutput -v,其中:

  • -i []是我读过的输入文件
  • -o []是我用
  • 写的文件位置
  • -v设置用于调试目的的详细模式

2. 我还想做的是在我的程序中允许管道。例如,当我致电someProg | myProg > result.whatever时,它将获取someProg的结果,处理它,并将其写入result.whatever

1 个答案:

答案 0 :(得分:0)

@ 1:这很简单,以下是一个片段,向您展示如何执行此操作

public static void main(String[] args) {
    // variables we will need
    String fileIn = "";
    String fileOut = "";
    int i = 0;

    for(String s : args) {
        if (s.equals("-v")) { // we check for if it is equal to -v
            // depends on your logger
            // for example for simpleLogger and slf4j
            System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug");
        } else if(s.equals("-i") && !args[i+1].equals("-o") && args[i+1] != null) { // in an else if to have no input = -v
            fileIn = args[i+1];
        } else if(s.equals("-o") && args[i+1] != null) { // in an else if to have no input = -v or -i
            fileOut = args[i+1];
        } else {
            System.err.println(s + "is an invalid parameter");
            System.exit(-1);
        }
    }

    // then you can do whatever you want with your input and output

}

@ 2:您可以这样做,但如果用户只是在没有参数或管道myprog的情况下调用您的程序,则可能会遇到一些问题,这就是您必须执行超时的原因。

private Document doc;
public void loadFile(String fileLocation) { 
    final Thread parent = Thread.currentThread(); // our current thread
    Thread timeout = new Thread() { // to have a timeout
        public void run() {
            try {
            sleep(5000); //waits for 5 seconds
            parent.interrupt();

            //say user that no input has been given or found
            System.exit(-1); // exit the program
            } catch (InterruptedException e) {
                //Timeout was interrupted
            }
        }
    };
    timeout.start(); // starts the timeout thingie that will wait for 5 seconds and shut down the main thread.
    if(fileLocation.equals("")) { // in case no input, so System.in
        File f = null;
        try {
            f = java.io.File.createTempFile("buf", ".whatever", null);
        } catch (IOException e) {
            e2.printStackTrace();
            System.exit(-1);
        }

        PrintWriter writer = null;
        try {
            writer = new PrintWriter(f);
        } catch (FileNotFoundException e) {
            e1.printStackTrace();
            System.exit(-1);
        }

        Scanner sc = new Scanner(System.in);

        while (sc.hasNextLine()) { // we have to do the thread thing because of this line
// hasNextLine() always return true for System.in since it could have a next line
            String b = sc.nextLine();
            writer.println(b);
        }

        timeout.interrupt(); // since it worked fine we shut timeout down
        sc.close();
        writer.close();

        // now you can have your document in f and handle it
        // do things with f

        // don't forget
        f.delete();
    } else {
        // load the file from the path
    }
}