在IntelliJ调试/运行中向java程序传递字符串缓冲区
在IntelliJ或Eclipse中,如何实现在命令行上运行以下行的等效功能。。。。 :在IntelliJ调试/运行中向java程序传递字符串缓冲区,java,intellij-idea,stringbuffer,Java,Intellij Idea,Stringbuffer,在IntelliJ或Eclipse中,如何实现在命令行上运行以下行的等效功能。。。。 : InputStream stdin = null; try { stdin = System.in; //Give the file path FileInputStream stream = new FileInputStream("SomeTextFile.txt"); System.setIn
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);
}
java MyJava
我试图在IntelliJ中的运行/调试配置的程序参数字段中提供文件的位置,您可以使用它来读取系统输入:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
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);
}
正如@Maba所说,我们不能在eclipse/intellij中使用输入重定向操作符(任何重定向操作符),因为没有shell,但您可以通过stdin模拟从文件读取输入,如下所示
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);
}
你不能直接在Intellij中实现这一点,但我正在开发一个插件,它允许将文件重定向到stdin。有关详细信息,请参见我在此处对类似问题的回答[1],或者尝试一下该插件[2]
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);
}
[1]
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);
}
[2] 我认为这与发出命令时使用的shell有关。从IntelliJ/Eclipse运行时,我猜根本没有使用shell。我在MyJava应用程序中使用BufferedInputStream。问题是如何通过IntelliJ或Eclipse将参数的数量传递给这个程序,就像上面所示的通过bash命令行传递参数一样。
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);
}