Category: 09. Java.io package

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTu7QxNc2xSxxckK_X5MKxqPL6enQiDOyfZ2w&s

  • Writer 

    Introduction

    The Java.io.Writer class is a abstract class for writing to character streams.

    Class declaration

    Following is the declaration for Java.io.Writer class −

    public abstract class Writer
       extends Object
    
      implements Appendable, Closeable, Flushable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.Writer class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1protected Writer()This creates a new character-stream writer whose critical sections will synchronize on the writer itself.
    2protected Writer(Object lock)This creates a new character-stream writer whose critical sections will synchronize on the given object.

    Class methods

    Sr.No.Method & Description
    1Writer append(char c)This method appends the specified character to this writer.
    2Writer append(CharSequence csq)This method appends the specified character sequence to this writer.
    3Writer append(CharSequence csq, int start, int end)This method appends a subsequence of the specified character sequence to this writer.
    4abstract void close()This method loses the stream, flushing it first.
    5abstract void flush()This method flushes the stream.
    6void write(char[] cbuf)This method writes an array of characters.
    7abstract void write(char[] cbuf, int off, int len)This method writes a portion of an array of characters.
    8void write(int c)This method writes a single character.
    9void write(String str)This method writes a string.
    10void write(String str, int off, int len)This method writes a portion of a string.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • StringWriter

    Introduction

    The Java.io.StringWriter class is a character stream that collects its output in a string buffer, which can then be used to construct a string.Closing a StringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

    Class declaration

    Following is the declaration for Java.io.StringWriter class −

    public class StringWriter
      extends Writer
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.StringWriter class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1StringWriter()This creates a new string writer using the default initial string-buffer size.
    2StringWriter(int initialSize)This creates a new string writer using the specified initial string-buffer size.

    Class methods

    Sr.No.Method & Description
    1StringWriter append(char c)This method appends the specified character to this writer.
    2StringWriter append(CharSequence csq)This method appends the specified character sequence to this writer.
    3StringWriter append(CharSequence csq, int start, int end)This method appends a subsequence of the specified character sequence to this writer.
    4void close()Closing a StringWriter has no effect.
    5void flush()This method flush the stream.
    6StringBuffer getBuffer()This method return the string buffer itself.
    7String toString()This method return the buffer’s current value as a string.
    8void write(char[] cbuf, int off, int len)This method write a portion of an array of characters.
    9void write(int c)This method write a single character.
    10void write(String str)This method writes a string.
    11void write(String str, int off, int len)This method writes a portion of a string.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Writer
    • Java.io.Object
  • StringReader

    Introduction

    The Java.io.StringReader class is a character stream whose source is a string.

    Class declaration

    Following is the declaration for Java.io.StringReader class −

    public class StringReader
       extends Reader
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.StringReader class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1StringReader(String s)This creates a new string reader.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes the stream and releases any system resources associated with it.
    2void mark(int readAheadLimit)This method marks the present position in the stream.
    3boolean markSupported()This method tells whether this stream supports the mark() operation, which it does.
    4int read()This method reads a single character.
    5int read(char[] cbuf, int off, int len)This method reads characters into a portion of an array.
    6boolean ready()This method tells whether this stream is ready to be read.
    7void reset()This method resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.
    8long skip(long ns)This method skips the specified number of characters in the stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Reader
    • Java.io.Object
  • StringBufferInputStream 

    Introduction

    The Java.io.StringBufferInputStream class allows an application to create an input stream in which the bytes read are supplied by the contents of a string. Applications can also read bytes from a byte array by using a ByteArrayInputStream.Only the low eight bits of each character in the string are used by this class.

    This class has been deprecated by Oracle and should not be used any more.

    Class declaration

    Following is the declaration for Java.io.StringBufferInputStream class −

    public class StringBufferInputStream
       extends InputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.StringBufferInputStream class −

    • protected String buffer − This is the string from which bytes are read.
    • protected int count − This is the number of valid characters in the input stream buffer.
    • protected int pos − This is the index of the next character to read from the input stream buffer.

    Class constructors

    Sr.No.Constructor & Description
    1StringBufferInputStream(String s)This creates a string input stream to read data from the specified string.

    Class methods

    Sr.No.Method & Description
    1int available()This method returns the number of bytes that can be read from the input stream without blocking.
    2int read()This method reads the next byte of data from this input stream.
    3int read(byte[] b, int off, int len)This method reads up to len bytes of data from this input stream into an array of bytes.
    4void reset()This method resets the input stream to begin reading from the first character of this input stream’s underlying buffer.
    5long skip(long n)This method skips n bytes of input from this input stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.InputStreams
    • Java.io.Object
  • StreamTokenizer 

    Introduction

    The Java.io.StreamTokenizer class takes an input stream and parses it into “tokens”, allowing the tokens to be read one at a time. The stream tokenizer can recognize identifiers, numbers, quoted strings, and various comment styles.

    Class declaration

    Following is the declaration for Java.io.StreamTokenizer class −

    public class StreamTokenizer
       extends Object
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.StreamTokenizer class −

    • double nval − If the current token is a number, this field contains the value of that number.
    • String sval − If the current token is a word token, this field contains a string giving the characters of the word token.
    • static int TT_EOF − A constant indicating that the end of the stream has been read.
    • static int TT_EOL − A constant indicating that the end of the line has been read.
    • static int TT_NUMBER − A constant indicating that a number token has been read.
    • static int TT_WORD − A constant indicating that a word token has been read.
    • int ttype − After a call to the nextToken method, this field contains the type of the token just read.

    Class constructors

    Sr.No.Constructor & Description
    1StreamTokenizer(Reader r)This creates a tokenizer that parses the given character stream.

    Class methods

    Sr.No.Method & Description
    1void commentChar(int ch)Specified that the character argument starts a single-line comment.
    2void eolIsSignificant(boolean flag)This method determines whether or not ends of line are treated as tokens.
    3int lineno()This method returns the current line number.
    4void lowerCaseMode(boolean fl)This method determines whether or not word token are automatically lowercased.
    5int nextToken()This method parses the next token from the input stream of this tokenizer.
    6void ordinaryChar(int ch)This method specifies that the character argument is “ordinary” in this tokenizer.
    7void ordinaryChars(int low, int hi)This method specifies that all characters c in the range low <= c <= high are “ordinary” in this tokenizer.
    8void parseNumbers()This method specifies that numbers should be parsed by this tokenizer.
    9void pushBack()This method causes the next call to the nextToken method of this tokenizer to return the current value in the ttype field, and not to modify the value in the nval or sval field.
    10void quoteChar(int ch)This method specifies that matching pairs of this character delimit string constants in this tokenizer.
    11void resetSyntax()This method resets this tokenizer’s syntax table so that all characters are “ordinary.” See the ordinaryChar method for more information on a character being ordinary.
    12void slashSlashComments(boolean flag)This method determines whether or not the tokenizer recognizes C++ style comments.
    13void slashStarComments(boolean flag)This method determines whether or not the tokenizer recognizes C style comments.
    14String toString()This method returns the string representation of the current stream token and the line number it occurs on.
    15void whitespaceChars(int low, int hi)This method specifies that all characters c in the range low <= c <= high are white space characters.
    16void wordChars(int low, int hi)This method specifies that all characters c in the range low <= c >= high are word constituents.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • SerializablePermission 

    Introduction

    The Java.io.SerializablePermission class is for Serializable permissions. A SerializablePermission contains a name (also referred to as a “target name”) but no actions list; you either have the named permission or you don’t.The target name is the name of the Serializable permission.

    Class declaration

    Following is the declaration for Java.io.SerializablePermission class −

    public final class SerializablePermission
       extends BasicPermission
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1SerializablePermission(String name)This creates a new SerializablePermission with the specified name.
    2SerializablePermission(String name, String actions)This creates a new SerializablePermission object with the specified name.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.BasicPermission
    • Java.io.Permission
    • Java.io.Object
  • SequenceInputStream 

    Introduction

    The Java.io.SequenceInputStream class represents the logical concatenation of other input streams. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams.

    Class declaration

    Following is the declaration for Java.io.SequenceInputStream class −

    public class SequenceInputStream
       extends InputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1SequenceInputStream(Enumeration<? extends InputStream> e)This initializes a newly created SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects whose run-time type is InputStream.
    2SequenceInputStream(InputStream s1, InputStream s2)This initializes a newly created SequenceInputStream by remembering the two arguments, which will be read in order, first s1 and then s2, to provide the bytes to be read from this SequenceInputStream.

    Class methods

    Sr.No.Method & Description
    1int available()This method returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking by the next invocation of a method for the current underlying input stream.
    2void close()This method closes this input stream and releases any system resources associated with the stream.
    3int read()This method reads the next byte of data from this input stream.
    4int read(byte[] b, int off, int len)This method reads up to len bytes of data from this input stream into an array of bytes.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.InputStream
    • Java.io.Object
  • Reader 

    Introduction

    The Java.io.Reader class is a abstract class for reading character streams.

    Class declaration

    Following is the declaration for Java.io.Reader class −

    public class Reader
       extends Object
    
      implements DataOutput, DataInput, Closeable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.Reader class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1protected Reader()This creates a new character-stream reader whose critical sections will synchronize on the reader itself.
    2protected Reader(Object lock)This creates a new character-stream reader whose critical sections will synchronize on the given object.

    Class methods

    Sr.No.Method & Description
    1abstract void close()This method closes the stream and releases any system resources associated with it.
    2void mark(int readAheadLimit)This method marks the present position in the stream.
    3boolean markSupported()This method tells whether this stream supports the mark() operation.
    4int read()This method reads a single character.
    5int read(char[] cbuf)This method reads characters into an array.
    6abstract int read(char[] cbuf, int off, int len)This method reads characters into a portion of an array.
    7int read(CharBuffer target)This method attempts to read characters into the specified character buffer.
    8boolean ready()This method tells whether this stream is ready to be read.
    9void reset()This method resets the stream.
    10long skip(long n)This method skips characters.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • RandomAccessFile 

    Introduction

    The Java.io.RandomAccessFile class file behaves like a large array of bytes stored in the file system.Instances of this class support both reading and writing to a random access file.

    Class declaration

    Following is the declaration for Java.io.RandomAccessFile class −

    public class RandomAccessFile
       extends Object
    
      implements DataOutput, DataInput, Closeable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1RandomAccessFile(File file, String mode)This creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
    2RandomAccessFile(File file, String mode)This creates a random access file stream to read from, and optionally to write to, a file with the specified name.

    Class methods

    Sr.No.Method & Description
    1void close()This method Closes this random access file stream and releases any system resources associated with the stream.
    2FileChannel getChannel()This method returns the unique FileChannel object associated with this file.
    3FileDescriptor getFD()This method returns the opaque file descriptor object associated with this stream.
    4long getFilePointer()This method returns the current offset in this file.
    5long length()This method returns the length of this file.
    6int read()This method reads a byte of data from this file.
    7int read(byte[] b)This method reads up to b.length bytes of data from this file into an array of bytes.
    8int read(byte[] b, int off, int len)This method reads up to len bytes of data from this file into an array of bytes.
    9boolean readBoolean()This method reads a boolean from this file.
    10byte readByte()This method reads a signed eight-bit value from this file.
    11char readChar()This method reads a character from this file.
    12double readDouble()This method reads a double from this file.
    13float readFloat()This method reads a float from this file.
    14void readFully(byte[] b)This method reads b.length bytes from this file into the byte array, starting at the current file pointer.
    15void readFully(byte[] b, int off, int len)This method reads exactly len bytes from this file into the byte array, starting at the current file pointer.
    16int readInt()This method reads a signed 32-bit integer from this file.
    17String readLine()This method reads the next line of text from this file.
    18long readLong()This method reads a signed 64-bit integer from this file.
    19short readShort()This method reads a signed 16-bit number from this file.
    20int readUnsignedByte()This method reads an unsigned eight-bit number from this file.
    21int readUnsignedShort()This method reads an unsigned 16-bit number from this file.
    22String readUTF()This method reads in a string from this file.
    23void seek(long pos)This method sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
    24void setLength(long newLength)This method sets the length of this file.
    25int skipBytes(int n)This method attempts to skip over n bytes of input discarding the skipped bytes.
    26void write(byte[] b)This method writes b.length bytes from the specified byte array to this file, starting at the current file pointer.
    27void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this file.
    28void write(int b)This method writes the specified byte to this file.
    29void writeBoolean(boolean v)This method writes a boolean to the file as a one-byte value.
    30void writeByte(int v)This method writes a byte to the file as a one-byte value.
    31void writeBytes(String s)This method writes the string to the file as a sequence of bytes.
    32void writeChar(int v)This method writes a char to the file as a two-byte value, high byte first.
    33void writeChars(String s)This method writes a string to the file as a sequence of characters.
    34void writeDouble(double v)This method converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first.
    35void writeFloat(float v)This method converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the file as a four-byte quantity, high byte first.
    36void writeInt(int v)This method writes an int to the file as four bytes, high byte first.
    37void writeLong(long v)This method writes a long to the file as eight bytes, high byte first.
    38void writeShort(int v)This method writes a short to the file as two bytes, high byte first.
    39void writeUTF(String str)This method writes a string to the file using modified UTF-8 encoding in a machine-independent manner.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • PushbackReader 

    Introduction

    The Java.io.PushbackReader class is a character-stream reader that allows characters to be pushed back into the stream.

    Class declaration

    Following is the declaration for Java.io.PushbackReader class −

    public class PushbackReader
       extends FilterReader
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.PushbackReader class −

    • protected Reader in − This is the character-input stream.
    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1PushbackReader(Reader in)This creates a new pushback reader with a one-character pushback buffer.
    2PushbackReader(Reader in, int size)This creates a new pushback reader with a pushback buffer of the given size.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes the stream and releases any system resources associated with it.
    2void mark(int readAheadLimit)This method marks the present position in the stream.
    3boolean markSupported()This method tells whether this stream supports the mark() operation, which it does not.
    4int read()This method reads a single character.
    5int read(char[] cbuf, int off, int len)This method reads characters into a portion of an array.
    6boolean ready()This method tells whether this stream is ready to be read.
    7void reset()This method resets the stream.
    8long skip(long n)This method skips characters.
    9void unread(char[] cbuf)This method pushes back an array of characters by copying it to the front of the pushback buffer.
    10void unread(char[] cbuf, int off, int len)This method pushes back a portion of an array of characters by copying it to the front of the pushback buffer.
    11void unread(int c)This method pushes back a single character by copying it to the front of the pushback buffer.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Reader
    • Java.io.Object