开发者

Read InputStream just to the first ENTER?

开发者 https://www.devze.com 2023-03-08 22:43 出处:网络
I have an Inputstream (the stuff what the user is typing into the consol, System.in.read();) Now i want to put this InputStream later into a String it looks something like that:

I have an Inputstream (the stuff what the user is typing into the consol, System.in.read();)

Now i want to put this InputStream later into a String it looks something like that:

    InputStream input = System.in;


    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = input.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }

    System开发者_运维问答.out.println(out);

I know there are a lot of better ways. But i want to finish this one. The only Problem is with != -1 , it never happens. Whats the int for ENTER? So my for ends.

Thx!


this is about the standard way to do it

Scanner scan = new Scanner(System.in);
String line = scan.nextLine();


Normally [CONTROL]+[D] would end input for your application, which should return you a -1 for your method call. Depending on the OS, you might want to try [ENTER] then [CONTROL]+[D] and [ENTER] again.


it should be one of 10 or 13 values depending on os.

0

精彩评论

暂无评论...
验证码 换一张
取 消