Get file that PrintWriter writes to

How do you get the name of the file that a PrintWriter object is writing to?

PrintWriter myPrintWriter = new PrintWriter(fileName, "UTF-8");

I need the fileName parameter.

Jon Skeet
people
quotationmark

You can't - it might not even be writing to a file, e.g.

StringWriter stringWriter = new StringWriter();
PrinterWriter printWriter = new PrintWriter(stringWriter);

If you need this information, you could potentially create a subclass of PrintWriter which remembers (and exposes) the file it's writing to... but usually the need to do this indicates that you're best off taking a look at your design and reconsidering it. (Personally I tend to avoid PrintWriter anyway, as I don't like exceptions just being swallowed, but that's a different matter.)

people

See more on this question at Stackoverflow