Command Line Pipe Input in Java

前端 未结 4 715
忘掉有多难
忘掉有多难 2020-12-05 08:24

Here is a simple piece of code:

import java.io.*;
public class Read {
 public static void main(String[] args) {
     BufferedReader f = new BufferedReader(ne         


        
4条回答
  •  盖世英雄少女心
    2020-12-05 09:05

    You need to terminate your while loop when the line is null, like this:

        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        }
        catch (IOException e) {
            logger.error("IOException reading System.in", e);
            throw e;
        }
        finally {
            if (in != null) {
                in.close();
            }
        }
    

提交回复
热议问题