I think you have three options,
Option 1: Wrap your PrintStream around a FileOutputStream in append mode.
Option 2: Make a single PrintStream global variable.
Option 3: Use System.setOut(PrintStream) - then you can just call System.out.printf everywhere to write to your PrintStream (honestly though, this is just another way to do option 2).
I think you have three options,
Option 1: Wrap your PrintStream around a FileOutputStream in append mode.
Option 2: Make a single PrintStream global variable.
Option 3: Use System.setOut(PrintStream) - then you can just call System.out.printf everywhere to write to your PrintStream (honestly though, this is just another way to do option 2).
Just declare ps as a static member of the Main class, instead of as a local variable of the main method:
public class Main {
private static PrintStream ps;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
// Don't declare `ps` here; just initialize it...
ps = new PrintStream("brooklynCarpet.txt");
// remaining logic of `main` below...
} catch (FileNotFoundException fnf){
System.out.println("Unexpected exception: " + fnf.toString());
}
}
// ...
}
Now any method in the Main class can access 'ps`.
Videos
PrintStream was the original bridge to deal with encoding characters and other datatypes. If you look at the javadoc for java.io.OutputStream you'll see methods only for writing two distinct data types: byte and int.
In early versions of the JDK (1.0.x), when you wanted to write characters, you could do one of two things, write bytes to an output stream (which are assumed to be in the system default character set):
outputStream.write("foobar".getBytes());
or wrap another outputStream in a PrintStream:
PrintStream printStream = new PrintStream(outputStream);
printStream.write("foobar");
See the difference? PrintStream is handling the character conversion to bytes, as well as encoding (the constructor call above uses the system default encoding, but you could pass it as a parameter). It also provides convenience methods for writing double, boolean, etc....
In fact System.out and System.err are defined as PrintStream instances.
Along comes JDK 1.1, and they realize they need a better way to deal with pure character data, since PrintStream still has the byte based methods for writing. So they introduced the Writer abstract class to deal strictly with char, String and int data.
PrintWriter adds methods for other types like double, boolean, etc...
Nowadays PrintWriter also has format() / printf() methods for format printing, etc...
As a general rule, if you're writing character data, use Writer instances. If you're writing binary (or mixed) data use OutputStream instances.
From the Javadoc for PrintWriter:
Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
Think of it this way: a PrintStream sits on top of some OutputStream. Since output streams deal with bytes rather than characters, the PrintStream must take responsibility for encoding characters into bytes. The OutputStream 'merely' writes the bytes out to a file/console/socket whatever.
A PrintWriter, on the other hand, sits on top of a Writer. Since the Writer is responsible for encoding characters into bytes, the PrintWriter does not do encoding. I just knows about newlines etc. (Yes, PrintWriters do have constructors that take Files and OutputStreams, but those are simply conveniences. For example, PrintWriter(OutputStream).
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.
BTW, In case you are thinking that the PrintWriter really doesn't have much utility, remember that both PrintWriter and PrintStream absorb IOExceptions from printing logic.