View Javadoc
1 /* ------------------------------------------------------------------- 2 * Java source file for the class FileIO 3 * 4 * Copyright (c), 2002, Masahiro Takatsuka. 5 * All Rights Researved. 6 * 7 * Original Author: Masahiro Takatsuka (masa@jbeans.net) 8 * $Author: takatsukam $ 9 * 10 * $Date: 2003/07/25 04:51:45 $ 11 * 12 * $Id: FileIO.java,v 1.1.1.1 2003/07/25 04:51:45 takatsukam Exp $ 13 * 14 * Reference: Document no: 15 * ___ ___ 16 * 17 * To Do: 18 * ___ 19 * 20 ------------------------------------------------------------------- */ 21 22 /* --------------------------- Package ---------------------------- */ 23 package net.jbeans.io; 24 25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */ 26 import java.io.*; 27 import java.util.zip.*; 28 import java.util.*; 29 30 /*==================================================================== 31 Implementation of class FileIO 32 ====================================================================*/ 33 /*** 34 * routines for reading and writing files. Features include 35 * transparent use of compression and decompression, stdin/stdout. 36 * 37 * @version $Revision: 1.1.1.1 $ 38 * @author Masahiro Takatsuka (masa@jbeans.net) 39 */ 40 41 public final class FileIO { 42 /* ----------------- constants used for file I/O ------------------ */ 43 public static final int NO_ZIP = 0;// no compression 44 public static final int ZIP = 1; // zip compression 45 public static final int GZIP = 2; // gnuzip compression 46 47 /* ---------------- name for standard in, out, err ---------------- */ 48 public static final String STDIN_NAME = "stdin"; 49 public static final String STDOUT_NAME = "stdout"; 50 public static final String STDERR_NAME = "stderr"; 51 52 /* -------------------------- file mode --------------------------- */ 53 private static final int UNKNOWN_MODE = 0x00;// unknown mode 54 private static final int READ_MODE = 0x01;// read mode 55 private static final int WRITE_MODE = 0x02; // write mode 56 57 /* -------------------------- error code -------------------------- */ 58 public static final int OK = 0;// no error 59 public static final int UNKNOWN = 1; // unknown error 60 public static final int NO_MEMORY = 2; // can't allocate memory 61 public static final int FILEMODE = 3; // incorrect file mode 62 public static final int NO_PIPES = 4; // operations on pipes are not supported (on those systems that don't have popen() 63 public static final int OPENFILE = 5; // can't open file. look at errno for more info 64 public static final int COMMAND = 6; // can't execute command 65 public static final int REWINDFILE = 7; // can't rewind file 66 public static final int REWINDPIPE = 8; // can't rewind regular pipe 67 public static final int LINETOOLONG = 9; // input line too long 68 public static final int FILEERR = 10; // file error, see erno for more info 69 public static final int HEADER = 11; // error in file headers 70 public static final int FILEFORMAT = 12; // error in data file 71 public static final int EOF = 13; // reached the end-of-file 72 73 /* ---------------------- instance variables ---------------------- */ 74 private String originalname;// original name of file (full path) 75 private String originalmode;// original mode string 76 private String name; // name of file (full path) 77 private int mode; // file mode 78 private JBeansReader ir; // reference to the reader 79 private PrintWriter pw; // reference to the writer 80 private int compressed; // the type of file compression 81 private boolean pipe = false; // the file is a pipe 82 private boolean eof = false; // has EOF been reached 83 private int error; // error code or Error.OK (see Error class) 84 private int lineno; // current line number 85 86 /*** 87 * returns a input reader associated with this file I/O. 88 * 89 * @return InputStreamReader 90 * @see #getWriter() 91 */ 92 public final JBeansReader getReader() { 93 return this.ir; 94 } 95 96 /*** 97 * returns a writer associated with this file I/O. 98 * 99 * @return PrintWriter 100 * @see #getReader() 101 */ 102 public final PrintWriter getWriter() { 103 return this.pw; 104 } 105 106 /*** 107 * Returns the file mode. 108 * 109 * @return int 110 */ 111 public final int getMode() { 112 return this.mode; 113 } 114 115 /*** 116 * Returns the original file name. 117 * 118 * @return String 119 */ 120 public final String getOriginalFilename() { 121 return this.originalname; 122 } 123 124 /*** 125 * Returns the original mode string. 126 * 127 * @return String 128 */ 129 public final String getOriginalMode() { 130 return this.originalmode; 131 } 132 133 /*** 134 * Returns true if the file has reached EOF. 135 * 136 * @return boolean 137 */ 138 public final boolean hasReachedEOF() { 139 return this.eof; 140 } 141 142 /*** 143 * Returns true if the file is piped. 144 * 145 * @return boolean 146 */ 147 public final boolean getPipeFlag() { 148 return this.pipe; 149 } 150 151 /*** 152 * Returns current line number. 153 * 154 * @return int 155 */ 156 public final int getLineNo() { 157 return this.lineno; 158 } 159 160 /*** 161 * Opens a file for reading or writing. If name is 'null', standard 162 * input (System.in) or standard output (System.out) will be used. 163 * There are two basic mode "r" for reading and "w" for writing. The mode 164 * can have optional characters, which are "z" for "ZIP" compression 165 * and "g" for "GZIP" compression. The pipe is not supported yet. 166 * If optional characters are not specified, they will be guessed from the 167 * file name. The suffix ".zip" implies "ZIP" mode and ".gz", ".z" or ".Z" 168 * imply "GZIP" mode. 169 * 170 * <PRE> 171 * try { 172 * FileIO fio0 = new FileIO("foo0", "r"); 173 * FileIO fio1 = new FileIO("foo1.gz", "r"); 174 * FileIO fio2 = new FileIO("foo2.zip", "rz"); 175 * FileIO fio3 = new FileIO("foo3.gz", "rg"); 176 * ... 177 * fio0.close(); 178 * fio1.close(); 179 * fio2.close(); 180 * fio3.close(); 181 * } catch (InvalidFileModeException ifme) { 182 * System.err.println(ifme); 183 * } catch (FileIOException fioe) { 184 * System.err.println(fioe); 185 * } catch (IOException ioe) { 186 * System.err.println(ioe); 187 * } finally { 188 * } 189 * </PRE> 190 * 191 * @param name The name of the filename to be opened. 192 * @param mode The file mode. 193 * @return A newly created FileIO instance. 194 * @exception (full-classname) (description) 195 * @see #close() 196 */ 197 public FileIO(String filename, String filemode) throws InvalidFileModeException, FileIOException, IOException { 198 this.ir = null; 199 this.pw = null; 200 this.compressed = NO_ZIP; 201 this.pipe = false; 202 this.eof = false; 203 this.lineno = 0; 204 this.error = OK; 205 this.originalname = (filename != null)?filename.intern():null; 206 this.originalmode = (filemode != null)?filemode.intern():null; 207 /* ----------------------- local variables ------------------------ */ 208 this.mode = UNKNOWN_MODE;// READ_MODE = read, WRITE_MODE = write 209 StringBuffer buf = new StringBuffer(); // used to construct messages. 210 int idx; // used to hold index No. of char in a String. 211 212 if(filemode == null){ 213 buf.append("FileIO: incorrect file mode: ").append(filemode).append(" for file ").append(filename); 214 throw new InvalidFileModeException(buf.toString()); 215 } 216 217 /* ---------------------- analyse file mode ----------------------- */ 218 if (filemode.indexOf('r')>=0) this.mode = READ_MODE; 219 else if (filemode.indexOf('w')>=0) this.mode = WRITE_MODE; 220 else { // one of the above must be used 221 buf.append("FileIO: incorrect file mode: ").append(filemode).append(" for file ").append(filename); 222 throw new InvalidFileModeException(buf.toString()); 223 } 224 225 // compressed file? 226 if ((idx = filemode.indexOf('z'))>=0 || 227 (idx = filemode.indexOf('g'))>=0) { 228 this.compressed = ZIP; 229 this.pipe = false; 230 buf.setLength(0); 231 filemode = buf.append(filemode.substring(0, idx)).append(filemode.substring(idx+1)).toString(); // remove z from mode 232 } 233 234 // piped? 235 if ((idx = filemode.indexOf('p'))>=0) { 236 this.pipe = true; 237 this.compressed = NO_ZIP; // no compression with a pipe. 238 buf.setLength(0); 239 filemode = buf.append(filemode.substring(0, idx)).append(filemode.substring(idx+1)).toString(); // remove p from mode 240 } 241 242 /* -------- if filename starts with '|' use piped command --------- */ 243 if (filename!=null && (filename.charAt(0) == '|')) { 244 this.pipe = true; 245 this.compressed = NO_ZIP; // no compression with a pipe. 246 filename = filename.substring(1);// remove '|' 247 } 248 249 if (filename!=null){ 250 filename.trim(); // to remove white space from both ends. 251 if (!this.pipe) // this is faster than if(this.pipe==false) 252 this.compressed = checkCompressionType(filename); 253 254 if (filename.compareTo("-") == 0) // "-" means use stdin/out */ 255 filename = null; 256 } 257 258 if (filename == null) { // use stdin/stdout if filename is null 259 if(this.mode==READ_MODE){ 260 this.ir = new JBeansReader(System.in); 261 this.pw = null; 262 } else { 263 this.ir = null; 264 this.pw = new PrintWriter(System.out); 265 } 266 /* assume that stdin/out is a regular file or that we don't need to 267 rewind it */ 268 filename = (this.mode == READ_MODE) ? STDIN_NAME : STDOUT_NAME; 269 } else if (this.compressed>NO_ZIP) { //compressed files 270 if (this.mode == READ_MODE) { // unzip or ungzip 271 switch(this.compressed) { 272 case ZIP: 273 ZipFile zf = new ZipFile(filename); 274 Enumeration enum = zf.entries(); 275 ZipEntry target = (ZipEntry)enum.nextElement(); 276 this.ir = new JBeansReader(zf.getInputStream(target)); 277 break; 278 case GZIP: 279 GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(filename)); 280 this.ir = new JBeansReader(gzis); 281 break; 282 } 283 this.pw = null; 284 } else { // zip or gizp 285 File zf = new File(filename); 286 switch(this.compressed) { 287 case ZIP: 288 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zf)); 289 String fullpath = zf.getPath(); 290 String path = fullpath.substring(0, fullpath.lastIndexOf(".")); 291 ZipEntry target = new ZipEntry(path); 292 zos.putNextEntry(target); 293 this.ir = null; 294 this.pw = new PrintWriter(zos, true); 295 break; 296 case GZIP: 297 GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream(zf)); 298 this.ir = null; 299 this.pw = new PrintWriter(gos, true); 300 break; 301 } 302 } 303 } else if (this.pipe) { // piped commands 304 // strcpy(buf, name); 305 // fp = popen(buf, (_mode == FM_READ) ? "r" : "w" ); 306 // if (fp == null) { 307 // fprintf(stderr, "open_file: can't execute command '%s'\n", 308 // buf); 309 // perror("open_file"); 310 // } 311 // fi.flags._pipe = 1; 312 } else { 313 if (this.mode == READ_MODE) { 314 this.ir = new JBeansReader(new FileInputStream(filename)); 315 this.pw = null; 316 } else { 317 this.ir = null; 318 this.pw = new PrintWriter(new FileOutputStream(filename), true); 319 } 320 } 321 322 this.name = filename.intern(); 323 } 324 325 /*** 326 * Closes FileIO instance. 327 * 328 * @see #FileIO(java.lang.String, java.lang.String) 329 */ 330 public final void close() throws IOException { 331 if (this.name!=null) 332 this.name = null; 333 if (this.ir != null) 334 this.ir.close(); 335 if (this.pw != null) { 336 this.pw.flush(); // make sure everything is written. 337 this.pw.close(); 338 } 339 // if pipe is used, close pipe. 340 } 341 342 343 /*** 344 * Returns a filename associated with this FileIO object. 345 * 346 * @return a filename 347 */ 348 public final String getFilename() { 349 return this.name; 350 } 351 352 /*** 353 * check_for_compression - check if name indicates compression, 354 * i.e. the ending is one of .gz, .z, .Z. If suffix is found, returns 355 * an index of the dot, otherwise returns -1 356 * 357 * @param name The name of file. 358 * @return integer value indicating the type of compression 359 * (NO_ZIP : no compression, ZIP : zip, GZIP : gnuzip 360 */ 361 private int checkCompressionType(String name) { 362 String s; 363 int idx = -1; 364 365 if (name == null) 366 return NO_ZIP; 367 368 // look for the last '.' in name 369 if ((idx = name.lastIndexOf('.'))<0)// no suffix 370 return NO_ZIP; 371 372 s = name.substring(idx); 373 if (s.compareTo(".gz") == 0 || // compressed with gzip 374 s.compareTo(".z") == 0 || // compressed with gzip (older version) 375 s.compareTo(".Z") == 0) // compressed with compress 376 return GZIP; 377 else if (s.compareTo(".zip") == 0) // compressed with zip 378 return ZIP; 379 380 /* unknown suffix */ 381 return NO_ZIP; 382 } 383 384 385 /*** 386 * Reads a line from file. Returns a String object containing a line, 387 * null on error. 388 * 389 * @return String containing a line. 390 */ 391 public final String getLine() throws EOFException, IOException { 392 String tstr = null; 393 394 this.error = 0; 395 this.eof = false; 396 /* increment file line number */ 397 this.lineno += 1; 398 try { 399 tstr = this.ir.readLine(); 400 if (tstr == null) { 401 this.eof = true; 402 throw new EOFException(); 403 } 404 } catch (EOFException eofe) { 405 this.eof = true; 406 throw eofe; 407 } catch (IOException ioe) { 408 this.error = UNKNOWN; 409 throw ioe; 410 } 411 412 return tstr; 413 } 414 415 416 /* ---------------- wrapper for JBeansReader ---------------- */ 417 /*** 418 * Read a single character. 419 * 420 * @return The character read, or -1 if the end of the stream has been 421 * reached 422 * 423 * @exception IOException If an I/O error occurs 424 */ 425 public final int read() throws IOException { 426 if (this.ir==null) 427 throw new IOException("FileIO: _ir == null"); 428 return this.ir.read(); 429 } 430 431 /*** 432 * Read characters into a portion of an array. 433 * 434 * @param cbuf Destination buffer 435 * @param off Offset at which to start storing characters 436 * @param len Maximum number of characters to read 437 * 438 * @return The number of characters read, or -1 if the end of the stream 439 * has been reached 440 * 441 * @exception IOException If an I/O error occurs 442 */ 443 public final int read(char cbuf[], int off, int len) throws IOException { 444 if (this.ir==null) 445 throw new IOException("FileIO: _ir == null"); 446 return this.ir.read(cbuf, off, len); 447 } 448 449 /*** 450 * Read a line of text. A line is considered to be terminated by any one 451 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return 452 * followed immediately by a linefeed. 453 * 454 * @return A String containing the contents of the line, not including 455 * any line-termination characters, or null if the end of the 456 * stream has been reached 457 * 458 * @exception IOException If an I/O error occurs 459 */ 460 public final String readLine() throws IOException { 461 if (this.ir==null) 462 throw new IOException("FileIO: _ir == null"); 463 return this.ir.readLine(); 464 } 465 466 /*** 467 * Read a line of text. A line is considered to be terminated by any one 468 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return 469 * followed immediately by a linefeed. 470 * 471 * @return A String containing the contents of the line, not including 472 * any line-termination characters, or null if the end of the 473 * stream has been reached 474 * 475 * @exception IOException If an I/O error occurs 476 */ 477 public final String readLine(String commentchars) throws IOException { 478 if (this.ir==null) 479 throw new IOException("FileIO: _ir == null"); 480 return this.ir.readLine(commentchars); 481 } 482 483 /*** 484 * Read a line of text. A line is considered to be terminated by any one 485 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return 486 * followed immediately by a linefeed. 487 * 488 * @return A String containing the contents of the line, not including 489 * any line-termination characters, or null if the end of the 490 * stream has been reached 491 * 492 * @exception IOException If an I/O error occurs 493 */ 494 public final String readLine(char commentchar) throws IOException { 495 if (this.ir == null) 496 throw new IOException("FileIO: _ir == null"); 497 return this.ir.readLine(commentchar); 498 } 499 500 /*** 501 * Read one token as a string. 502 * 503 * @return A String containing the string, or null if the end of the 504 * stream has been reached. 505 * 506 * @exception IOException If an I/O error occurs 507 */ 508 public final String readToken() throws IOException { 509 if (this.ir==null) 510 throw new IOException("FileIO: _ir == null"); 511 return this.ir.readToken(); 512 } 513 514 /*** 515 * Reads a <code>boolean</code> from this data input stream. This 516 * method reads a single byte from the underlying input stream. A 517 * value of <code>0</code> represents <code>false</code>. Any other 518 * value represents <code>true</code>. This method blocks until 519 * either the byte is read, the end of the stream is detected, or an 520 * exception is thrown. 521 * 522 * @return the <code>boolean</code> value read. 523 * @exception EOFException if this input stream has reached the end. 524 * @exception IOException if an I/O error occurs. 525 */ 526 public final boolean readBoolean() throws IOException { 527 if (this.ir==null) 528 throw new IOException("FileIO: _ir == null"); 529 return this.ir.readBoolean(); 530 } 531 532 /*** 533 * Reads a signed 8-bit value from this data input stream. This 534 * method reads a byte from the underlying input stream. If the byte 535 * read is <code>b</code>, where 536 * 0 <= <code>b</code> <= 255, then the 537 * result is: 538 * <ul><code> 539 * (byte)(b) 540 * </code></ul> 541 * <p> 542 * This method blocks until either the byte is read, the end of the 543 * stream is detected, or an exception is thrown. 544 * 545 * @return the next byte of this input stream as a signed 8-bit 546 * <code>byte</code>. 547 * @exception EOFException if this input stream has reached the end. 548 * @exception IOException if an I/O error occurs. 549 */ 550 public final byte readByte() throws IOException { 551 if (this.ir==null) 552 throw new IOException("FileIO: _ir == null"); 553 return this.ir.readByte(); 554 } 555 556 /*** 557 * Reads a signed 16-bit number from this data input stream. The 558 * method reads two bytes from the underlying input stream. If the two 559 * bytes read, in order, are <code>b1</code> and <code>b2</code>, 560 * where each of the two values is between <code>0</code> and 561 * <code>255</code>, inclusive, then the result is equal to: 562 * <ul><code> 563 * (short)((b1 << 8) | b2) 564 * </code></ul> 565 * <p> 566 * This method blocks until the two bytes are read, the end of the 567 * stream is detected, or an exception is thrown. 568 * 569 * @return the next two bytes of this input stream, interpreted as a 570 * signed 16-bit number. 571 * @exception EOFException if this input stream reaches the end before 572 * reading two bytes. 573 * @exception IOException if an I/O error occurs. 574 */ 575 public final short readShort() throws IOException { 576 if (this.ir==null) 577 throw new IOException("FileIO: _ir == null"); 578 return this.ir.readShort(); 579 } 580 581 /*** 582 * Reads a Unicode character from this data input stream. This 583 * method reads two bytes from the underlying input stream. If the 584 * bytes read, in order, are <code>b1</code> and <code>b2</code>, 585 * where 0 <= <code>b1</code>, 586 * <code>b1</code> <= 255, then the result is equal to: 587 * <ul><code> 588 * (char)((b1 << 8) | b2) 589 * </code></ul> 590 * <p> 591 * This method blocks until either the two bytes are read, the end of 592 * the stream is detected, or an exception is thrown. 593 * 594 * @return the next two bytes of this input stream as a Unicode 595 * character. 596 * @exception EOFException if this input stream reaches the end before 597 * reading two bytes. 598 * @exception IOException if an I/O error occurs. 599 */ 600 public final char readChar() throws IOException { 601 if (this.ir==null) 602 throw new IOException("FileIO: _ir == null"); 603 return this.ir.readChar(); 604 } 605 606 /*** 607 * Reads a signed 32-bit integer from this data input stream. This 608 * method reads four bytes from the underlying input stream. If the 609 * bytes read, in order, are <code>b1</code>, <code>b2</code>, 610 * <code>b3</code>, and <code>b4</code>, where 611 * 0 <= <code>b1</code>, <code>b2</code>, 612 * <code>b3</code>, <code>b4</code> <= 255, then the 613 * result is equal to: 614 * <ul><code> 615 * (b1 << 24) | (b2 << 16) + (b3 << 8) +b4 616 * </code></ul> 617 * <p> 618 * This method blocks until the four bytes are read, the end of the 619 * stream is detected, or an exception is thrown. 620 * 621 * @return the next four bytes of this input stream, interpreted as an 622 * <code>int</code>. 623 * @exception EOFException if this input stream reaches the end before 624 * reading four bytes. 625 * @exception IOException if an I/O error occurs. 626 */ 627 public final int readInt() throws IOException { 628 if (this.ir==null) 629 throw new IOException("FileIO: _ir == null"); 630 return this.ir.readInt(); 631 } 632 633 /*** 634 * Reads a signed 64-bit integer from this data input stream. This 635 * method reads eight bytes from the underlying input stream. If the 636 * bytes read, in order, are <code>b1</code>, <code>b2</code>, 637 * <code>b3</code>, <code>b4</code>, <code>b5</code>, 638 * <code>b6</code>, <code>b7</code>, and <code>b8</code>, where 639 * <ul><code> 640 * 0 <= b1, b2, b3, b4, b5, b6, b7, b8 <= 255, 641 * </code></ul> 642 * <p> 643 * then the result is equal to: 644 * <p><blockquote><pre> 645 * ((long)b1 << 56) + ((long)b2 << 48) + 646 * ((long)b3 << 40) + ((long)b4 << 32) + 647 * ((long)b5 << 24) + (b6 << 16) + 648 * (b7 << 8) + b8 649 * </pre></blockquote> 650 * <p> 651 * This method blocks until the eight bytes are read, the end of the 652 * stream is detected, or an exception is thrown. 653 * 654 * @return the next eight bytes of this input stream, interpreted as a 655 * <code>long</code>. 656 * @exception EOFException if this input stream reaches the end before 657 * reading eight bytes. 658 * @exception IOException if an I/O error occurs. 659 */ 660 public final long readLong() throws IOException { 661 if (this.ir==null) 662 throw new IOException("FileIO: _ir == null"); 663 return this.ir.readLong(); 664 } 665 666 /*** 667 * Reads a <code>float</code> from this data input stream. This 668 * method reads an <code>int</code> value as if by the 669 * <code>readInt</code> method and then converts that 670 * <code>int</code> to a <code>float</code> using the 671 * <code>intBitsToFloat</code> method in class <code>Float</code>. 672 * This method blocks until the four bytes are read, the end of the 673 * stream is detected, or an exception is thrown. 674 * 675 * @return the next four bytes of this input stream, interpreted as a 676 * <code>float</code>. 677 * @exception EOFException if this input stream reaches the end before 678 * reading four bytes. 679 * @exception IOException if an I/O error occurs. 680 */ 681 public final float readFloat() throws IOException { 682 if (this.ir==null) 683 throw new IOException("FileIO: _ir == null"); 684 return this.ir.readFloat(); 685 } 686 687 /*** 688 * Reads a <code>double</code> from this data input stream. This 689 * method reads a <code>long</code> value as if by the 690 * <code>readLong</code> method and then converts that 691 * <code>long</code> to a <code>double</code> using the 692 * <code>longBitsToDouble</code> method in class <code>Double</code>. 693 * <p> 694 * This method blocks until the eight bytes are read, the end of the 695 * stream is detected, or an exception is thrown. 696 * 697 * @return the next eight bytes of this input stream, interpreted as a 698 * <code>double</code>. 699 * @exception EOFException if this input stream reaches the end before 700 * reading eight bytes. 701 * @exception IOException if an I/O error occurs. 702 */ 703 public final double readDouble() throws IOException { 704 if (this.ir==null) 705 throw new IOException("FileIO: _ir == null"); 706 return this.ir.readDouble(); 707 } 708 709 /*** Flush the stream. */ 710 public final void flush() { 711 if (this.pw!=null) 712 this.pw.flush(); 713 } 714 715 /*** 716 * Flush the stream and check its error state. Errors are cumulative; 717 * once the stream encounters an error, this routine will return true on 718 * all successive calls. 719 * 720 * @return True if the print stream has encountered an error, either on the 721 * underlying output stream or during a format conversion. 722 */ 723 public final boolean checkError() { 724 if (this.pw!=null) 725 return this.pw.checkError(); 726 return true; 727 } 728 729 /*** Write a single character. */ 730 public final void write(int c) { 731 if (this.pw!=null) 732 this.pw.write(c); 733 } 734 735 /*** Write a portion of an array of characters. */ 736 public final void write(char buf[], int off, int len) { 737 if (this.pw!=null) 738 this.pw.write(buf, off, len); 739 } 740 741 /*** 742 * Write an array of characters. This method cannot be inherited from the 743 * Writer class because it must suppress I/O exceptions. 744 */ 745 public final void write(char buf[]) { 746 if (this.pw!=null) 747 this.pw.write(buf); 748 } 749 750 /*** Write a portion of a string. */ 751 public final void write(String s, int off, int len) { 752 if (this.pw!=null) 753 this.pw.write(s, off, len); 754 } 755 756 /*** 757 * Write a string. This method cannot be inherited from the Writer class 758 * because it must suppress I/O exceptions. 759 */ 760 public final void write(String s) { 761 if (this.pw!=null) 762 this.pw.write(s); 763 } 764 765 /* Methods that do not terminate lines */ 766 767 /*** 768 * Print a boolean value. The string produced by <code>{@link 769 * java.lang.String#valueOf(boolean)}</code> is translated into bytes 770 * according to the platform's default character encoding, and these bytes 771 * are written in exactly the manner of the <code>{@link 772 * #write(int)}</code> method. 773 * 774 * @param b The <code>boolean</code> to be printed 775 */ 776 public final void print(boolean b) { 777 if (this.pw!=null) 778 this.pw.print(b); 779 } 780 781 /*** 782 * Print a character. The character is translated into one or more bytes 783 * according to the platform's default character encoding, and these bytes 784 * are written in exactly the manner of the <code>{@link 785 * #write(int)}</code> method. 786 * 787 * @param c The <code>char</code> to be printed 788 */ 789 public final void print(char c) { 790 if (this.pw!=null) 791 this.pw.print(c); 792 } 793 794 /*** 795 * Print an integer. The string produced by <code>{@link 796 * java.lang.String#valueOf(int)}</code> is translated into bytes according 797 * to the platform's default character encoding, and these bytes are 798 * written in exactly the manner of the <code>{@link #write(int)}</code> 799 * method. 800 * 801 * @param i The <code>int</code> to be printed 802 * @see java.lang.Integer#toString(int) 803 */ 804 public final void print(int i) { 805 if (this.pw!=null) 806 this.pw.print(i); 807 } 808 809 /*** 810 * Print a long integer. The string produced by <code>{@link 811 * java.lang.String#valueOf(long)}</code> is translated into bytes 812 * according to the platform's default character encoding, and these bytes 813 * are written in exactly the manner of the <code>{@link #write(int)}</code> 814 * method. 815 * 816 * @param l The <code>long</code> to be printed 817 * @see java.lang.Long#toString(long) 818 */ 819 public final void print(long l) { 820 if (this.pw!=null) 821 this.pw.print(l); 822 } 823 824 /*** 825 * Print a floating-point number. The string produced by <code>{@link 826 * java.lang.String#valueOf(float)}</code> is translated into bytes 827 * according to the platform's default character encoding, and these bytes 828 * are written in exactly the manner of the <code>{@link #write(int)}</code> 829 * method. 830 * 831 * @param f The <code>float</code> to be printed 832 * @see java.lang.Float#toString(float) 833 */ 834 public final void print(float f) { 835 if (this.pw!=null) 836 this.pw.print(f); 837 } 838 839 /*** 840 * Print a double-precision floating-point number. The string produced by 841 * <code>{@link java.lang.String#valueOf(double)}</code> is translated into 842 * bytes according to the platform's default character encoding, and these 843 * bytes are written in exactly the manner of the <code>{@link 844 * #write(int)}</code> method. 845 * 846 * @param d The <code>double</code> to be printed 847 * @see java.lang.Double#toString(double) 848 */ 849 public final void print(double d) { 850 if (this.pw!=null) 851 this.pw.print(d); 852 } 853 854 /*** 855 * Print an array of characters. The characters are converted into bytes 856 * according to the platform's default character encoding, and these bytes 857 * are written in exactly the manner of the <code>{@link #write(int)}</code> 858 * method. 859 * 860 * @param s The array of chars to be printed 861 * 862 * @throws NullPointerException If <code>s</code> is <code>null</code> 863 */ 864 public final void print(char s[]) { 865 if (this.pw!=null) 866 this.pw.print(s); 867 } 868 869 /*** 870 * Print a string. If the argument is <code>null</code> then the string 871 * <code>"null"</code> is printed. Otherwise, the string's characters are 872 * converted into bytes according to the platform's default character 873 * encoding, and these bytes are written in exactly the manner of the 874 * <code>{@link #write(int)}</code> method. 875 * 876 * @param s The <code>String</code> to be printed 877 */ 878 public final void print(String s) { 879 if (this.pw!=null) 880 this.pw.print(s); 881 } 882 883 /*** 884 * Print an object. The string produced by the <code>{@link 885 * java.lang.String#valueOf(Object)}</code> method is translated into bytes 886 * according to the platform's default character encoding, and these bytes 887 * are written in exactly the manner of the <code>{@link #write(int)}</code> 888 * method. 889 * 890 * @param obj The <code>Object</code> to be printed 891 * @see java.lang.Object#toString() 892 */ 893 public final void print(Object obj) { 894 if (this.pw!=null) 895 this.pw.print(obj); 896 } 897 898 /* Methods that do terminate lines */ 899 900 /*** 901 * Terminate the current line by writing the line separator string. The 902 * line separator string is defined by the system property 903 * <code>line.separator</code>, and is not necessarily a single newline 904 * character (<code>'\n'</code>). 905 */ 906 public final void println() { 907 if (this.pw!=null) 908 this.pw.println(); 909 } 910 911 /*** 912 * Print a boolean value and then terminate the line. This method behaves 913 * as though it invokes <code>{@link #print(boolean)}</code> and then 914 * <code>{@link #println()}</code>. 915 */ 916 public final void println(boolean x) { 917 if (this.pw!=null) 918 this.pw.println(x); 919 } 920 921 /*** 922 * Print a character and then terminate the line. This method behaves as 923 * though it invokes <code>{@link #print(char)}</code> and then <code>{@link 924 * #println()}</code>. 925 */ 926 public final void println(char x) { 927 if (this.pw!=null) 928 this.pw.println(x); 929 } 930 931 /*** 932 * Print an integer and then terminate the line. This method behaves as 933 * though it invokes <code>{@link #print(int)}</code> and then <code>{@link 934 * #println()}</code>. 935 */ 936 public final void println(int x) { 937 if (this.pw!=null) 938 this.pw.println(x); 939 } 940 941 /*** 942 * Print a long integer and then terminate the line. This method behaves 943 * as though it invokes <code>{@link #print(long)}</code> and then 944 * <code>{@link #println()}</code>. 945 */ 946 public final void println(long x) { 947 if (this.pw!=null) 948 this.pw.println(x); 949 } 950 951 /*** 952 * Print a floating-point number and then terminate the line. This method 953 * behaves as though it invokes <code>{@link #print(float)}</code> and then 954 * <code>{@link #println()}</code>. 955 */ 956 public final void println(float x) { 957 if (this.pw!=null) 958 this.pw.println(x); 959 } 960 961 /*** 962 * Print a double-precision floating-point number and then terminate the 963 * line. This method behaves as though it invokes <code>{@link 964 * #print(double)}</code> and then <code>{@link #println()}</code>. 965 */ 966 public final void println(double x) { 967 if (this.pw!=null) 968 this.pw.println(x); 969 } 970 971 /*** 972 * Print an array of characters and then terminate the line. This method 973 * behaves as though it invokes <code>{@link #print(char[])}</code> and then 974 * <code>{@link #println()}</code>. 975 */ 976 public final void println(char x[]) { 977 if (this.pw!=null) 978 this.pw.println(x); 979 } 980 981 /*** 982 * Print a String and then terminate the line. This method behaves as 983 * though it invokes <code>{@link #print(String)}</code> and then 984 * <code>{@link #println()}</code>. 985 */ 986 public final void println(String x) { 987 if (this.pw!=null) 988 this.pw.println(x); 989 } 990 991 /*** 992 * Print an Object and then terminate the line. This method behaves as 993 * though it invokes <code>{@link #print(Object)}</code> and then 994 * <code>{@link #println()}</code>. 995 */ 996 public final void println(Object x) { 997 if (this.pw!=null) 998 this.pw.println(x); 999 } 1000 1001 /*** 1002 * Tests FileIO class. It reads from args[0] and write to args[1]. 1003 * 1004 * @param args An array of input and output filename. 1005 */ 1006 public final static void main(String[] args) { 1007 try { 1008 FileIO fi = new FileIO(args[0], "r"); 1009 FileIO fw = new FileIO(args[1], "w"); 1010 int c; 1011 1012 while( (c = fi.read())!=-1) { 1013 fw.write((byte)c); 1014 } 1015 fi.close(); 1016 fw.close(); 1017 } catch (InvalidFileModeException ifme) { 1018 System.err.println(ifme); 1019 } catch (FileIOException fioe) { 1020 System.err.println(fioe); 1021 } catch (IOException ioe) { 1022 System.err.println(ioe); 1023 } 1024 } 1025 } 1026

This page was automatically generated by Maven