0

I would like to achieve the following:

BufferedInputStream is = new BufferedInputStream(System.in);
        int c = 0;
        while((c=is.read())!=-1)
        Files.copy(is, path3, StandardCopyOption.REPLACE_EXISTING);

Hoewver it gets stuck in waiting System.in forever.. Any workaround for that?

Thanks in advance.

Rollerball
  • 12,618
  • 23
  • 92
  • 161
  • It can't block at the constructor. Do you mean at `is.read()`? – Sotirios Delimanolis Aug 06 '13 at 14:25
  • It's blocking trying to read from the console.. did you key-in any input? – Ravi K Thapliyal Aug 06 '13 at 14:26
  • Did you press `return` at the console? Before that, no input is seen by `System.in`. – Marko Topolnik Aug 06 '13 at 14:29
  • 3
    If you want reaction at each keypress, `System.in` is not the way to go. You need AWT events. – Marko Topolnik Aug 06 '13 at 14:30
  • @MarkoTopolnik Therefore something like that will never terminate in anyhow? no flags no typing -1 as an input nothing? Files.copy(System.in, path3, StandardCopyOption.REPLACE_EXISTING); where path3 is a valid and existing path/file – Rollerball Aug 06 '13 at 15:02
  • 1
    Did you type ctrl/z or ctrl/d (system dependent) at the console to provide the EOS? Your code won't terminate until it gets one. However your code makes no sense anyway. You are reading one character from System.in, throwing it away, then copying the entire stream, then repeating until the character read is -1. What is the actual objective here? – user207421 Aug 06 '13 at 21:46
  • @EJP just preparing for the OCPJP7. and thinking of possible tricky questions. – Rollerball Aug 07 '13 at 07:03
  • @EJP However with ctrl+d works! it was just what I was looking for! thanks a lot! – Rollerball Aug 07 '13 at 07:06

1 Answers1

1

If you are trying to stop reading on new line or carriage return or end of stream, you may try-

do {

    try {

        c = is.read();

        // Do something

    } catch (IOException e) {

        e.printStackTrace();

    }

} while ((c != -1) & (c != '\n') & (c != '\r'));
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74