English 中文(简体)
阅读系统。 in多处造成IOException?
原标题:Reading System.in multiple times in Java causes IOException?
  • 时间:2011-09-08 12:56:14
  •  标签:
  • java

我正在玩.,试图打下几条线游戏,以强化我过去几个月在 Java学到的一些东西。

我试图建立一种称为“Input(Input)(Input)”的方法,这种方法使我能够再次呼吁的“努力”。 它第一次完美地运作,但第二次造成国际独立组织。 例外。 如果我删除了声明之二者,则该声明是行之有效的,但却被教授到接近尾声上去,因为这样做是错的。

有些人可以把我指向正确的方向,因为我已经og,但没有结果。

方法......

private String readInput()
{
    String input = null;
    BufferedReader bisr = null;
    try
    {
        bisr = new BufferedReader(new InputStreamReader(System.in));
        input = bisr.readLine();
    }
    catch (Exception e)
    {
        System.out.println("Error: " + e);
    }
    finally
    {
        try
        {
            bisr.close();
        }
        catch (Exception e)
        {
            System.out.println("Error:" + e);
        }
        return input;
    }
}
最佳回答

问题是关闭<代码>。 BufferedReader 并自动关闭<代码> InputStreamReader , 默示关闭System.in

你第二次称“<条码>System.in”的方法已经关闭,这意味着你从中得起。

“关闭通道”仅真正适用于你所投入的资源!

问题回答

The first time it works perfectly, the second time however it causes an IO.Exception

bisr.close() will also close the underlying input stream (in this case System.in). This is why consecutive reads will result in an IOException.

If I remove the statement bisr.close(); it works but was taught to close streams as it is bad practice to leave them open

保留<代码>System.in在处决期间不出现任何问题。

If you don t want to create unnecessarily many objects, you can create the BufferedReader once, and pass that around.

For this particular situation, I would probably just go with

private String readInput() {
    return new Scanner(System.in).nextLine();
}

For System.in it is better to have a global BufferedReader or Scanner you create once. This is because a BufferedReader and Scanner can read more than one line which it buffers for performance so you could be discarding some lines or parts of lines.

public static void main(String... args) throws  InterruptedException {
  for(int i=0;i<5;i++) {
    System.out.println("
read "+readLine());
    // give me time to write more than one line, no problem from a file.
    Thread.sleep(1000);
  }
}

public static String readLine() {
  // can lose input.
  return new Scanner(System.in).nextLine();
}

if I enter the numbers 1,2,3 etc across the keyword quickly.

1

read 1
2
3
4
read 2

5
6
7
read 4

8
9
0
read 7

-
=

read 0

如果我使用全球斯堪的物体,也做同样的事情。

static final Scanner IN = new Scanner(System.in);

public static void main(String... args) throws InterruptedException {
  for (int i = 0; i < 10; i++) {
    System.out.println("
read " + readLine());
    // give me time to write more than one line, no problem from a file.
    Thread.sleep(1000);
  }
}

public static String readLine() {
  return IN.nextLine();
}

印刷

1

read 1
2
3
4
read 2

5
6
read 3

7
8

read 4
9

read 5
0

read 6

read 7

read 8

read 9

read 0




相关问题
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 ...

热门标签