Input/Output Redirection in DrJava?

On the AP Computer Science mailing list, there was a teacher who wanted to do input/output redirection and who asked why the following didn’t work:

java LetterCounter < input.txt

The Interactions Pane in DrJava is not a DOS command line or Unix-type shell, and it does not support I/O redirection.

The java MyClass arg0 arg1 arg2 thing is only "syntactic sugar" for

MyClass.main(new String[] {"arg0", "arg1", "arg2"})

While the Interactions Pane doesn't support I/O redirection and piping with <, > and |, it is a fully functional Java interpreter.

You can, therefore, create a FileInputStream and use it as System.in before starting your program.

For example, if my program is the following:

[cc lang="java"]import java.io.*;

public class ReadFromStdIn {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
}[/cc]

Then I can start it the following way in DrJava to make it read from the test.txt file on my desktop:

[cc lang="java"]Welcome to DrJava. Working directory is C:\Users\mgricken\Documents\Dev\Java
> import java.io.FileInputStream
> System.setIn(new FileInputStream("C:/Users/mgricken/Desktop/test.txt"))
> java ReadFromStdIn
Foo
Bar
Fuzz
Bam
>[/cc]

The four lines with "Foo Bar Fuzz Bam" are coming from the test.txt file.

I realize that this is different from how Java programs are started in DOS or Unix shells, but it's only because the main method is a very special method. DrJava's Interaction Pane becomes elegant because you can run any method.

I could write the program this way, for example:

[cc lang="java"]import java.io.*;

public class ReadFromStream {
public void readFromStream(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
}[/cc]

Then I could run the program in the following ways:

[cc lang="java"]Welcome to DrJava. Working directory is C:\Users\mgricken\Documents\Dev\Java
> import java.io.FileInputStream
>
> // read from standard in
> new ReadFromStream().readFromStream(System.in)
>
> // read from test.txt
> new ReadFromStream().readFromStream(new FileInputStream("C:/Users/mgricken/Desktop/test.txt"))[/cc]

I generally advise to do as little processing in the main method and rely as little as possible on magic like "where is my standard in coming from?", and to write as much general purpose code as possible like the [cci lang="java"]readFromStream()[/cci] method. It will make code more reusable and work better in DrJava's Interaction Pane.

I hope this helps. Thanks for using DrJava!

Share

About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in DrJava, Uncategorized. Bookmark the permalink.

Leave a Reply