net.jbeans.io
Class FileIO

java.lang.Object
  extended bynet.jbeans.io.FileIO

public final class FileIO
extends java.lang.Object

routines for reading and writing files. Features include transparent use of compression and decompression, stdin/stdout.

Version:
$Revision: 1.1.1.1 $
Author:
Masahiro Takatsuka (masa@jbeans.net)

Field Summary
static int COMMAND
           
static int EOF
           
static int FILEERR
           
static int FILEFORMAT
           
static int FILEMODE
           
static int GZIP
           
static int HEADER
           
static int LINETOOLONG
           
static int NO_MEMORY
           
static int NO_PIPES
           
static int NO_ZIP
           
static int OK
           
static int OPENFILE
           
static int REWINDFILE
           
static int REWINDPIPE
           
static java.lang.String STDERR_NAME
           
static java.lang.String STDIN_NAME
           
static java.lang.String STDOUT_NAME
           
static int UNKNOWN
           
static int ZIP
           
 
Constructor Summary
FileIO(java.lang.String filename, java.lang.String filemode)
          Opens a file for reading or writing.
 
Method Summary
 boolean checkError()
          Flush the stream and check its error state.
 void close()
          Closes FileIO instance.
 void flush()
          Flush the stream.
 java.lang.String getFilename()
          Returns a filename associated with this FileIO object.
 java.lang.String getLine()
          Reads a line from file.
 int getLineNo()
          Returns current line number.
 int getMode()
          Returns the file mode.
 java.lang.String getOriginalFilename()
          Returns the original file name.
 java.lang.String getOriginalMode()
          Returns the original mode string.
 boolean getPipeFlag()
          Returns true if the file is piped.
 JBeansReader getReader()
          returns a input reader associated with this file I/O.
 java.io.PrintWriter getWriter()
          returns a writer associated with this file I/O.
 boolean hasReachedEOF()
          Returns true if the file has reached EOF.
static void main(java.lang.String[] args)
          Tests FileIO class.
 void print(boolean b)
          Print a boolean value.
 void print(char c)
          Print a character.
 void print(char[] s)
          Print an array of characters.
 void print(double d)
          Print a double-precision floating-point number.
 void print(float f)
          Print a floating-point number.
 void print(int i)
          Print an integer.
 void print(long l)
          Print a long integer.
 void print(java.lang.Object obj)
          Print an object.
 void print(java.lang.String s)
          Print a string.
 void println()
          Terminate the current line by writing the line separator string.
 void println(boolean x)
          Print a boolean value and then terminate the line.
 void println(char x)
          Print a character and then terminate the line.
 void println(char[] x)
          Print an array of characters and then terminate the line.
 void println(double x)
          Print a double-precision floating-point number and then terminate the line.
 void println(float x)
          Print a floating-point number and then terminate the line.
 void println(int x)
          Print an integer and then terminate the line.
 void println(long x)
          Print a long integer and then terminate the line.
 void println(java.lang.Object x)
          Print an Object and then terminate the line.
 void println(java.lang.String x)
          Print a String and then terminate the line.
 int read()
          Read a single character.
 int read(char[] cbuf, int off, int len)
          Read characters into a portion of an array.
 boolean readBoolean()
          Reads a boolean from this data input stream.
 byte readByte()
          Reads a signed 8-bit value from this data input stream.
 char readChar()
          Reads a Unicode character from this data input stream.
 double readDouble()
          Reads a double from this data input stream.
 float readFloat()
          Reads a float from this data input stream.
 int readInt()
          Reads a signed 32-bit integer from this data input stream.
 java.lang.String readLine()
          Read a line of text.
 java.lang.String readLine(char commentchar)
          Read a line of text.
 java.lang.String readLine(java.lang.String commentchars)
          Read a line of text.
 long readLong()
          Reads a signed 64-bit integer from this data input stream.
 short readShort()
          Reads a signed 16-bit number from this data input stream.
 java.lang.String readToken()
          Read one token as a string.
 void write(char[] buf)
          Write an array of characters.
 void write(char[] buf, int off, int len)
          Write a portion of an array of characters.
 void write(int c)
          Write a single character.
 void write(java.lang.String s)
          Write a string.
 void write(java.lang.String s, int off, int len)
          Write a portion of a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_ZIP

public static final int NO_ZIP
See Also:
Constant Field Values

ZIP

public static final int ZIP
See Also:
Constant Field Values

GZIP

public static final int GZIP
See Also:
Constant Field Values

STDIN_NAME

public static final java.lang.String STDIN_NAME
See Also:
Constant Field Values

STDOUT_NAME

public static final java.lang.String STDOUT_NAME
See Also:
Constant Field Values

STDERR_NAME

public static final java.lang.String STDERR_NAME
See Also:
Constant Field Values

OK

public static final int OK
See Also:
Constant Field Values

UNKNOWN

public static final int UNKNOWN
See Also:
Constant Field Values

NO_MEMORY

public static final int NO_MEMORY
See Also:
Constant Field Values

FILEMODE

public static final int FILEMODE
See Also:
Constant Field Values

NO_PIPES

public static final int NO_PIPES
See Also:
Constant Field Values

OPENFILE

public static final int OPENFILE
See Also:
Constant Field Values

COMMAND

public static final int COMMAND
See Also:
Constant Field Values

REWINDFILE

public static final int REWINDFILE
See Also:
Constant Field Values

REWINDPIPE

public static final int REWINDPIPE
See Also:
Constant Field Values

LINETOOLONG

public static final int LINETOOLONG
See Also:
Constant Field Values

FILEERR

public static final int FILEERR
See Also:
Constant Field Values

HEADER

public static final int HEADER
See Also:
Constant Field Values

FILEFORMAT

public static final int FILEFORMAT
See Also:
Constant Field Values

EOF

public static final int EOF
See Also:
Constant Field Values
Constructor Detail

FileIO

public FileIO(java.lang.String filename,
              java.lang.String filemode)
       throws InvalidFileModeException,
              FileIOException,
              java.io.IOException
Opens a file for reading or writing. If name is 'null', standard input (System.in) or standard output (System.out) will be used. There are two basic mode "r" for reading and "w" for writing. The mode can have optional characters, which are "z" for "ZIP" compression and "g" for "GZIP" compression. The pipe is not supported yet. If optional characters are not specified, they will be guessed from the file name. The suffix ".zip" implies "ZIP" mode and ".gz", ".z" or ".Z" imply "GZIP" mode.
  try {
 		FileIO fio0 = new FileIO("foo0", "r");
 		FileIO fio1 = new FileIO("foo1.gz", "r");
 		FileIO fio2 = new FileIO("foo2.zip", "rz");
 		FileIO fio3 = new FileIO("foo3.gz", "rg");
				...
		fio0.close();
		fio1.close();
		fio2.close();
		fio3.close();	 
  } catch (InvalidFileModeException ifme) {
  	System.err.println(ifme);
	} catch (FileIOException fioe) {
  	System.err.println(fioe);			
  } catch (IOException ioe) {
  	System.err.println(ioe);			
  } finally {
  }
 

Throws:
(full-classname) - (description)
InvalidFileModeException
FileIOException
java.io.IOException
See Also:
close()
Method Detail

getReader

public final JBeansReader getReader()
returns a input reader associated with this file I/O.

Returns:
InputStreamReader
See Also:
getWriter()

getWriter

public final java.io.PrintWriter getWriter()
returns a writer associated with this file I/O.

Returns:
PrintWriter
See Also:
getReader()

getMode

public final int getMode()
Returns the file mode.

Returns:
int

getOriginalFilename

public final java.lang.String getOriginalFilename()
Returns the original file name.

Returns:
String

getOriginalMode

public final java.lang.String getOriginalMode()
Returns the original mode string.

Returns:
String

hasReachedEOF

public final boolean hasReachedEOF()
Returns true if the file has reached EOF.

Returns:
boolean

getPipeFlag

public final boolean getPipeFlag()
Returns true if the file is piped.

Returns:
boolean

getLineNo

public final int getLineNo()
Returns current line number.

Returns:
int

close

public final void close()
                 throws java.io.IOException
Closes FileIO instance.

Throws:
java.io.IOException
See Also:
FileIO(java.lang.String, java.lang.String)

getFilename

public final java.lang.String getFilename()
Returns a filename associated with this FileIO object.

Returns:
a filename

getLine

public final java.lang.String getLine()
                               throws java.io.EOFException,
                                      java.io.IOException
Reads a line from file. Returns a String object containing a line, null on error.

Returns:
String containing a line.
Throws:
java.io.EOFException
java.io.IOException

read

public final int read()
               throws java.io.IOException
Read a single character.

Returns:
The character read, or -1 if the end of the stream has been reached
Throws:
java.io.IOException - If an I/O error occurs

read

public final int read(char[] cbuf,
                      int off,
                      int len)
               throws java.io.IOException
Read characters into a portion of an array.

Parameters:
cbuf - Destination buffer
off - Offset at which to start storing characters
len - Maximum number of characters to read
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws:
java.io.IOException - If an I/O error occurs

readLine

public final java.lang.String readLine()
                                throws java.io.IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
java.io.IOException - If an I/O error occurs

readLine

public final java.lang.String readLine(java.lang.String commentchars)
                                throws java.io.IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
java.io.IOException - If an I/O error occurs

readLine

public final java.lang.String readLine(char commentchar)
                                throws java.io.IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
java.io.IOException - If an I/O error occurs

readToken

public final java.lang.String readToken()
                                 throws java.io.IOException
Read one token as a string.

Returns:
A String containing the string, or null if the end of the stream has been reached.
Throws:
java.io.IOException - If an I/O error occurs

readBoolean

public final boolean readBoolean()
                          throws java.io.IOException
Reads a boolean from this data input stream. This method reads a single byte from the underlying input stream. A value of 0 represents false. Any other value represents true. This method blocks until either the byte is read, the end of the stream is detected, or an exception is thrown.

Returns:
the boolean value read.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - if an I/O error occurs.

readByte

public final byte readByte()
                    throws java.io.IOException
Reads a signed 8-bit value from this data input stream. This method reads a byte from the underlying input stream. If the byte read is b, where 0 <= b <= 255, then the result is:

This method blocks until either the byte is read, the end of the stream is detected, or an exception is thrown.

Returns:
the next byte of this input stream as a signed 8-bit byte.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - if an I/O error occurs.

readShort

public final short readShort()
                      throws java.io.IOException
Reads a signed 16-bit number from this data input stream. The method reads two bytes from the underlying input stream. If the two bytes read, in order, are b1 and b2, where each of the two values is between 0 and 255, inclusive, then the result is equal to:

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next two bytes of this input stream, interpreted as a signed 16-bit number.
Throws:
java.io.EOFException - if this input stream reaches the end before reading two bytes.
java.io.IOException - if an I/O error occurs.

readChar

public final char readChar()
                    throws java.io.IOException
Reads a Unicode character from this data input stream. This method reads two bytes from the underlying input stream. If the bytes read, in order, are b1 and b2, where 0 <= b1, b1 <= 255, then the result is equal to:

This method blocks until either the two bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next two bytes of this input stream as a Unicode character.
Throws:
java.io.EOFException - if this input stream reaches the end before reading two bytes.
java.io.IOException - if an I/O error occurs.

readInt

public final int readInt()
                  throws java.io.IOException
Reads a signed 32-bit integer from this data input stream. This method reads four bytes from the underlying input stream. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to:

This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next four bytes of this input stream, interpreted as an int.
Throws:
java.io.EOFException - if this input stream reaches the end before reading four bytes.
java.io.IOException - if an I/O error occurs.

readLong

public final long readLong()
                    throws java.io.IOException
Reads a signed 64-bit integer from this data input stream. This method reads eight bytes from the underlying input stream. If the bytes read, in order, are b1, b2, b3, b4, b5, b6, b7, and b8, where

then the result is equal to:

     ((long)b1 << 56) + ((long)b2 << 48) +
        ((long)b3 << 40) + ((long)b4 << 32) +
        ((long)b5 << 24) + (b6 << 16) +
        (b7 << 8) + b8
 

This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next eight bytes of this input stream, interpreted as a long.
Throws:
java.io.EOFException - if this input stream reaches the end before reading eight bytes.
java.io.IOException - if an I/O error occurs.

readFloat

public final float readFloat()
                      throws java.io.IOException
Reads a float from this data input stream. This method reads an int value as if by the readInt method and then converts that int to a float using the intBitsToFloat method in class Float. This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next four bytes of this input stream, interpreted as a float.
Throws:
java.io.EOFException - if this input stream reaches the end before reading four bytes.
java.io.IOException - if an I/O error occurs.

readDouble

public final double readDouble()
                        throws java.io.IOException
Reads a double from this data input stream. This method reads a long value as if by the readLong method and then converts that long to a double using the longBitsToDouble method in class Double.

This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next eight bytes of this input stream, interpreted as a double.
Throws:
java.io.EOFException - if this input stream reaches the end before reading eight bytes.
java.io.IOException - if an I/O error occurs.

flush

public final void flush()
Flush the stream.


checkError

public final boolean checkError()
Flush the stream and check its error state. Errors are cumulative; once the stream encounters an error, this routine will return true on all successive calls.

Returns:
True if the print stream has encountered an error, either on the underlying output stream or during a format conversion.

write

public final void write(int c)
Write a single character.


write

public final void write(char[] buf,
                        int off,
                        int len)
Write a portion of an array of characters.


write

public final void write(char[] buf)
Write an array of characters. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.


write

public final void write(java.lang.String s,
                        int off,
                        int len)
Write a portion of a string.


write

public final void write(java.lang.String s)
Write a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.


print

public final void print(boolean b)
Print a boolean value. The string produced by String.valueOf(boolean) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
b - The boolean to be printed

print

public final void print(char c)
Print a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
c - The char to be printed

print

public final void print(int i)
Print an integer. The string produced by String.valueOf(int) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
i - The int to be printed
See Also:
Integer.toString(int)

print

public final void print(long l)
Print a long integer. The string produced by String.valueOf(long) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
l - The long to be printed
See Also:
Long.toString(long)

print

public final void print(float f)
Print a floating-point number. The string produced by String.valueOf(float) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
f - The float to be printed
See Also:
Float.toString(float)

print

public final void print(double d)
Print a double-precision floating-point number. The string produced by String.valueOf(double) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
d - The double to be printed
See Also:
Double.toString(double)

print

public final void print(char[] s)
Print an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
s - The array of chars to be printed
Throws:
java.lang.NullPointerException - If s is null

print

public final void print(java.lang.String s)
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
s - The String to be printed

print

public final void print(java.lang.Object obj)
Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
obj - The Object to be printed
See Also:
Object.toString()

println

public final void println()
Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').


println

public final void println(boolean x)
Print a boolean value and then terminate the line. This method behaves as though it invokes print(boolean) and then println().


println

public final void println(char x)
Print a character and then terminate the line. This method behaves as though it invokes print(char) and then println().


println

public final void println(int x)
Print an integer and then terminate the line. This method behaves as though it invokes print(int) and then println().


println

public final void println(long x)
Print a long integer and then terminate the line. This method behaves as though it invokes print(long) and then println().


println

public final void println(float x)
Print a floating-point number and then terminate the line. This method behaves as though it invokes print(float) and then println().


println

public final void println(double x)
Print a double-precision floating-point number and then terminate the line. This method behaves as though it invokes print(double) and then println().


println

public final void println(char[] x)
Print an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().


println

public final void println(java.lang.String x)
Print a String and then terminate the line. This method behaves as though it invokes print(String) and then println().


println

public final void println(java.lang.Object x)
Print an Object and then terminate the line. This method behaves as though it invokes print(Object) and then println().


main

public static final void main(java.lang.String[] args)
Tests FileIO class. It reads from args[0] and write to args[1].

Parameters:
args - An array of input and output filename.


Copyright © 2003 JBean project. All Rights Reserved.