简体   繁体   English

从命令行获取参数

[英]Getting arguments from the command line

I have wrote the following for scanning few lines of code as a string and parsing it to an integer. 我编写了以下内容,用于扫描几行代码作为字符串并将其解析为整数。 But within my main method, I am using p.letsReadIn to read in the file. 但在我的main方法中,我使用p.letsReadIn来读入文件。 Does anyone know how I can convert this to use command line argument instead for taking in the file? 有谁知道如何将此转换为使用命令行参数而不是接收文件? So instead of having the user change p.letsReadIn("ReadMuah.txt"); 所以不要让用户改变p.letsReadIn(“ReadMuah.txt”); every time, they can just use command line just for reading in the file. 每次,他们只需使用命令行来读取文件。

public void letsReadIn(String filename) throws FileNotFoundException {
        int x;
        Scanner scan = new Scanner(new File(filename));

        StringTokenizer st;
        String nextLine = scan.nextLine();

        st = new StringTokenizer(nextLine);

        x = Integer.parseInt(st.nextToken());

        nextLine = scan.nextLine();

        st = new StringTokenizer(nextLine);

        y= Integer.parseInt(st.nextToken());

        nextLine = scan.nextLine();

        st = new StringTokenizer(nextLine);

        z= Integer.parseInt(st.nextToken());

        for (int i = 0; i < x; i++) {
            nextLine = scan.nextLine();
            st = new StringTokenizer(nextLine);
            listing.add(Integer.parseInt(st.nextToken()));
        }

        scan.close();
    }
    public static void main(String[] args) throws FileNotFoundException {

        BagItems p = new BagItems();
        p.letsReadIn("ReadMuah.txt");
        p.StartBagging();

    }

The String[] args parameter for the main() method are the command line arguments. main()方法的String[] args参数是命令行参数。 If you put the filename on the command line, it'll be in that array. 如果您将文件名放在命令行上,它将在该数组中。

It depends what you mean by "use command line argument instead for taking in the file" 这取决于你的意思是“使用命令行参数代替接收文件”

If you mean taking the arguments themselves from the command line (literally) then replace the Scanner and Scanner.parseInt calls with stuff that iterates over the String[] , and parses using Integer.parseInt(String) calls. 如果你的意思是从命令行中获取参数(字面意思),那么用迭代在String[]上的东西替换ScannerScanner.parseInt调用,并使用Integer.parseInt(String)调用进行解析。 But that's going to give you a non-user-friendly command line syntax, so you may want to add some "options" or whatever 但这会给你一个非用户友好的命令行语法,所以你可能想要添加一些“选项”或其他

If you mean that you want to be able to specify the source of the input parameters from the command line, then try something like this: 如果您希望能够从命令行指定输入参数的来源,请尝试以下操作:

InputStream in;
if (args.length > 0) {
    in = new FileInoputStream(args[0]);
} else {
    in = System.in;
} = System.in
....
Scanner scan = new Scanner(input);
// Read as before.

This (or something like it) will allow your application to read the input either from a named file, or from standard input. 这个(或类似的东西)将允许您的应用程序从命名文件或标准输入读取输入。 In the latter case, you could run it like this (on Linux) 在后一种情况下,您可以像这样运行它(在Linux上)

$ java ... my.Program << EOF
1
2
...
EOF

You could use p.parser(args[0]); 你可以使用p.parser(args[0]); , of course you need to check if args is containing some strings. 当然你需要检查args是否包含一些字符串。 ie you have passed an argument, just do if (args.length > 0) for that. 即你已经传递了一个参数,就这样做if (args.length > 0)

Use first element from args[] instead of hard coded file name in code. 在代码中使用args []中的第一个元素而不是硬编码文件名。

public static void main(String[] args) throws FileNotFoundException {
    BagItems p = new BagItems();
    if (args.length > 0)
    {
        p.parser(args[0]);
        p.StartBagging();
    }
    else
    {
        p.parser("hard-coded-file.txt");
        p.StartBagging();
    }
}

Now you can pass file name to program as command line and your code should pick that. 现在您可以将文件名作为命令行传递给程序,您的代码应该选择它。

Please note that array index start from 0 by default in java, so, to access first element of array (command-line arguments array) you should use [0]. 请注意,java中的数组索引默认从0开始,因此,要访问数组的第一个元素(命令行参数数组),您应该使用[0]。

For some more tips please follow the link below. 如需更多提示,请点击以下链接。

how to accept values using command line argument in java 如何使用java中的命令行参数接受值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM