1 7 8 package java.io; 9 10 import java.util.Formatter ; 11 import java.util.Locale ; 12 13 34 35 public class PrintWriter extends Writer { 36 37 43 protected Writer out; 44 45 private boolean autoFlush = false; 46 private boolean trouble = false; 47 private Formatter formatter; 48 49 53 private String lineSeparator; 54 55 60 public PrintWriter (Writer out) { 61 this(out, false); 62 } 63 64 72 public PrintWriter(Writer out, 73 boolean autoFlush) { 74 super(out); 75 this.out = out; 76 this.autoFlush = autoFlush; 77 lineSeparator = (String ) java.security.AccessController.doPrivileged( 78 new sun.security.action.GetPropertyAction("line.separator")); 79 } 80 81 91 public PrintWriter(OutputStream out) { 92 this(out, false); 93 } 94 95 108 public PrintWriter(OutputStream out, boolean autoFlush) { 109 this(new BufferedWriter (new OutputStreamWriter (out)), autoFlush); 110 } 111 112 139 public PrintWriter(String fileName) throws FileNotFoundException { 140 this(new BufferedWriter (new OutputStreamWriter (new FileOutputStream (fileName))), 141 false); 142 } 143 144 177 public PrintWriter(String fileName, String csn) 178 throws FileNotFoundException , UnsupportedEncodingException 179 { 180 this(new BufferedWriter (new OutputStreamWriter (new FileOutputStream (fileName), csn)), 181 false); 182 } 183 184 211 public PrintWriter(File file) throws FileNotFoundException { 212 this(new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file))), 213 false); 214 } 215 216 249 public PrintWriter(File file, String csn) 250 throws FileNotFoundException , UnsupportedEncodingException 251 { 252 this(new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file), csn)), 253 false); 254 } 255 256 257 private void ensureOpen() throws IOException { 258 if (out == null) 259 throw new IOException ("Stream closed"); 260 } 261 262 266 public void flush() { 267 try { 268 synchronized (lock) { 269 ensureOpen(); 270 out.flush(); 271 } 272 } 273 catch (IOException x) { 274 trouble = true; 275 } 276 } 277 278 282 public void close() { 283 try { 284 synchronized (lock) { 285 if (out == null) 286 return; 287 out.close(); 288 out = null; 289 } 290 } 291 catch (IOException x) { 292 trouble = true; 293 } 294 } 295 296 304 public boolean checkError() { 305 if (out != null) 306 flush(); 307 return trouble; 308 } 309 310 311 protected void setError() { 312 trouble = true; 313 try { 314 throw new IOException (); 315 } catch (IOException x) { 316 } 317 } 318 319 320 324 325 329 public void write(int c) { 330 try { 331 synchronized (lock) { 332 ensureOpen(); 333 out.write(c); 334 } 335 } 336 catch (InterruptedIOException x) { 337 Thread.currentThread().interrupt(); 338 } 339 catch (IOException x) { 340 trouble = true; 341 } 342 } 343 344 350 public void write(char buf[], int off, int len) { 351 try { 352 synchronized (lock) { 353 ensureOpen(); 354 out.write(buf, off, len); 355 } 356 } 357 catch (InterruptedIOException x) { 358 Thread.currentThread().interrupt(); 359 } 360 catch (IOException x) { 361 trouble = true; 362 } 363 } 364 365 370 public void write(char buf[]) { 371 write(buf, 0, buf.length); 372 } 373 374 380 public void write(String s, int off, int len) { 381 try { 382 synchronized (lock) { 383 ensureOpen(); 384 out.write(s, off, len); 385 } 386 } 387 catch (InterruptedIOException x) { 388 Thread.currentThread().interrupt(); 389 } 390 catch (IOException x) { 391 trouble = true; 392 } 393 } 394 395 400 public void write(String s) { 401 write(s, 0, s.length()); 402 } 403 404 private void newLine() { 405 try { 406 synchronized (lock) { 407 ensureOpen(); 408 out.write(lineSeparator); 409 if (autoFlush) 410 out.flush(); 411 } 412 } 413 catch (InterruptedIOException x) { 414 Thread.currentThread().interrupt(); 415 } 416 catch (IOException x) { 417 trouble = true; 418 } 419 } 420 421 422 423 424 433 public void print(boolean b) { 434 write(b ? "true" : "false"); 435 } 436 437 445 public void print(char c) { 446 write(c); 447 } 448 449 459 public void print(int i) { 460 write(String.valueOf(i)); 461 } 462 463 473 public void print(long l) { 474 write(String.valueOf(l)); 475 } 476 477 487 public void print(float f) { 488 write(String.valueOf(f)); 489 } 490 491 501 public void print(double d) { 502 write(String.valueOf(d)); 503 } 504 505 515 public void print(char s[]) { 516 write(s); 517 } 518 519 528 public void print(String s) { 529 if (s == null) { 530 s = "null"; 531 } 532 write(s); 533 } 534 535 545 public void print(Object obj) { 546 write(String.valueOf(obj)); 547 } 548 549 550 551 552 558 public void println() { 559 newLine(); 560 } 561 562 569 public void println(boolean x) { 570 synchronized (lock) { 571 print(x); 572 println(); 573 } 574 } 575 576 583 public void println(char x) { 584 synchronized (lock) { 585 print(x); 586 println(); 587 } 588 } 589 590 597 public void println(int x) { 598 synchronized (lock) { 599 print(x); 600 println(); 601 } 602 } 603 604 611 public void println(long x) { 612 synchronized (lock) { 613 print(x); 614 println(); 615 } 616 } 617 618 625 public void println(float x) { 626 synchronized (lock) { 627 print(x); 628 println(); 629 } 630 } 631 632 639 public void println(double x) { 640 synchronized (lock) { 641 print(x); 642 println(); 643 } 644 } 645 646 653 public void println(char x[]) { 654 synchronized (lock) { 655 print(x); 656 println(); 657 } 658 } 659 660 667 public void println(String x) { 668 synchronized (lock) { 669 print(x); 670 println(); 671 } 672 } 673 674 681 public void println(Object x) { 682 synchronized (lock) { 683 print(x); 684 println(); 685 } 686 } 687 688 730 public PrintWriter printf(String format, Object ... args) { 731 return format(format, args); 732 } 733 734 781 public PrintWriter printf(Locale l, String format, Object ... args) { 782 return format(l, format, args); 783 } 784 785 825 public PrintWriter format(String format, Object ... args) { 826 try { 827 synchronized (lock) { 828 ensureOpen(); 829 if ((formatter == null) 830 || (formatter.locale() != Locale.getDefault())) 831 formatter = new Formatter (this); 832 formatter.format(Locale.getDefault(), format, args); 833 if (autoFlush) 834 out.flush(); 835 } 836 } catch (InterruptedIOException x) { 837 Thread.currentThread().interrupt(); 838 } catch (IOException x) { 839 trouble = true; 840 } 841 return this; 842 } 843 844 885 public PrintWriter format(Locale l, String format, Object ... args) { 886 try { 887 synchronized (lock) { 888 ensureOpen(); 889 if ((formatter == null) || (formatter.locale() != l)) 890 formatter = new Formatter (this, l); 891 formatter.format(l, format, args); 892 if (autoFlush) 893 out.flush(); 894 } 895 } catch (InterruptedIOException x) { 896 Thread.currentThread().interrupt(); 897 } catch (IOException x) { 898 trouble = true; 899 } 900 return this; 901 } 902 903 927 public PrintWriter append(CharSequence csq) { 928 if (csq == null) 929 write("null"); 930 else 931 write(csq.toString()); 932 return this; 933 } 934 935 967 public PrintWriter append(CharSequence csq, int start, int end) { 968 CharSequence cs = (csq == null ? "null" : csq); 969 write(cs.subSequence(start, end).toString()); 970 return this; 971 } 972 973 989 public PrintWriter append(char c) { 990 write(c); 991 return this; 992 } 993 } 994 | Popular Tags |