Passing string buffer to java program in IntelliJ debug/run

半腔热情 提交于 2019-12-05 00:04:05

As @Maba said we can not use Input redirection operator (any redirection operator) in eclipse/intellij as there no shell but you can simulate the input reading from a file through stdin like the below

       InputStream stdin = null;
        try
        {
        stdin = System.in;
        //Give the file path
        FileInputStream stream = new FileInputStream("SomeTextFile.txt");
        System.setIn(stream);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
                    br.close(); 
                    stream.close()

        //Reset System instream in finally clause
        }finally{             
            System.setIn(stdin);
        }
raymi

You can't do this directly in Intellij but I'm working on a plugin which allows a file to be redirected to stdin. For details see my answer to a similar question here [1] or give the plugin a try [2].

[1] Simulate input from stdin when running a program in intellij

[2] https://github.com/raymi/opcplugin

You can use BufferedReader for this purpose to read from system input:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!