< prev index next >
src/java.base/share/classes/java/io/PrintWriter.java
Print this page
@@ -107,10 +107,19 @@
super(out);
this.out = out;
this.autoFlush = autoFlush;
}
+ private PrintWriter(Charset charset, OutputStream out, boolean autoFlush) {
+ this(new BufferedWriter(new OutputStreamWriter(out, charset)), autoFlush);
+
+ // save print stream for error propagation
+ if (out instanceof java.io.PrintStream) {
+ psOut = (PrintStream) out;
+ }
+ }
+
/**
* 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.
@@ -118,11 +127,11 @@
* @param out An output stream
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
*/
public PrintWriter(OutputStream out) {
- this(out, false);
+ this(Charset.defaultCharset(), out, false);
}
/**
* Creates a new PrintWriter from an existing OutputStream. This
* convenience constructor creates the necessary intermediate
@@ -135,16 +144,11 @@
* flush the output buffer
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
*/
public PrintWriter(OutputStream out, boolean autoFlush) {
- this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
-
- // save print stream for error propagation
- if (out instanceof java.io.PrintStream) {
- psOut = (PrintStream) out;
- }
+ this(Charset.defaultCharset(), out, autoFlush);
}
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file name. This convenience constructor creates the necessary
@@ -171,20 +175,11 @@
* access to the file
*
* @since 1.5
*/
public PrintWriter(String fileName) throws FileNotFoundException {
- this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
- false);
- }
-
- /* Private constructor */
- private PrintWriter(Charset charset, File file)
- throws FileNotFoundException
- {
- this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
- false);
+ this(Charset.defaultCharset(), new FileOutputStream(fileName), false);
}
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file name and charset. This convenience constructor creates
@@ -219,11 +214,11 @@
* @since 1.5
*/
public PrintWriter(String fileName, String csn)
throws FileNotFoundException, UnsupportedEncodingException
{
- this(toCharset(csn), new File(fileName));
+ this(toCharset(csn), new FileOutputStream(fileName), false);
}
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file. This convenience constructor creates the necessary
@@ -250,12 +245,11 @@
* denies write access to the file
*
* @since 1.5
*/
public PrintWriter(File file) throws FileNotFoundException {
- this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
- false);
+ this(Charset.defaultCharset(), new FileOutputStream(file), false);
}
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file and charset. This convenience constructor creates the
@@ -290,20 +284,103 @@
* @since 1.5
*/
public PrintWriter(File file, String csn)
throws FileNotFoundException, UnsupportedEncodingException
{
- this(toCharset(csn), file);
+ this(toCharset(csn), new FileOutputStream(file), false);
+ }
+
+ /**
+ * Creates a new PrintWriter, without automatic line flushing, with the
+ * specified file and charset. This convenience constructor creates the
+ * necessary intermediate {@link java.io.OutputStreamWriter
+ * OutputStreamWriter}, which will encode characters using the provided
+ * charset.
+ *
+ * @param file
+ * The file to use as the destination of this writer. If the file
+ * exists then it will be truncated to zero size; otherwise, a new
+ * file will be created. The output will be written to the file
+ * and is buffered.
+ *
+ * @param charset
+ * The {@linkplain java.nio.charset.Charset charset} used for
+ * encoding
+ *
+ * @throws FileNotFoundException
+ * If the given file object does not denote an existing, writable
+ * regular file and a new regular file of that name cannot be
+ * created, or if some other error occurs while opening or
+ * creating the file
+ *
+ * @throws SecurityException
+ * If a security manager is present and {@link
+ * SecurityManager#checkWrite checkWrite(file.getPath())}
+ * denies write access to the file
+ *
+ * @since 1.9
+ */
+ public PrintWriter(File file, Charset charset) throws FileNotFoundException
+ {
+ this(Objects.requireNonNull(charset, "charset"),
+ new FileOutputStream(file),
+ false);
+ }
+
+ /**
+ * Creates a new PrintWriter, without automatic line flushing, with the
+ * specified OutputStream and charset. This convenience constructor creates
+ * the necessary intermediate {@link java.io.OutputStreamWriter
+ * OutputStreamWriter}, which will encode characters using the provided
+ * charset.
+ *
+ * @param out
+ * An output stream
+ *
+ * @param charset
+ * The {@linkplain java.nio.charset.Charset charset} used for
+ * encoding
+ *
+ * @since 1.9
+ */
+ public PrintWriter(OutputStream out, Charset charset)
+ {
+ this(Objects.requireNonNull(charset, "charset"),
+ Objects.requireNonNull(out, "out"),
+ false);
}
/** Checks to make sure that the stream has not been closed */
private void ensureOpen() throws IOException {
if (out == null)
throw new IOException("Stream closed");
}
/**
+ * Activates {@code printf}, or {@code format} methods to automatically
+ * flush the output buffer after writing their data.
+ *
+ * @param autoFlush A boolean; if true, the {@code println},
+ * {@code printf}, or {@code format} methods will
+ * flush the output buffer
+ *
+ * @return the current print writer instance if the actual auto flush does
+ * not change, otherwise a new instance is returned.
+ *
+ * @since 1.9
+ */
+ public PrintWriter withAutoFlush(boolean autoFlush) {
+ if (this.autoFlush == autoFlush) {
+ return this;
+ } else {
+ PrintWriter pw = new PrintWriter(out, autoFlush);
+ pw.psOut = psOut;
+ return pw;
+ }
+ }
+
+ /**
* Flushes the stream.
* @see #checkError()
*/
public void flush() {
try {
< prev index next >