English 中文(简体)
是否有办法有时要求从 st中而不是在 Java的数据?
原标题:Is there a way to sometimes require data from stdin and sometimes not in Java?

我将下面的法典改为从指挥线的线上排出的缩略语:

    BufferedReader in = null;
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
        in = new BufferedReader(new InputStreamReader(System.in));
        while((line = in.readLine()) != null) {
            sb.append(line);
        }
    } finally {
        if (in!=null) in.close();
    }

我的问题是,应用有时需要从零线上操作数据,有时不需要(没有管道投入)。 如果上述代码没有输入,in.readLine()各栏。 是否有办法改写这部法典,这样它才能继续运作,如果没有任何东西。

最新资料:申请的目的是要从指挥线而不是从钥匙板获得管道数据。

最佳回答

我不认为,如果你最终能够再接一线投入,那将有任何办法加以检查。 请注意,如果用户关闭输入流(例如,在终点站有^D),现行代码即告终止。

<代码>BufferedReader.ready(>查询,如果有关于流的数据。 和T.J.提到的情况一样,你在实际收到数据之前可能不会仓促要求数据,而你的用户会感到悲伤,因为你没有回答他们的询问。

Scanner.hasNextLine() is a blocking operation, so probably not what you re looking for.

用户可以说明是否从系统中读到,例如使用指挥线论据。

问题回答

支持从中选读的单一公用事业通常根据其指挥线来显示什么。 例如,如果在指挥线上没有档案,<代码>cat将读到第6栏。 你可以按照这些路线做一些事情,即悬挂指挥线的旗帜或选择,或表明方案是否应当尝试从小方向读任何东西。

另一种做法是,如果你不希望方案读到,则将方案/方案的投入转作。 在这种情况下,你可以直线阅读,并立即发出最后记录。 窗口等值从NUL转出。

如果你真的需要你的方案来检测它是否能够在不设障碍的情况下从零读到,请看InputStream. Available(。 但是,如果你想要支持方案打字的人,那么投入(或将其复制成一个终端窗口,说)到用户实际类型之前,InputStream获得了一定投入。

凭单,可检查<代码>/proc/ Self/fd/0在档案、管道或ole中的位置。

$ ls -l /proc/self/fd/0
lrwx------ 1 peter peter 64 Apr 25 18:12 /proc/self/fd/0 -> /dev/pts/21
$ ls -l /proc/self/fd/0 < /dev/null
lr-x------ 1 peter peter 64 Apr 25 18:13 /proc/self/fd/0 -> /dev/null
$ echo Hello World | ls -l /proc/self/fd/0 
lr-x------ 1 peter peter 64 Apr 25 18:13 /proc/self/fd/0 -> pipe:[139250355]

如果投入是档案、管道或终点站,这将告诉你。

It depends on how your program is run when there is no piped input. Generally a program will have an open stdin and all you can do is set a timer, wait a certain amount of time for input, and if the timer expires without input, assume there isn t going to be any. The alternative would be to arrange for the program to be run with stdin closed, in which case you ll get an EOF when you try to read.

• 在无主人(包括麦克索)的旁遮普时,请填写一份标本,以启动你的节目:

#!/bin/bash

exec 0>&- # close stdin

java -jar yourProgram.jar # run your program

Of course, if you re launching from the bash command line anyway, you don t need a script:

prompt> java -jar yourProgram.jar 0>&-

但是,如果你的方案在 Java EE集装箱内运行,我不知道你在发射前如何接近尾声,也许你可以 t。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签