我不确定您应该如何从 Java 文件的系统输入中读取数据。
我希望能够调用java myProg < file
file 是我想要作为字符串读取并在 main 方法中提供给 myProg 的内容。
有什么建议吗?
最佳答案
您可以使用 System.in
从标准输入中读取。它的工作原理就像从键盘输入一样。操作系统处理从文件到标准输入。
import java.util.Scanner;
class MyProg {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Printing the file passed in:");
while(sc.hasNextLine()) System.out.println(sc.nextLine());
}
}
关于java - 从 System.in 读入 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5488072/