vamaddur
vamaddur

Reputation: 65

How do you print out the output in Java Eclipse?

I need to physically print out my output box (using a printer) for a class.

Can someone please explain to me how to do this?

Upvotes: 0

Views: 8126

Answers (1)

intboolstring
intboolstring

Reputation: 7110

Instead of printing the console from eclipse, you can output all your System.out.println() directly to a file. Basically, you are using the file as a debugger instead of the console window. To do this, you can use the code below.

import java.io.*;
PrintWriter writer = new PrintWriter("debuggerOutput.txt", "UTF-8");
//in your class constructor, you will need to add some exception throws
write.println("I used to be in the debugger, but now I go in the file!!")

For each System.out.println(), add an extra write.println() with the same thing in the parenthesis so it outputs to the file what goes in the console. Then, you will see all your output in a file that you can easily print. At the end, make sure to write.close()

Full code:

import java.io.*;
public class test {

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

        PrintWriter writer = new PrintWriter("myFile.txt", "UTF-8");
        writer.println("The line");

    writer.close();
    }

}

Upvotes: 2

Related Questions