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

查看:124
本文介绍了从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

其中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.

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天全站免登陆