Questions tagged [printwriter]

Java built-in class that prints formatted representations of objects to a text-output stream.

Public class PrintWriter extends Writer.

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.

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().

Since: JDK1.1

Source: Oracle

821 questions
133
votes
6 answers

Java: Difference between PrintStream and PrintWriter

What is the difference between PrintStream and PrintWriter? They have many methods in common due to which I often mix these two classes up. Moreover, I think we can use them for exactly the same things. But there has to be a difference, otherwise,…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
60
votes
5 answers

PrintWriter append method not appending

The following method only writes out the latest item I have added, it does not append to previous entries. What am I doing wrong? public void addNew() { try { PrintWriter pw = new PrintWriter(new File("persons.txt")); int id =…
snnlankrdsm
  • 1,401
  • 5
  • 19
  • 29
59
votes
11 answers

How to use PrintWriter and File classes in Java?

I am trying to understand PrintWriter for a small program I'm making, and I cant seem to get java to make the file and then write on it. When I execute the program below it gives me a Filenotfoundexeption error on line 9. It also fails to make the…
SPARK
  • 595
  • 1
  • 4
  • 5
53
votes
6 answers

How to make the PrintWriter to write UTF-8?

How to make the PrintWriter to write UTF-8? pstream = new PrintWriter(csocket.getOutputStream(), true); String res = "some string"; pstream.println(res); // here I want to output string as UTF-8
yeah its me
  • 1,131
  • 4
  • 18
  • 27
42
votes
7 answers

PrintWriter vs FileWriter in Java

Are PrintWriter and FileWriter in Java the same and no matter which one to use? So far I have used both because their results are the same. Is there some special cases where it makes sense to prefer one over the other? public static void…
ough
  • 549
  • 1
  • 6
  • 10
26
votes
7 answers

how to convert PrintWriter to String or write to a File?

I am generating dynamic page using JSP, I want to save this dynamically generated complete page in file as archive. In JSP, everything is written to PrintWriter out = response.getWriter(); At the end of page, before sending response to client I…
superzoom
  • 461
  • 1
  • 6
  • 8
19
votes
6 answers

FileWrite BufferedWriter and PrintWriter combined

Ok so I am learning about I/O, and I found the following code in one of the slides. can someone please explain why there is a need to have a FileWrite, BufferedWriter and PrintWriter? I know BufferedWriter is to buffer the output and put it all at…
Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65
17
votes
4 answers

How do you set a timeout on BufferedReader and PrintWriter in Java 1.4?

How does one set a timeout on a BufferedReader and a PrintWriter created using a socket connection? Here is the code I have for the server right now, which works until either the server or the client crashes: while(isReceiving){ str = null; …
codewario
  • 19,553
  • 20
  • 90
  • 159
17
votes
7 answers

What is PrintWriter out = response.getWriter() in a Servlet?

I am new to Servlets. Please, tell me about this line, and its use in the Jersey Framework + RESTful web services. PrintWriter out = response.getWriter();
user3498842
  • 171
  • 1
  • 1
  • 6
17
votes
9 answers

What is the exact difference between out.write() and out.print()

In my servlet I gave both out.print and out.write. but both prints in the browser. What is the exact difference between these two and when to use out.print and out.write ?
Kumaran Palani
  • 217
  • 1
  • 3
  • 6
17
votes
2 answers

Difference between JspWriter and PrintWriter in Java EE?

For all you "duplicate" fanatics, there is a similar question on SO right here. The difference is that I paint a vivid example that I can not understand the output of. The documentation for JspWriter and PrintWriter says there are two differences:…
Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
15
votes
4 answers

System.out.println vs PrintWriter

Is there a difference in using these two? When would you use one over the other? System.out.println(result); versus PrintWriter out = new PrintWriter(System.out); out.println(result); out.flush();
Nikhil
  • 797
  • 6
  • 12
  • 30
15
votes
4 answers

error: unreported exception FileNotFoundException; must be caught or declared to be thrown

I'm trying to create a simple program that will output a string to a text file. Using code I found here, I have put together the following code: import java.io.*; public class Testing { public static void main(String[] args) { File file =…
user2956248
  • 197
  • 1
  • 1
  • 9
14
votes
6 answers

Is PrintWriter thread-safe?

Given the following: public class CConsole { public static PrintWriter pw = new PrintWriter(System.out, true); } Is CConsole.pw.format("%d %d", x, y) thread-safe? That is, can multiple threads use this invocation and where is it described as…
H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
12
votes
1 answer

Printwriter println: no new line created

I am trying to decode an outlook .MSG file to a text file, using Apache POI classes. Everything works fine, except for the println method of PrintWriter: it doesn´t create a new line. It just concatenates every sentence directly one after another.…
nicBBB
  • 267
  • 1
  • 4
  • 18
1
2 3
54 55