author | bpb |
Thu, 06 Jun 2019 08:11:25 -0700 | |
changeset 55259 | 61fff1345ee6 |
parent 48252 | 77b88d8f8380 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55259
61fff1345ee6
8219992: Correct the documentation of PrintWriter to refer System.lineSeparator
bpb
parents:
48252
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
7966
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
28 |
import java.util.Objects; |
2 | 29 |
import java.util.Formatter; |
30 |
import java.util.Locale; |
|
7966
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
31 |
import java.nio.charset.Charset; |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
32 |
import java.nio.charset.IllegalCharsetNameException; |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
33 |
import java.nio.charset.UnsupportedCharsetException; |
2 | 34 |
|
35 |
/** |
|
36 |
* Prints formatted representations of objects to a text-output stream. This |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
37 |
* class implements all of the {@code print} methods found in {@link |
2 | 38 |
* PrintStream}. It does not contain methods for writing raw bytes, for which |
39 |
* a program should use unencoded byte streams. |
|
40 |
* |
|
41 |
* <p> Unlike the {@link PrintStream} class, if automatic flushing is enabled |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
42 |
* it will be done only when one of the {@code println}, {@code printf}, or |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
43 |
* {@code format} methods is invoked, rather than whenever a newline character |
2 | 44 |
* happens to be output. These methods use the platform's own notion of line |
45 |
* separator rather than the newline character. |
|
46 |
* |
|
47 |
* <p> Methods in this class never throw I/O exceptions, although some of its |
|
48 |
* constructors may. The client may inquire as to whether any errors have |
|
49 |
* occurred by invoking {@link #checkError checkError()}. |
|
50 |
* |
|
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
51 |
* <p> This class always replaces malformed and unmappable character sequences with |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
52 |
* the charset's default replacement string. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
53 |
* The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
54 |
* control over the encoding process is required. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
55 |
* |
2 | 56 |
* @author Frank Yellin |
57 |
* @author Mark Reinhold |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
14342
diff
changeset
|
58 |
* @since 1.1 |
2 | 59 |
*/ |
60 |
||
61 |
public class PrintWriter extends Writer { |
|
62 |
||
63 |
/** |
|
64 |
* The underlying character-output stream of this |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
65 |
* {@code PrintWriter}. |
2 | 66 |
* |
67 |
* @since 1.2 |
|
68 |
*/ |
|
69 |
protected Writer out; |
|
70 |
||
7966
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
71 |
private final boolean autoFlush; |
2 | 72 |
private boolean trouble = false; |
73 |
private Formatter formatter; |
|
74 |
private PrintStream psOut = null; |
|
75 |
||
76 |
/** |
|
7966
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
77 |
* Returns a charset object for the given charset name. |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
78 |
* @throws NullPointerException is csn is null |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
79 |
* @throws UnsupportedEncodingException if the charset is not supported |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
80 |
*/ |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
81 |
private static Charset toCharset(String csn) |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
82 |
throws UnsupportedEncodingException |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
83 |
{ |
8166
13423c0952ad
7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents:
7966
diff
changeset
|
84 |
Objects.requireNonNull(csn, "charsetName"); |
7966
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
85 |
try { |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
86 |
return Charset.forName(csn); |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
87 |
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
88 |
// UnsupportedEncodingException should be thrown |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
89 |
throw new UnsupportedEncodingException(csn); |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
90 |
} |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
91 |
} |
2 | 92 |
|
93 |
/** |
|
94 |
* Creates a new PrintWriter, without automatic line flushing. |
|
95 |
* |
|
96 |
* @param out A character-output stream |
|
97 |
*/ |
|
98 |
public PrintWriter (Writer out) { |
|
99 |
this(out, false); |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Creates a new PrintWriter. |
|
104 |
* |
|
105 |
* @param out A character-output stream |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
106 |
* @param autoFlush A boolean; if true, the {@code println}, |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
107 |
* {@code printf}, or {@code format} methods will |
2 | 108 |
* flush the output buffer |
109 |
*/ |
|
110 |
public PrintWriter(Writer out, |
|
111 |
boolean autoFlush) { |
|
112 |
super(out); |
|
113 |
this.out = out; |
|
114 |
this.autoFlush = autoFlush; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Creates a new PrintWriter, without automatic line flushing, from an |
|
119 |
* existing OutputStream. This convenience constructor creates the |
|
120 |
* necessary intermediate OutputStreamWriter, which will convert characters |
|
121 |
* into bytes using the default character encoding. |
|
122 |
* |
|
123 |
* @param out An output stream |
|
124 |
* |
|
125 |
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) |
|
126 |
*/ |
|
127 |
public PrintWriter(OutputStream out) { |
|
128 |
this(out, false); |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* Creates a new PrintWriter from an existing OutputStream. This |
|
133 |
* convenience constructor creates the necessary intermediate |
|
134 |
* OutputStreamWriter, which will convert characters into bytes using the |
|
135 |
* default character encoding. |
|
136 |
* |
|
137 |
* @param out An output stream |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
138 |
* @param autoFlush A boolean; if true, the {@code println}, |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
139 |
* {@code printf}, or {@code format} methods will |
2 | 140 |
* flush the output buffer |
141 |
* |
|
142 |
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) |
|
143 |
*/ |
|
144 |
public PrintWriter(OutputStream out, boolean autoFlush) { |
|
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
145 |
this(out, autoFlush, Charset.defaultCharset()); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
146 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
147 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
148 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
149 |
* Creates a new PrintWriter from an existing OutputStream. This |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
150 |
* convenience constructor creates the necessary intermediate |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
151 |
* OutputStreamWriter, which will convert characters into bytes using the |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
152 |
* specified charset. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
153 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
154 |
* @param out An output stream |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
155 |
* @param autoFlush A boolean; if true, the {@code println}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
156 |
* {@code printf}, or {@code format} methods will |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
157 |
* flush the output buffer |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
158 |
* @param charset |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
159 |
* A {@linkplain java.nio.charset.Charset charset} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
160 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
161 |
* @since 10 |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
162 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
163 |
public PrintWriter(OutputStream out, boolean autoFlush, Charset charset) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
164 |
this(new BufferedWriter(new OutputStreamWriter(out, charset)), autoFlush); |
2 | 165 |
|
166 |
// save print stream for error propagation |
|
167 |
if (out instanceof java.io.PrintStream) { |
|
168 |
psOut = (PrintStream) out; |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Creates a new PrintWriter, without automatic line flushing, with the |
|
174 |
* specified file name. This convenience constructor creates the necessary |
|
175 |
* intermediate {@link java.io.OutputStreamWriter OutputStreamWriter}, |
|
176 |
* which will encode characters using the {@linkplain |
|
177 |
* java.nio.charset.Charset#defaultCharset() default charset} for this |
|
178 |
* instance of the Java virtual machine. |
|
179 |
* |
|
180 |
* @param fileName |
|
181 |
* The name of the file to use as the destination of this writer. |
|
182 |
* If the file exists then it will be truncated to zero size; |
|
183 |
* otherwise, a new file will be created. The output will be |
|
184 |
* written to the file and is buffered. |
|
185 |
* |
|
186 |
* @throws FileNotFoundException |
|
187 |
* If the given string does not denote an existing, writable |
|
188 |
* regular file and a new regular file of that name cannot be |
|
189 |
* created, or if some other error occurs while opening or |
|
190 |
* creating the file |
|
191 |
* |
|
192 |
* @throws SecurityException |
|
193 |
* If a security manager is present and {@link |
|
194 |
* SecurityManager#checkWrite checkWrite(fileName)} denies write |
|
195 |
* access to the file |
|
196 |
* |
|
197 |
* @since 1.5 |
|
198 |
*/ |
|
199 |
public PrintWriter(String fileName) throws FileNotFoundException { |
|
200 |
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))), |
|
201 |
false); |
|
202 |
} |
|
203 |
||
7966
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
204 |
/* Private constructor */ |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
205 |
private PrintWriter(Charset charset, File file) |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
206 |
throws FileNotFoundException |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
207 |
{ |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
208 |
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)), |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
209 |
false); |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
210 |
} |
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
211 |
|
2 | 212 |
/** |
213 |
* Creates a new PrintWriter, without automatic line flushing, with the |
|
214 |
* specified file name and charset. This convenience constructor creates |
|
215 |
* the necessary intermediate {@link java.io.OutputStreamWriter |
|
216 |
* OutputStreamWriter}, which will encode characters using the provided |
|
217 |
* charset. |
|
218 |
* |
|
219 |
* @param fileName |
|
220 |
* The name of the file to use as the destination of this writer. |
|
221 |
* If the file exists then it will be truncated to zero size; |
|
222 |
* otherwise, a new file will be created. The output will be |
|
223 |
* written to the file and is buffered. |
|
224 |
* |
|
225 |
* @param csn |
|
226 |
* The name of a supported {@linkplain java.nio.charset.Charset |
|
227 |
* charset} |
|
228 |
* |
|
229 |
* @throws FileNotFoundException |
|
230 |
* If the given string does not denote an existing, writable |
|
231 |
* regular file and a new regular file of that name cannot be |
|
232 |
* created, or if some other error occurs while opening or |
|
233 |
* creating the file |
|
234 |
* |
|
235 |
* @throws SecurityException |
|
236 |
* If a security manager is present and {@link |
|
237 |
* SecurityManager#checkWrite checkWrite(fileName)} denies write |
|
238 |
* access to the file |
|
239 |
* |
|
240 |
* @throws UnsupportedEncodingException |
|
241 |
* If the named charset is not supported |
|
242 |
* |
|
243 |
* @since 1.5 |
|
244 |
*/ |
|
245 |
public PrintWriter(String fileName, String csn) |
|
246 |
throws FileNotFoundException, UnsupportedEncodingException |
|
247 |
{ |
|
7966
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
248 |
this(toCharset(csn), new File(fileName)); |
2 | 249 |
} |
250 |
||
251 |
/** |
|
252 |
* Creates a new PrintWriter, without automatic line flushing, with the |
|
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
253 |
* specified file name and charset. This convenience constructor creates |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
254 |
* the necessary intermediate {@link java.io.OutputStreamWriter |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
255 |
* OutputStreamWriter}, which will encode characters using the provided |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
256 |
* charset. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
257 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
258 |
* @param fileName |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
259 |
* The name of the file to use as the destination of this writer. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
260 |
* If the file exists then it will be truncated to zero size; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
261 |
* otherwise, a new file will be created. The output will be |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
262 |
* written to the file and is buffered. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
263 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
264 |
* @param charset |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
265 |
* A {@linkplain java.nio.charset.Charset charset} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
266 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
267 |
* @throws IOException |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
268 |
* if an I/O error occurs while opening or creating the file |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
269 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
270 |
* @throws SecurityException |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
271 |
* If a security manager is present and {@link |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
272 |
* SecurityManager#checkWrite checkWrite(fileName)} denies write |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
273 |
* access to the file |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
274 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
275 |
* @since 10 |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
276 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
277 |
public PrintWriter(String fileName, Charset charset) throws IOException { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
278 |
this(Objects.requireNonNull(charset, "charset"), new File(fileName)); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
279 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
280 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
281 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
282 |
* Creates a new PrintWriter, without automatic line flushing, with the |
2 | 283 |
* specified file. This convenience constructor creates the necessary |
284 |
* intermediate {@link java.io.OutputStreamWriter OutputStreamWriter}, |
|
285 |
* which will encode characters using the {@linkplain |
|
286 |
* java.nio.charset.Charset#defaultCharset() default charset} for this |
|
287 |
* instance of the Java virtual machine. |
|
288 |
* |
|
289 |
* @param file |
|
290 |
* The file to use as the destination of this writer. If the file |
|
291 |
* exists then it will be truncated to zero size; otherwise, a new |
|
292 |
* file will be created. The output will be written to the file |
|
293 |
* and is buffered. |
|
294 |
* |
|
295 |
* @throws FileNotFoundException |
|
296 |
* If the given file object does not denote an existing, writable |
|
297 |
* regular file and a new regular file of that name cannot be |
|
298 |
* created, or if some other error occurs while opening or |
|
299 |
* creating the file |
|
300 |
* |
|
301 |
* @throws SecurityException |
|
302 |
* If a security manager is present and {@link |
|
303 |
* SecurityManager#checkWrite checkWrite(file.getPath())} |
|
304 |
* denies write access to the file |
|
305 |
* |
|
306 |
* @since 1.5 |
|
307 |
*/ |
|
308 |
public PrintWriter(File file) throws FileNotFoundException { |
|
309 |
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))), |
|
310 |
false); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Creates a new PrintWriter, without automatic line flushing, with the |
|
315 |
* specified file and charset. This convenience constructor creates the |
|
316 |
* necessary intermediate {@link java.io.OutputStreamWriter |
|
317 |
* OutputStreamWriter}, which will encode characters using the provided |
|
318 |
* charset. |
|
319 |
* |
|
320 |
* @param file |
|
321 |
* The file to use as the destination of this writer. If the file |
|
322 |
* exists then it will be truncated to zero size; otherwise, a new |
|
323 |
* file will be created. The output will be written to the file |
|
324 |
* and is buffered. |
|
325 |
* |
|
326 |
* @param csn |
|
327 |
* The name of a supported {@linkplain java.nio.charset.Charset |
|
328 |
* charset} |
|
329 |
* |
|
330 |
* @throws FileNotFoundException |
|
331 |
* If the given file object does not denote an existing, writable |
|
332 |
* regular file and a new regular file of that name cannot be |
|
333 |
* created, or if some other error occurs while opening or |
|
334 |
* creating the file |
|
335 |
* |
|
336 |
* @throws SecurityException |
|
337 |
* If a security manager is present and {@link |
|
338 |
* SecurityManager#checkWrite checkWrite(file.getPath())} |
|
339 |
* denies write access to the file |
|
340 |
* |
|
341 |
* @throws UnsupportedEncodingException |
|
342 |
* If the named charset is not supported |
|
343 |
* |
|
344 |
* @since 1.5 |
|
345 |
*/ |
|
346 |
public PrintWriter(File file, String csn) |
|
347 |
throws FileNotFoundException, UnsupportedEncodingException |
|
348 |
{ |
|
7966
a23e3f47c5a8
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents:
5506
diff
changeset
|
349 |
this(toCharset(csn), file); |
2 | 350 |
} |
351 |
||
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
352 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
353 |
* Creates a new PrintWriter, without automatic line flushing, with the |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
354 |
* specified file and charset. This convenience constructor creates the |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
355 |
* necessary intermediate {@link java.io.OutputStreamWriter |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
356 |
* OutputStreamWriter}, which will encode characters using the provided |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
357 |
* charset. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
358 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
359 |
* @param file |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
360 |
* The file to use as the destination of this writer. If the file |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
361 |
* exists then it will be truncated to zero size; otherwise, a new |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
362 |
* file will be created. The output will be written to the file |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
363 |
* and is buffered. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
364 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
365 |
* @param charset |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
366 |
* A {@linkplain java.nio.charset.Charset charset} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
367 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
368 |
* @throws IOException |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
369 |
* if an I/O error occurs while opening or creating the file |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
370 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
371 |
* @throws SecurityException |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
372 |
* If a security manager is present and {@link |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
373 |
* SecurityManager#checkWrite checkWrite(file.getPath())} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
374 |
* denies write access to the file |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
375 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
376 |
* @since 10 |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
377 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
378 |
public PrintWriter(File file, Charset charset) throws IOException { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
379 |
this(Objects.requireNonNull(charset, "charset"), file); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
380 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47216
diff
changeset
|
381 |
|
2 | 382 |
/** Checks to make sure that the stream has not been closed */ |
383 |
private void ensureOpen() throws IOException { |
|
384 |
if (out == null) |
|
385 |
throw new IOException("Stream closed"); |
|
386 |
} |
|
387 |
||
388 |
/** |
|
389 |
* Flushes the stream. |
|
390 |
* @see #checkError() |
|
391 |
*/ |
|
392 |
public void flush() { |
|
393 |
try { |
|
394 |
synchronized (lock) { |
|
395 |
ensureOpen(); |
|
396 |
out.flush(); |
|
397 |
} |
|
398 |
} |
|
399 |
catch (IOException x) { |
|
400 |
trouble = true; |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
/** |
|
405 |
* Closes the stream and releases any system resources associated |
|
406 |
* with it. Closing a previously closed stream has no effect. |
|
407 |
* |
|
408 |
* @see #checkError() |
|
409 |
*/ |
|
410 |
public void close() { |
|
411 |
try { |
|
412 |
synchronized (lock) { |
|
413 |
if (out == null) |
|
414 |
return; |
|
415 |
out.close(); |
|
416 |
out = null; |
|
417 |
} |
|
418 |
} |
|
419 |
catch (IOException x) { |
|
420 |
trouble = true; |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
/** |
|
425 |
* Flushes the stream if it's not closed and checks its error state. |
|
426 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
427 |
* @return {@code true} if the print stream has encountered an error, |
2 | 428 |
* either on the underlying output stream or during a format |
429 |
* conversion. |
|
430 |
*/ |
|
431 |
public boolean checkError() { |
|
432 |
if (out != null) { |
|
433 |
flush(); |
|
434 |
} |
|
435 |
if (out instanceof java.io.PrintWriter) { |
|
436 |
PrintWriter pw = (PrintWriter) out; |
|
437 |
return pw.checkError(); |
|
438 |
} else if (psOut != null) { |
|
439 |
return psOut.checkError(); |
|
440 |
} |
|
441 |
return trouble; |
|
442 |
} |
|
443 |
||
444 |
/** |
|
445 |
* Indicates that an error has occurred. |
|
446 |
* |
|
447 |
* <p> This method will cause subsequent invocations of {@link |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
448 |
* #checkError()} to return {@code true} until {@link |
2 | 449 |
* #clearError()} is invoked. |
450 |
*/ |
|
451 |
protected void setError() { |
|
452 |
trouble = true; |
|
453 |
} |
|
454 |
||
455 |
/** |
|
456 |
* Clears the error state of this stream. |
|
457 |
* |
|
458 |
* <p> This method will cause subsequent invocations of {@link |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
459 |
* #checkError()} to return {@code false} until another write |
2 | 460 |
* operation fails and invokes {@link #setError()}. |
461 |
* |
|
462 |
* @since 1.6 |
|
463 |
*/ |
|
464 |
protected void clearError() { |
|
465 |
trouble = false; |
|
466 |
} |
|
467 |
||
468 |
/* |
|
469 |
* Exception-catching, synchronized output operations, |
|
470 |
* which also implement the write() methods of Writer |
|
471 |
*/ |
|
472 |
||
473 |
/** |
|
474 |
* Writes a single character. |
|
475 |
* @param c int specifying a character to be written. |
|
476 |
*/ |
|
477 |
public void write(int c) { |
|
478 |
try { |
|
479 |
synchronized (lock) { |
|
480 |
ensureOpen(); |
|
481 |
out.write(c); |
|
482 |
} |
|
483 |
} |
|
484 |
catch (InterruptedIOException x) { |
|
485 |
Thread.currentThread().interrupt(); |
|
486 |
} |
|
487 |
catch (IOException x) { |
|
488 |
trouble = true; |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
/** |
|
493 |
* Writes A Portion of an array of characters. |
|
494 |
* @param buf Array of characters |
|
495 |
* @param off Offset from which to start writing characters |
|
496 |
* @param len Number of characters to write |
|
38373
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
497 |
* |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
498 |
* @throws IndexOutOfBoundsException |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
499 |
* If the values of the {@code off} and {@code len} parameters |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
500 |
* cause the corresponding method of the underlying {@code Writer} |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
501 |
* to throw an {@code IndexOutOfBoundsException} |
2 | 502 |
*/ |
503 |
public void write(char buf[], int off, int len) { |
|
504 |
try { |
|
505 |
synchronized (lock) { |
|
506 |
ensureOpen(); |
|
507 |
out.write(buf, off, len); |
|
508 |
} |
|
509 |
} |
|
510 |
catch (InterruptedIOException x) { |
|
511 |
Thread.currentThread().interrupt(); |
|
512 |
} |
|
513 |
catch (IOException x) { |
|
514 |
trouble = true; |
|
515 |
} |
|
516 |
} |
|
517 |
||
518 |
/** |
|
519 |
* Writes an array of characters. This method cannot be inherited from the |
|
520 |
* Writer class because it must suppress I/O exceptions. |
|
521 |
* @param buf Array of characters to be written |
|
522 |
*/ |
|
523 |
public void write(char buf[]) { |
|
524 |
write(buf, 0, buf.length); |
|
525 |
} |
|
526 |
||
527 |
/** |
|
528 |
* Writes a portion of a string. |
|
529 |
* @param s A String |
|
530 |
* @param off Offset from which to start writing characters |
|
531 |
* @param len Number of characters to write |
|
38373
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
532 |
* |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
533 |
* @throws IndexOutOfBoundsException |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
534 |
* If the values of the {@code off} and {@code len} parameters |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
535 |
* cause the corresponding method of the underlying {@code Writer} |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
536 |
* to throw an {@code IndexOutOfBoundsException} |
2 | 537 |
*/ |
538 |
public void write(String s, int off, int len) { |
|
539 |
try { |
|
540 |
synchronized (lock) { |
|
541 |
ensureOpen(); |
|
542 |
out.write(s, off, len); |
|
543 |
} |
|
544 |
} |
|
545 |
catch (InterruptedIOException x) { |
|
546 |
Thread.currentThread().interrupt(); |
|
547 |
} |
|
548 |
catch (IOException x) { |
|
549 |
trouble = true; |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
/** |
|
554 |
* Writes a string. This method cannot be inherited from the Writer class |
|
555 |
* because it must suppress I/O exceptions. |
|
556 |
* @param s String to be written |
|
557 |
*/ |
|
558 |
public void write(String s) { |
|
559 |
write(s, 0, s.length()); |
|
560 |
} |
|
561 |
||
562 |
private void newLine() { |
|
563 |
try { |
|
564 |
synchronized (lock) { |
|
565 |
ensureOpen(); |
|
28422
a571480286c5
8068498: Remove constructor dependency on line.separator from PrintWriter and BufferedWriter
redestad
parents:
25859
diff
changeset
|
566 |
out.write(System.lineSeparator()); |
2 | 567 |
if (autoFlush) |
568 |
out.flush(); |
|
569 |
} |
|
570 |
} |
|
571 |
catch (InterruptedIOException x) { |
|
572 |
Thread.currentThread().interrupt(); |
|
573 |
} |
|
574 |
catch (IOException x) { |
|
575 |
trouble = true; |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
/* Methods that do not terminate lines */ |
|
580 |
||
581 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
582 |
* Prints a boolean value. The string produced by {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
583 |
* java.lang.String#valueOf(boolean)} is translated into bytes |
2 | 584 |
* according to the platform's default character encoding, and these bytes |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
585 |
* are written in exactly the manner of the {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
586 |
* #write(int)} method. |
2 | 587 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
588 |
* @param b The {@code boolean} to be printed |
2 | 589 |
*/ |
590 |
public void print(boolean b) { |
|
40410 | 591 |
write(String.valueOf(b)); |
2 | 592 |
} |
593 |
||
594 |
/** |
|
595 |
* Prints a character. The character is translated into one or more bytes |
|
596 |
* according to the platform's default character encoding, and these bytes |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
597 |
* are written in exactly the manner of the {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
598 |
* #write(int)} method. |
2 | 599 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
600 |
* @param c The {@code char} to be printed |
2 | 601 |
*/ |
602 |
public void print(char c) { |
|
603 |
write(c); |
|
604 |
} |
|
605 |
||
606 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
607 |
* Prints an integer. The string produced by {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
608 |
* java.lang.String#valueOf(int)} is translated into bytes according |
2 | 609 |
* to the platform's default character encoding, and these bytes are |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
610 |
* written in exactly the manner of the {@link #write(int)} |
2 | 611 |
* method. |
612 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
613 |
* @param i The {@code int} to be printed |
2 | 614 |
* @see java.lang.Integer#toString(int) |
615 |
*/ |
|
616 |
public void print(int i) { |
|
617 |
write(String.valueOf(i)); |
|
618 |
} |
|
619 |
||
620 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
621 |
* Prints a long integer. The string produced by {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
622 |
* java.lang.String#valueOf(long)} is translated into bytes |
2 | 623 |
* according to the platform's default character encoding, and these bytes |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
624 |
* are written in exactly the manner of the {@link #write(int)} |
2 | 625 |
* method. |
626 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
627 |
* @param l The {@code long} to be printed |
2 | 628 |
* @see java.lang.Long#toString(long) |
629 |
*/ |
|
630 |
public void print(long l) { |
|
631 |
write(String.valueOf(l)); |
|
632 |
} |
|
633 |
||
634 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
635 |
* Prints a floating-point number. The string produced by {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
636 |
* java.lang.String#valueOf(float)} is translated into bytes |
2 | 637 |
* according to the platform's default character encoding, and these bytes |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
638 |
* are written in exactly the manner of the {@link #write(int)} |
2 | 639 |
* method. |
640 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
641 |
* @param f The {@code float} to be printed |
2 | 642 |
* @see java.lang.Float#toString(float) |
643 |
*/ |
|
644 |
public void print(float f) { |
|
645 |
write(String.valueOf(f)); |
|
646 |
} |
|
647 |
||
648 |
/** |
|
649 |
* Prints a double-precision floating-point number. The string produced by |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
650 |
* {@link java.lang.String#valueOf(double)} is translated into |
2 | 651 |
* bytes according to the platform's default character encoding, and these |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
652 |
* bytes are written in exactly the manner of the {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
653 |
* #write(int)} method. |
2 | 654 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
655 |
* @param d The {@code double} to be printed |
2 | 656 |
* @see java.lang.Double#toString(double) |
657 |
*/ |
|
658 |
public void print(double d) { |
|
659 |
write(String.valueOf(d)); |
|
660 |
} |
|
661 |
||
662 |
/** |
|
663 |
* Prints an array of characters. The characters are converted into bytes |
|
664 |
* according to the platform's default character encoding, and these bytes |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
665 |
* are written in exactly the manner of the {@link #write(int)} |
2 | 666 |
* method. |
667 |
* |
|
668 |
* @param s The array of chars to be printed |
|
669 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
670 |
* @throws NullPointerException If {@code s} is {@code null} |
2 | 671 |
*/ |
672 |
public void print(char s[]) { |
|
673 |
write(s); |
|
674 |
} |
|
675 |
||
676 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
677 |
* Prints a string. If the argument is {@code null} then the string |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
678 |
* {@code "null"} is printed. Otherwise, the string's characters are |
2 | 679 |
* converted into bytes according to the platform's default character |
680 |
* encoding, and these bytes are written in exactly the manner of the |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
681 |
* {@link #write(int)} method. |
2 | 682 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
683 |
* @param s The {@code String} to be printed |
2 | 684 |
*/ |
685 |
public void print(String s) { |
|
40410 | 686 |
write(String.valueOf(s)); |
2 | 687 |
} |
688 |
||
689 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
690 |
* Prints an object. The string produced by the {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
691 |
* java.lang.String#valueOf(Object)} method is translated into bytes |
2 | 692 |
* according to the platform's default character encoding, and these bytes |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
693 |
* are written in exactly the manner of the {@link #write(int)} |
2 | 694 |
* method. |
695 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
696 |
* @param obj The {@code Object} to be printed |
2 | 697 |
* @see java.lang.Object#toString() |
698 |
*/ |
|
699 |
public void print(Object obj) { |
|
700 |
write(String.valueOf(obj)); |
|
701 |
} |
|
702 |
||
703 |
/* Methods that do terminate lines */ |
|
704 |
||
705 |
/** |
|
706 |
* Terminates the current line by writing the line separator string. The |
|
55259
61fff1345ee6
8219992: Correct the documentation of PrintWriter to refer System.lineSeparator
bpb
parents:
48252
diff
changeset
|
707 |
* line separator is {@link System#lineSeparator()} and is not necessarily |
61fff1345ee6
8219992: Correct the documentation of PrintWriter to refer System.lineSeparator
bpb
parents:
48252
diff
changeset
|
708 |
* a single newline character ({@code '\n'}). |
2 | 709 |
*/ |
710 |
public void println() { |
|
711 |
newLine(); |
|
712 |
} |
|
713 |
||
714 |
/** |
|
715 |
* Prints a boolean value and then terminates the line. This method behaves |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
716 |
* as though it invokes {@link #print(boolean)} and then |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
717 |
* {@link #println()}. |
2 | 718 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
719 |
* @param x the {@code boolean} value to be printed |
2 | 720 |
*/ |
721 |
public void println(boolean x) { |
|
722 |
synchronized (lock) { |
|
723 |
print(x); |
|
724 |
println(); |
|
725 |
} |
|
726 |
} |
|
727 |
||
728 |
/** |
|
729 |
* Prints a character and then terminates the line. This method behaves as |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
730 |
* though it invokes {@link #print(char)} and then {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
731 |
* #println()}. |
2 | 732 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
733 |
* @param x the {@code char} value to be printed |
2 | 734 |
*/ |
735 |
public void println(char x) { |
|
736 |
synchronized (lock) { |
|
737 |
print(x); |
|
738 |
println(); |
|
739 |
} |
|
740 |
} |
|
741 |
||
742 |
/** |
|
743 |
* Prints an integer and then terminates the line. This method behaves as |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
744 |
* though it invokes {@link #print(int)} and then {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
745 |
* #println()}. |
2 | 746 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
747 |
* @param x the {@code int} value to be printed |
2 | 748 |
*/ |
749 |
public void println(int x) { |
|
750 |
synchronized (lock) { |
|
751 |
print(x); |
|
752 |
println(); |
|
753 |
} |
|
754 |
} |
|
755 |
||
756 |
/** |
|
757 |
* Prints a long integer and then terminates the line. This method behaves |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
758 |
* as though it invokes {@link #print(long)} and then |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
759 |
* {@link #println()}. |
2 | 760 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
761 |
* @param x the {@code long} value to be printed |
2 | 762 |
*/ |
763 |
public void println(long x) { |
|
764 |
synchronized (lock) { |
|
765 |
print(x); |
|
766 |
println(); |
|
767 |
} |
|
768 |
} |
|
769 |
||
770 |
/** |
|
771 |
* Prints a floating-point number and then terminates the line. This method |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
772 |
* behaves as though it invokes {@link #print(float)} and then |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
773 |
* {@link #println()}. |
2 | 774 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
775 |
* @param x the {@code float} value to be printed |
2 | 776 |
*/ |
777 |
public void println(float x) { |
|
778 |
synchronized (lock) { |
|
779 |
print(x); |
|
780 |
println(); |
|
781 |
} |
|
782 |
} |
|
783 |
||
784 |
/** |
|
785 |
* Prints a double-precision floating-point number and then terminates the |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
786 |
* line. This method behaves as though it invokes {@link |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
787 |
* #print(double)} and then {@link #println()}. |
2 | 788 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
789 |
* @param x the {@code double} value to be printed |
2 | 790 |
*/ |
791 |
public void println(double x) { |
|
792 |
synchronized (lock) { |
|
793 |
print(x); |
|
794 |
println(); |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
/** |
|
799 |
* Prints an array of characters and then terminates the line. This method |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
800 |
* behaves as though it invokes {@link #print(char[])} and then |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
801 |
* {@link #println()}. |
2 | 802 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
803 |
* @param x the array of {@code char} values to be printed |
2 | 804 |
*/ |
805 |
public void println(char x[]) { |
|
806 |
synchronized (lock) { |
|
807 |
print(x); |
|
808 |
println(); |
|
809 |
} |
|
810 |
} |
|
811 |
||
812 |
/** |
|
813 |
* Prints a String and then terminates the line. This method behaves as |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
814 |
* though it invokes {@link #print(String)} and then |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
815 |
* {@link #println()}. |
2 | 816 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
817 |
* @param x the {@code String} value to be printed |
2 | 818 |
*/ |
819 |
public void println(String x) { |
|
820 |
synchronized (lock) { |
|
821 |
print(x); |
|
822 |
println(); |
|
823 |
} |
|
824 |
} |
|
825 |
||
826 |
/** |
|
827 |
* Prints an Object and then terminates the line. This method calls |
|
828 |
* at first String.valueOf(x) to get the printed object's string value, |
|
829 |
* then behaves as |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
830 |
* though it invokes {@link #print(String)} and then |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
831 |
* {@link #println()}. |
2 | 832 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
833 |
* @param x The {@code Object} to be printed. |
2 | 834 |
*/ |
835 |
public void println(Object x) { |
|
836 |
String s = String.valueOf(x); |
|
837 |
synchronized (lock) { |
|
838 |
print(s); |
|
839 |
println(); |
|
840 |
} |
|
841 |
} |
|
842 |
||
843 |
/** |
|
844 |
* A convenience method to write a formatted string to this writer using |
|
845 |
* the specified format string and arguments. If automatic flushing is |
|
846 |
* enabled, calls to this method will flush the output buffer. |
|
847 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
848 |
* <p> An invocation of this method of the form |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
849 |
* {@code out.printf(format, args)} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
850 |
* behaves in exactly the same way as the invocation |
2 | 851 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
852 |
* <pre>{@code |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
853 |
* out.format(format, args) |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
854 |
* }</pre> |
2 | 855 |
* |
856 |
* @param format |
|
857 |
* A format string as described in <a |
|
858 |
* href="../util/Formatter.html#syntax">Format string syntax</a>. |
|
859 |
* |
|
860 |
* @param args |
|
861 |
* Arguments referenced by the format specifiers in the format |
|
862 |
* string. If there are more arguments than format specifiers, the |
|
863 |
* extra arguments are ignored. The number of arguments is |
|
864 |
* variable and may be zero. The maximum number of arguments is |
|
865 |
* limited by the maximum dimension of a Java array as defined by |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8166
diff
changeset
|
866 |
* <cite>The Java™ Virtual Machine Specification</cite>. |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8166
diff
changeset
|
867 |
* The behaviour on a |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
868 |
* {@code null} argument depends on the <a |
2 | 869 |
* href="../util/Formatter.html#syntax">conversion</a>. |
870 |
* |
|
14014 | 871 |
* @throws java.util.IllegalFormatException |
2 | 872 |
* If a format string contains an illegal syntax, a format |
873 |
* specifier that is incompatible with the given arguments, |
|
874 |
* insufficient arguments given the format string, or other |
|
875 |
* illegal conditions. For specification of all possible |
|
876 |
* formatting errors, see the <a |
|
877 |
* href="../util/Formatter.html#detail">Details</a> section of the |
|
878 |
* formatter class specification. |
|
879 |
* |
|
880 |
* @throws NullPointerException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
881 |
* If the {@code format} is {@code null} |
2 | 882 |
* |
883 |
* @return This writer |
|
884 |
* |
|
885 |
* @since 1.5 |
|
886 |
*/ |
|
887 |
public PrintWriter printf(String format, Object ... args) { |
|
888 |
return format(format, args); |
|
889 |
} |
|
890 |
||
891 |
/** |
|
892 |
* A convenience method to write a formatted string to this writer using |
|
893 |
* the specified format string and arguments. If automatic flushing is |
|
894 |
* enabled, calls to this method will flush the output buffer. |
|
895 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
896 |
* <p> An invocation of this method of the form |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
897 |
* {@code out.printf(l, format, args)} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
898 |
* behaves in exactly the same way as the invocation |
2 | 899 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
900 |
* <pre>{@code |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
901 |
* out.format(l, format, args) |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
902 |
* }</pre> |
2 | 903 |
* |
904 |
* @param l |
|
905 |
* The {@linkplain java.util.Locale locale} to apply during |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
906 |
* formatting. If {@code l} is {@code null} then no localization |
2 | 907 |
* is applied. |
908 |
* |
|
909 |
* @param format |
|
910 |
* A format string as described in <a |
|
911 |
* href="../util/Formatter.html#syntax">Format string syntax</a>. |
|
912 |
* |
|
913 |
* @param args |
|
914 |
* Arguments referenced by the format specifiers in the format |
|
915 |
* string. If there are more arguments than format specifiers, the |
|
916 |
* extra arguments are ignored. The number of arguments is |
|
917 |
* variable and may be zero. The maximum number of arguments is |
|
918 |
* limited by the maximum dimension of a Java array as defined by |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8166
diff
changeset
|
919 |
* <cite>The Java™ Virtual Machine Specification</cite>. |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8166
diff
changeset
|
920 |
* The behaviour on a |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
921 |
* {@code null} argument depends on the <a |
2 | 922 |
* href="../util/Formatter.html#syntax">conversion</a>. |
923 |
* |
|
14014 | 924 |
* @throws java.util.IllegalFormatException |
2 | 925 |
* If a format string contains an illegal syntax, a format |
926 |
* specifier that is incompatible with the given arguments, |
|
927 |
* insufficient arguments given the format string, or other |
|
928 |
* illegal conditions. For specification of all possible |
|
929 |
* formatting errors, see the <a |
|
930 |
* href="../util/Formatter.html#detail">Details</a> section of the |
|
931 |
* formatter class specification. |
|
932 |
* |
|
933 |
* @throws NullPointerException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
934 |
* If the {@code format} is {@code null} |
2 | 935 |
* |
936 |
* @return This writer |
|
937 |
* |
|
938 |
* @since 1.5 |
|
939 |
*/ |
|
940 |
public PrintWriter printf(Locale l, String format, Object ... args) { |
|
941 |
return format(l, format, args); |
|
942 |
} |
|
943 |
||
944 |
/** |
|
945 |
* Writes a formatted string to this writer using the specified format |
|
946 |
* string and arguments. If automatic flushing is enabled, calls to this |
|
947 |
* method will flush the output buffer. |
|
948 |
* |
|
949 |
* <p> The locale always used is the one returned by {@link |
|
950 |
* java.util.Locale#getDefault() Locale.getDefault()}, regardless of any |
|
951 |
* previous invocations of other formatting methods on this object. |
|
952 |
* |
|
953 |
* @param format |
|
954 |
* A format string as described in <a |
|
955 |
* href="../util/Formatter.html#syntax">Format string syntax</a>. |
|
956 |
* |
|
957 |
* @param args |
|
958 |
* Arguments referenced by the format specifiers in the format |
|
959 |
* string. If there are more arguments than format specifiers, the |
|
960 |
* extra arguments are ignored. The number of arguments is |
|
961 |
* variable and may be zero. The maximum number of arguments is |
|
962 |
* limited by the maximum dimension of a Java array as defined by |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8166
diff
changeset
|
963 |
* <cite>The Java™ Virtual Machine Specification</cite>. |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8166
diff
changeset
|
964 |
* The behaviour on a |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
965 |
* {@code null} argument depends on the <a |
2 | 966 |
* href="../util/Formatter.html#syntax">conversion</a>. |
967 |
* |
|
14014 | 968 |
* @throws java.util.IllegalFormatException |
2 | 969 |
* If a format string contains an illegal syntax, a format |
970 |
* specifier that is incompatible with the given arguments, |
|
971 |
* insufficient arguments given the format string, or other |
|
972 |
* illegal conditions. For specification of all possible |
|
973 |
* formatting errors, see the <a |
|
974 |
* href="../util/Formatter.html#detail">Details</a> section of the |
|
975 |
* Formatter class specification. |
|
976 |
* |
|
977 |
* @throws NullPointerException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
978 |
* If the {@code format} is {@code null} |
2 | 979 |
* |
980 |
* @return This writer |
|
981 |
* |
|
982 |
* @since 1.5 |
|
983 |
*/ |
|
984 |
public PrintWriter format(String format, Object ... args) { |
|
985 |
try { |
|
986 |
synchronized (lock) { |
|
987 |
ensureOpen(); |
|
988 |
if ((formatter == null) |
|
989 |
|| (formatter.locale() != Locale.getDefault())) |
|
990 |
formatter = new Formatter(this); |
|
991 |
formatter.format(Locale.getDefault(), format, args); |
|
992 |
if (autoFlush) |
|
993 |
out.flush(); |
|
994 |
} |
|
995 |
} catch (InterruptedIOException x) { |
|
996 |
Thread.currentThread().interrupt(); |
|
997 |
} catch (IOException x) { |
|
998 |
trouble = true; |
|
999 |
} |
|
1000 |
return this; |
|
1001 |
} |
|
1002 |
||
1003 |
/** |
|
1004 |
* Writes a formatted string to this writer using the specified format |
|
1005 |
* string and arguments. If automatic flushing is enabled, calls to this |
|
1006 |
* method will flush the output buffer. |
|
1007 |
* |
|
1008 |
* @param l |
|
1009 |
* The {@linkplain java.util.Locale locale} to apply during |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1010 |
* formatting. If {@code l} is {@code null} then no localization |
2 | 1011 |
* is applied. |
1012 |
* |
|
1013 |
* @param format |
|
1014 |
* A format string as described in <a |
|
1015 |
* href="../util/Formatter.html#syntax">Format string syntax</a>. |
|
1016 |
* |
|
1017 |
* @param args |
|
1018 |
* Arguments referenced by the format specifiers in the format |
|
1019 |
* string. If there are more arguments than format specifiers, the |
|
1020 |
* extra arguments are ignored. The number of arguments is |
|
1021 |
* variable and may be zero. The maximum number of arguments is |
|
1022 |
* limited by the maximum dimension of a Java array as defined by |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8166
diff
changeset
|
1023 |
* <cite>The Java™ Virtual Machine Specification</cite>. |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8166
diff
changeset
|
1024 |
* The behaviour on a |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1025 |
* {@code null} argument depends on the <a |
2 | 1026 |
* href="../util/Formatter.html#syntax">conversion</a>. |
1027 |
* |
|
14014 | 1028 |
* @throws java.util.IllegalFormatException |
2 | 1029 |
* If a format string contains an illegal syntax, a format |
1030 |
* specifier that is incompatible with the given arguments, |
|
1031 |
* insufficient arguments given the format string, or other |
|
1032 |
* illegal conditions. For specification of all possible |
|
1033 |
* formatting errors, see the <a |
|
1034 |
* href="../util/Formatter.html#detail">Details</a> section of the |
|
1035 |
* formatter class specification. |
|
1036 |
* |
|
1037 |
* @throws NullPointerException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1038 |
* If the {@code format} is {@code null} |
2 | 1039 |
* |
1040 |
* @return This writer |
|
1041 |
* |
|
1042 |
* @since 1.5 |
|
1043 |
*/ |
|
1044 |
public PrintWriter format(Locale l, String format, Object ... args) { |
|
1045 |
try { |
|
1046 |
synchronized (lock) { |
|
1047 |
ensureOpen(); |
|
1048 |
if ((formatter == null) || (formatter.locale() != l)) |
|
1049 |
formatter = new Formatter(this, l); |
|
1050 |
formatter.format(l, format, args); |
|
1051 |
if (autoFlush) |
|
1052 |
out.flush(); |
|
1053 |
} |
|
1054 |
} catch (InterruptedIOException x) { |
|
1055 |
Thread.currentThread().interrupt(); |
|
1056 |
} catch (IOException x) { |
|
1057 |
trouble = true; |
|
1058 |
} |
|
1059 |
return this; |
|
1060 |
} |
|
1061 |
||
1062 |
/** |
|
1063 |
* Appends the specified character sequence to this writer. |
|
1064 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1065 |
* <p> An invocation of this method of the form {@code out.append(csq)} |
2 | 1066 |
* behaves in exactly the same way as the invocation |
1067 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1068 |
* <pre>{@code |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1069 |
* out.write(csq.toString()) |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1070 |
* }</pre> |
2 | 1071 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1072 |
* <p> Depending on the specification of {@code toString} for the |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1073 |
* character sequence {@code csq}, the entire sequence may not be |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1074 |
* appended. For instance, invoking the {@code toString} method of a |
2 | 1075 |
* character buffer will return a subsequence whose content depends upon |
1076 |
* the buffer's position and limit. |
|
1077 |
* |
|
1078 |
* @param csq |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1079 |
* The character sequence to append. If {@code csq} is |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1080 |
* {@code null}, then the four characters {@code "null"} are |
2 | 1081 |
* appended to this writer. |
1082 |
* |
|
1083 |
* @return This writer |
|
1084 |
* |
|
1085 |
* @since 1.5 |
|
1086 |
*/ |
|
1087 |
public PrintWriter append(CharSequence csq) { |
|
40410 | 1088 |
write(String.valueOf(csq)); |
2 | 1089 |
return this; |
1090 |
} |
|
1091 |
||
1092 |
/** |
|
1093 |
* Appends a subsequence of the specified character sequence to this writer. |
|
1094 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1095 |
* <p> An invocation of this method of the form |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1096 |
* {@code out.append(csq, start, end)} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1097 |
* when {@code csq} is not {@code null}, behaves in |
2 | 1098 |
* exactly the same way as the invocation |
1099 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1100 |
* <pre>{@code |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1101 |
* out.write(csq.subSequence(start, end).toString()) |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1102 |
* }</pre> |
2 | 1103 |
* |
1104 |
* @param csq |
|
1105 |
* The character sequence from which a subsequence will be |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1106 |
* appended. If {@code csq} is {@code null}, then characters |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1107 |
* will be appended as if {@code csq} contained the four |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1108 |
* characters {@code "null"}. |
2 | 1109 |
* |
1110 |
* @param start |
|
1111 |
* The index of the first character in the subsequence |
|
1112 |
* |
|
1113 |
* @param end |
|
1114 |
* The index of the character following the last character in the |
|
1115 |
* subsequence |
|
1116 |
* |
|
1117 |
* @return This writer |
|
1118 |
* |
|
1119 |
* @throws IndexOutOfBoundsException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1120 |
* If {@code start} or {@code end} are negative, {@code start} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1121 |
* is greater than {@code end}, or {@code end} is greater than |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1122 |
* {@code csq.length()} |
2 | 1123 |
* |
1124 |
* @since 1.5 |
|
1125 |
*/ |
|
1126 |
public PrintWriter append(CharSequence csq, int start, int end) { |
|
40410 | 1127 |
if (csq == null) csq = "null"; |
1128 |
return append(csq.subSequence(start, end)); |
|
2 | 1129 |
} |
1130 |
||
1131 |
/** |
|
1132 |
* Appends the specified character to this writer. |
|
1133 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1134 |
* <p> An invocation of this method of the form {@code out.append(c)} |
2 | 1135 |
* behaves in exactly the same way as the invocation |
1136 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1137 |
* <pre>{@code |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1138 |
* out.write(c) |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
28422
diff
changeset
|
1139 |
* }</pre> |
2 | 1140 |
* |
1141 |
* @param c |
|
1142 |
* The 16-bit character to append |
|
1143 |
* |
|
1144 |
* @return This writer |
|
1145 |
* |
|
1146 |
* @since 1.5 |
|
1147 |
*/ |
|
1148 |
public PrintWriter append(char c) { |
|
1149 |
write(c); |
|
1150 |
return this; |
|
1151 |
} |
|
1152 |
} |