Eclipse Reading Stdin (System.In) from a File

Eclipse reading StdIn(System.in) from console, but will not give any output

Assuming you are on windows hit Ctrl-Z to send the EOF character

Redirect stdin from a file in eclipse with run configuration

This is the announcement and description of the feature for Eclipse 4.5 (Mars): http://eclipse.org/mars/noteworthy/#_assigning_stdin_to_a_file

Stdin can now be assigned to a file in the Common tab of launch
configuration dialogs.
Sample Image

As expected, this is a new feature of the just released Eclipse 4.5 and will therefore not work in an older version.

Getting STDIN in eclipse?

When you run your program you can setup a run configuration. Just click on on the arrow next to the play button and choose "Run configurations..."

In the new run configuration dialog you create a new Java application run configuration. In the tab "Arguments" you can enter the arguments you want the program to receive.

Suggestion: In the last tab, "common", choose to save it as a shared file, this will create a *.launch file that you can use to quickly run the program. You can also create different launch files for different inputs.

How to allocate an input file instead of the stdin in an Eclipse CDT application?

In fact, solution 1 was using a relative path not related to the working directory. Using either button Workspace... or File System... in the GUI enables to select the files which shall already exist.

For example with Workspace definition, the field becomes :

${workspace_loc:/myprogram/inputfile.txt} (same for output)

And it works. Debuggers says :

[Console output redirected to file:/.../myprogram/outputfile.txt]

How to read a txt file as my system.in using eclipse

add folder to your eclipse project in that folder add your input file and then read it using BufferReader as follows
BufferedReader br = null;

try {

String sCurrentLine;

br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt"));

while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

this is one way the other way is to pass the path as argument to your program
as it shown bellow

try {

String sCurrentLine;

br = new BufferedReader(new FileReader(args[0]));

while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

how to do that when your run the application do run configuration and there you will find args you can add what ever path in it for example c:\myinput.txt
hopefully this help



Related Topics



Leave a reply



Submit