从 System.in 读入 - Java [英] Reading in from System.in - Java

查看:31
本文介绍了从 System.in 读入 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定您应该如何从 Java 文件的系统输入中读取数据.

I am not sure how you are supposed to read in from system input from a Java file.

我希望能够调用 java myProg <文件

file 是我想作为字符串读取并在 main 方法中提供给 myProg 的地方.

Where file is what I want to be read in as a string and given to myProg in the main method.

有什么建议吗?

推荐答案

您可以使用 System.in 从标准输入中读取.它的工作原理就像从键盘输入一样.操作系统处理从文件到标准输入的过程.

You can use System.in to read from the standard input. It works just like entering it from a keyboard. The OS handles going from file to standard input.

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());
    }
}

这篇关于从 System.in 读入 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆