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
6
votes
4 answers

In Java, can I consolidate two similar functions where uses JspWriter and the other PrintWriter?

I have the following class, which as you will see has rather a rather redundant formatNameAndAddress method: package hu.flux.helper; import java.io.PrintWriter; import javax.servlet.jsp.JspWriter; // A holder for formatting data public class…
Brian Kessler
  • 2,187
  • 6
  • 28
  • 58
6
votes
2 answers

Java PrintWriter Error

I'm a long time reader, but first time writer. I am currently trying to implement a logger with AspectJ in our codebase. AspectJ seems to work well, but I am encountering extremely weird Java errors. I am a longtime C++ and .Net developer who is…
6
votes
5 answers

PrintWriter writes only partial text

For some reason my String is written partially by PrintWriter. As a result I am getting partial text in my file. Here's the method: public void new_file_with_text(String text, String fname) { File f = null; try { f =…
casper
  • 1,391
  • 2
  • 16
  • 29
6
votes
2 answers

PrintWriter or any other output stream in Java do not know "\r\n"

I have trouble using PrintWriter or any other output stream to send message between server and client program. It works properly if I use println("abc") to communicate, but it does not work if I use print("abc\r\n"), print("abc\n") or…
Joey
  • 2,732
  • 11
  • 43
  • 63
6
votes
1 answer

PrintWriter not printing out complete string

I have the following code FileWriter F = new FileWriter("out.txt"); PrintWriter H = new PrintWriter(F); H.print(split[split.length - 2]); H.print("END"); When I examine the txt however, the last text is NOT 'END', but part of a word in the string.…
DreamsOfHummus
  • 735
  • 2
  • 7
  • 18
6
votes
3 answers

Write a StringBuilder to a Writer, without toString()

In a Servlet, I am building a very large amount of HTML content in a StringBuilder that, at the end, needs to be written to the response's PrintWriter. In order to use a PrintWriter, it must first call StringBuilder's toString() method to get the…
worpet
  • 3,788
  • 2
  • 31
  • 53
5
votes
1 answer

ISO 8859-1 Encoding of files printed in Java program

I write a program that implements a file structure, the program prints out a product file based on the structure. Product names include letters Æ, Ø and Å. These letters are not displayed correctly in the output file. I use PrintWriter printer =…
user265767
  • 559
  • 3
  • 12
  • 27
5
votes
2 answers

why does a local PrintWriter interfere with another local PrintWriter?

In this program, the third string never gets printed. Why? (This Java program was run on Eclipse Indigo on Ubuntu 10.10.) import java.io.PrintWriter; public class Tester { static void nested() { PrintWriter object2 = new…
H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
5
votes
1 answer

Tomcat 8.5 response.getWriter() has already been called for this response

I'm using Tomcat 8.5.23 in production with Servlets (with Spring autowiring capabilities if relevant) response.getWriter() return null in production It happens also when calling chain.doFilter(req, res); in custom filter: public class MyFilter…
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
5
votes
1 answer

What exactly is the use of flush for a printwriter object?

I'm looking for a theoretical analysis. I mean, how does the buffer system works and what advantage does using flush provide? Kindly illustrate with an example, if possible.
Guptill
  • 61
  • 1
  • 1
  • 3
5
votes
3 answers

Java PrintWriter FileNotFound

I'm having trouble with writing to a txt file. I am getting a FileNotFound Exception, but I don't know why because the file most definitely is there. Here is the code. import java.io.FileNotFoundException; import java.io.PrintWriter; import…
user3720913
  • 105
  • 1
  • 8
5
votes
1 answer

PrintWriter creates file but doesn't write

I used the example code on a website somewhere and it looks like this: package gdt.enlightening; import notify.*; import javax.swing.*; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; public class export { …
user3902017
  • 315
  • 3
  • 12
5
votes
4 answers

Using PrintWriter and OutputStream

I am creating a project with struts and I have a problem using Jasper IReports. I want to export some info into a pdf file and I keep getting the java.lang.IllegalStateException: getOutputStream() has already been call... Exception due to openning a…
Random
  • 1,105
  • 5
  • 24
  • 37
5
votes
3 answers

PrintWriter to print on next line

I have the following code to print a string(from a ResultSet) to a text file: PrintWriter writer = new PrintWriter(new FileOutputStream(file, false)); while(RS.next()) { writer.write(RS.getString(1)+"\n"); } I put a "\n" in the write statement…
Acitropy
  • 113
  • 1
  • 5
  • 18
5
votes
4 answers

PrintWriter add text to file

In my online computer science class I have to write a program to determine the surface gravity on each planet in the solar system. I have gotten almost every aspect of it to work save one. I need to write the surface gravity to a file using a…
spenserpro
  • 53
  • 1
  • 1
  • 3
1 2
3
54 55