Java 文件输入作为命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/196824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:11:02  来源:igfitidea点击:

Java file input as command line argument

javacommand-line

提问by

How do you process information in Java that was input from a file. For Example: suppose you have a file input.txt. The contents of this file is: abcdefghizzzzjklmnop azzbcdefghijklmnop

您如何处理从文件输入的 Java 信息​​。例如:假设您有一个文件 input.txt。这个文件的内容是:abcdefghizzzzjklmnop azzbcdefghijklmnop

My hope would be that the information would be put into the argument array of strings such that the following code would output "abcdefghizzzzjklmnop"

我希望将信息放入字符串的参数数组中,以便以下代码输出“abcdefghizzzzjklmnop”

class Test {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

The command I have been using throws an array out of bound exception. This command is:

我一直在使用的命令抛出一个数组越界异常。这个命令是:

java Test < input.txt

java测试<input.txt

Non-file based arguments work fine though. ie. java Test hello,a nd java Test < input.txt hello.

非基于文件的参数虽然工作正常。IE。java 测试你好,和 java 测试 < input.txt 你好。

More information: I have tried putting the file contents all on one line to see if \n \r characters may be messing things up. That didn't seem to help.

更多信息:我尝试将文件内容全部放在一行上,以查看 \n \r 字符是否可能会搞砸。那似乎没有帮助。

Also, I can't use the bufferedreader class for this because this is for a program for school, and it has to work with my professors shell script. He went over this during class, but I didn't write it down (or I can't find it).

另外,我不能为此使用 bufferedreader 类,因为这是用于学校的程序,并且它必须与我的教授的 shell 脚本一起使用。他在课堂上翻了一遍,但我没有写下来(或者我找不到)。

Any help?

有什么帮助吗?

回答by John Millikin

You should be able to read the input data from System.in.

您应该能够从 读取输入数据System.in

Here's some quick-and-dirty example code. javac Test.java; java Test < Test.java:

这是一些快速而肮脏的示例代码。javac Test.java; java Test < Test.java

class Test
{
    public static void main (String[] args)
    {
        byte[] bytes = new byte[1024];
        try
        {
            while (System.in.available() > 0)
            {
                int read = System.in.read (bytes, 0, 1024);
                System.out.write (bytes, 0, read);
            }
        } catch (Exception e)
        {
            e.printStackTrace ();
        }
    }
}

回答by John Millikin

It seems any time you post a formal question about your problem, you figure it out.

似乎每当您发布有关您的问题的正式问题时,您都会弄清楚。

Inputing a file via "< input.txt" inputs it as user input rather than as a command line argument. I realized this shortly after I explained why the bufferedreader class wouldn't work.

通过“< input.txt”输入文件作为用户输入而不是作为命令行参数输入。在我解释了为什么 bufferedreader 类不起作用后不久,我就意识到了这一点。

Turns out you have to use the buffered reader class.

原来你必须使用缓冲阅读器类。

回答by Uri

I'm not sure why you want to pass the contents of a file as command line arguments unless you're doing some weird testbed.

我不确定为什么要将文件的内容作为命令行参数传递,除非您正在做一些奇怪的测试台。

You could write a script that would read your file, generate a temporary script in which the java command is followed by your needs.

您可以编写一个脚本来读取您的文件,生成一个临时脚本,其中 java 命令后跟您的需求。