aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/java/io/PrintWriter.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/classpath/java/io/PrintWriter.java')
-rw-r--r--libjava/classpath/java/io/PrintWriter.java75
1 files changed, 72 insertions, 3 deletions
diff --git a/libjava/classpath/java/io/PrintWriter.java b/libjava/classpath/java/io/PrintWriter.java
index 5fd0b162f31..5667e705004 100644
--- a/libjava/classpath/java/io/PrintWriter.java
+++ b/libjava/classpath/java/io/PrintWriter.java
@@ -70,6 +70,11 @@ public class PrintWriter extends Writer
* on this stream.
*/
private boolean error;
+
+ /**
+ * Indicates whether or not the stream has been closed.
+ */
+ private boolean closed;
/**
* This is the underlying <code>Writer</code> we are sending output
@@ -139,6 +144,68 @@ public class PrintWriter extends Writer
}
/**
+ * This initializes a new PrintWriter object to write to the specified
+ * file. It creates a FileOutputStream object and wraps it in an
+ * OutputStreamWriter using the default encoding.
+ * @param file name of the file to write to
+ * @throws FileNotFoundException if the file cannot be written or created
+ *
+ * @since 1.5
+ */
+ public PrintWriter(String file) throws FileNotFoundException
+ {
+ this(new FileOutputStream(file));
+ }
+
+ /**
+ * This initializes a new PrintWriter object to write to the specified
+ * file. It creates a FileOutputStream object and wraps it in an
+ * OutputStreamWriter using the specified encoding.
+ * @param file name of the file to write to
+ * @param enc the encoding to use
+ * @throws FileNotFoundException if the file cannot be written or created
+ * @throws UnsupportedEncodingException if the encoding is not supported
+ *
+ * @since 1.5
+ */
+ public PrintWriter(String file, String enc)
+ throws FileNotFoundException, UnsupportedEncodingException
+ {
+ this(new OutputStreamWriter(new FileOutputStream(file), enc));
+ }
+
+ /**
+ * This initializes a new PrintWriter object to write to the specified
+ * file. It creates a FileOutputStream object and wraps it in an
+ * OutputStreamWriter using the default encoding.
+ * @param file the file to write to
+ * @throws FileNotFoundException if the file cannot be written or created
+ *
+ * @since 1.5
+ */
+ public PrintWriter(File file) throws FileNotFoundException
+ {
+ this(new FileOutputStream(file));
+ }
+
+ /**
+ * This initializes a new PrintWriter object to write to the specified
+ * file. It creates a FileOutputStream object and wraps it in an
+ * OutputStreamWriter using the specified encoding.
+ * @param file the file to write to
+ * @param enc the encoding to use
+ * @throws FileNotFoundException if the file cannot be written or created
+ * @throws UnsupportedEncodingException if the encoding is not supported
+ *
+ * @since 1.5
+ */
+ public PrintWriter(File file, String enc)
+ throws FileNotFoundException, UnsupportedEncodingException
+ {
+ this(new OutputStreamWriter(new FileOutputStream(file), enc));
+ }
+
+ /**
* This method can be called by subclasses to indicate that an error
* has occurred and should be reported by <code>checkError</code>.
*/
@@ -158,7 +225,8 @@ public class PrintWriter extends Writer
*/
public boolean checkError()
{
- flush();
+ if (! closed)
+ flush();
return error;
}
@@ -185,7 +253,8 @@ public class PrintWriter extends Writer
{
try
{
- out.close();
+ out.close();
+ closed = true;
}
catch (IOException ex)
{
@@ -310,7 +379,7 @@ public class PrintWriter extends Writer
* This is the system dependent line separator
*/
private static final char[] line_separator
- = System.getProperty("line.separator").toCharArray();
+ = System.getProperty("line.separator", "\n").toCharArray();
/**
* This method prints a line separator sequence to the stream. The value