如何使用 Java 中的命令行将文本文件读入扫描仪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28468151/
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-11-02 13:38:57  来源:igfitidea点击:

How to read a text file in to a scanner using command line in Java

javacommand-linecommand-line-arguments

提问by user1010101

I have a small snippet of code that will ask user to type input such as

我有一小段代码会要求用户输入输入,例如

5
12
59
58
28
58

The first number will indicate the size of the array i need to create and the rest of the numbers will be stored in that array. So with the given input an array of size 5 would be created and the numbers following would be stored in the array.

第一个数字将指示我需要创建的数组的大小,其余数字将存储在该数组中。因此,对于给定的输入,将创建一个大小为 5 的数组,随后的数字将存储在该数组中。

my code

我的代码

public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         int size = sc.nextInt();
         int[] array = new int[size];

        for(int i =0; i<size; i++)
        {
            array[i] = sc.nextInt();
        }                       
}

I was wondering is there a way of just feeding a text file instead of typing numbers manually. I mean know there are ways to read text files but is there a way to just feed it in command line. I Know in c there is some simple command where you can just type something like that and it works-> ./code.out > input.txt

我想知道有没有办法只输入文本文件而不是手动输入数字。我的意思是知道有多种方法可以读取文本文件,但是有没有一种方法可以在命令行中输入它。我知道在 c 中有一些简单的命令,您可以在其中键入类似的内容并且它可以工作->./code.out > input.txt

回答by Giorgian Borca-Tasciuc

Assuming that you're using a bash or POSIX command line interface, it's really quite simple.

假设您使用的是 bash 或 POSIX 命令行界面,这真的很简单。

1) Create a file named "input.txt" where you'll keep your input (in this case, the numbers).

1) 创建一个名为“input.txt”的文件,您将在其中保存您的输入(在本例中为数字)。

2) Save your input into the file (ie, by copying and pasting the numbers into the file).

2) 将您的输入保存到文件中(即,通过将数字复制并粘贴到文件中)。

3) Assuming that you compiled your java file with javac, run the program like this:

3)假设你用javac编译了你的java文件,像这样运行程序:

java yourprogram < input.txt

4) And perhaps if you want to save your output to a file:

4)也许如果您想将输出保存到文件中:

java yourprogram < input.txt > output.txt

To see more on bash redirection, read the web page corresponding to this link: https://www.gnu.org/software/bash/manual/html_node/Redirections.html.

要查看有关bash 重定向的更多信息,请阅读与此链接对应的网页:https: //www.gnu.org/software/bash/manual/html_node/Redirections.html

回答by Matt Clark

You can just use the Scanner!

你可以只使用Scanner!

Scanner scn = new Scanner(new File("myFile.txt"));

Using the command line, you will use the parameters given in your psv main.

使用命令行,您将使用psv main.

public static void main(String args[]) {

    Scanner scn = new Scanner(new File(args[0]));
}

You can then run your .jar file from the command line, and pass it a path.

然后,您可以从命令行运行 .jar 文件,并向其传递路径。

java -jar myJar.jar "/home/matt/myFile.txt"

java -jar myJar.jar "/home/matt/myFile.txt"

In Eclipse, you can go into your launch configuration edtor, and manualy specify arguments to be added on launch, from within Eclipse.

在 Eclipse 中,您可以进入启动配置编辑器,并在 Eclipse 中手动指定要在启动时添加的参数。