Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

java - Is there a way to redirect input from file to stdin in Netbeans?

I'm developing application with Netbeans and Maven. My application should obtain data from stdin. But I could not understand how to test it. Putting < data.txt into args list does not work.

I need the same as:

$ java MyProgram < data.txt 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I assume you have a thing like:

public static void main(String[] args) {
...
}

This can used as an entry point to your application and before that you change the input channel via:

FileInputStream is = new FileInputStream(new File("test.data"));
System.setIn(is);

The above can be used within a unit/integration test.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...