Author: saqibkhan

  • InputStreamReader 

    Introduction

    The Java.io.InputStreamReader class is a bridge from byte streams to character streams.It reads bytes and decodes them into characters using a specified charset.

    Class declaration

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

    public class InputStreamReader
       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.InputStreamReader class −

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

    Class constructors

    Sr.No.Constructor & Description
    1InputStreamReader(InputStream in)This creates an InputStreamReader that uses the default charset.
    2InputStreamReader(InputStream in, Charset cs)This creates an InputStreamReader that uses the given charset.
    3InputStreamReader(InputStream in, CharsetDecoder dec)This creates an InputStreamReader that uses the given charset decoder.
    4InputStreamReader(InputStream in, String charsetName)This creates an InputStreamReader that uses the named charset.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes the stream and releases any system resources associated with it.
    2String getEncoding()This method returns the name of the character encoding being used by this stream.
    3int read()This method reads a single character.
    4int read(char[] cbuf, int offset, int length)This method reads characters into a portion of an array.
    5boolean ready()This method tells whether this stream is ready to be read.

    Methods inherited

    This class inherits methods from the following classes −

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

    Introduction

    The Java.io.InputStream class is the superclass of all classes representing an input stream of bytes. Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.

    Class declaration

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

    public abstract class InputStream
       extends Object
    
      implements 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
    1InputStream()Single Constructor

    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 this input stream without blocking by the next invocation of a method for this input stream.
    2void close()This method closes this input stream and releases any system resources associated with the stream.
    3void mark(int readlimit)This method marks the current position in this input stream.
    4boolean markSupported()This method tests if this input stream supports the mark and reset methods.
    5abstract int read()This method reads the next byte of data from the input stream.
    6int read(byte[] b)This method reads some number of bytes from the input stream and stores them into the buffer array b.
    7int read(byte[] b, int off, int len) <This method reads up to len bytes of data from the input stream into an array of bytes.
    8void reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.
    9long skip(long n)This method skips over and discards n bytes of data from this input stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • FilterWriter 

    Introduction

    The Java.io.FilterWriter class is for writing filtered character streams. Following are the important points about FilterWriter −

    • The class itself provides default methods that pass all requests to the contained stream.
    • The Subclasses of FilterWriter should override some of these methods and may also provide additional methods and fields.

    Class declaration

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

    public abstract class FilterWriter
       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.FilterWriter class −

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

    Class constructors

    Sr.No.Constructor & Description
    1protected FilterWriter(Writer in)This creates a new filtered writer.

    Class methods

    Sr.No.Method & Description
    1void close()This method flushes the stream.
    2void flush()This method flushes the stream.
    3void write(char[] cbuf, int off, int len)This method writes a portion of an array of characters.
    4void write(int c)This method writes a single character.
    5void 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
  • FilterReader 

    Introduction

    The Java.io.FilterReader class is for reading filtered character streams. Following are the important points about FilterReader −

    • The class itself provides default methods that pass all requests to the contained stream.
    • The Subclasses of FilterReader should override some of these methods and may also provide additional methods and fields.

    Class declaration

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

    public abstract class FilterReader
       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.FilterReader 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
    1protected FilterReader(Reader in)This creates a new filtered 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.
    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.

    Methods inherited

    This class inherits methods from the following classes −

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

    Introduction

    The Java.io.FilterOutputStream class is the superclass of all classes that filter output streams. Following are the important points about FilterOutputStream −

    • The class itself simply overrides all methods of OutputStream with versions that pass all requests to the contained output stream.
    • The Subclasses of this class may further override some of these methods and may also provide additional methods and fields.

    Class declaration

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

    public class FilterOutputStream
       extends OutputStream
    

    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.FilterOutputStream class −

    • protected OutputStream out − This is the output stream to be filtered.

    Class constructors

    Sr.No.Constructor & Description
    1FilterOutputStream(OutputStream out)This creates an output stream filter built on top of the specified underlying output stream.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes this output stream and releases any system resources associated with the stream.
    2void flush()This method flushes this output stream and forces any buffered output bytes to be written out to the stream.
    3void write(byte[] b)This method writes b.length bytes to this output stream.
    4void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this output stream.
    5void write(int b)This method writes the specified byte to this output stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • FilterInputStream 

    Introduction

    The Java.io.FilterInputStream class contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality. Following are the important points about FilterInputStream −

    • The class itself simply overrides all methods of InputStream with versions that pass all requests to the contained input stream.
    • The Subclasses of this class may further override some of these methods and may also provide additional methods and fields.

    Class declaration

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

    public class FilterInputStream
       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.FilterInputStream class −

    • protected InputStream in − This is the input stream to be filtered.

    Class constructors

    Sr.No.Constructor & Description
    1protected FilterInputStream(InputStream in)This creates a FilterInputStream by assigning the argument in to the field this.in to remember it for later use.

    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 this input stream without blocking by the next caller of a method for this input stream.
    2void close()This method closes this input stream and releases any system resources associated with the stream.
    3void mark(int readlimit)This method marks the current position in this input stream.
    4boolean markSupported()This method tests if this input stream supports the mark and reset methods.
    5int read()This method reads the next byte of data from this input stream.
    6int read(byte[] b)This method reads up to byte.length bytes of data from this input stream into an array of bytes.
    7int 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.
    8void reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.
    9long skip(long n)This method skips over and discards n bytes of data from this input stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • FileWriter 

    Introduction

    The Java FileWriter class is a convenience class for writing character files.Following are the important points about FileWriter −

    • The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable.
    • FileWriter is meant for writing streams of characters. For writing streams of raw bytes, use FileOutputStream.

    Class declaration

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

    publicclassFileWriterextendsOutputStreamWriter

    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.FileWriter class −

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

    Class constructors

    Sr.No.Constructor & Description
    1FileWriter(File file)This constructor creates a FileWriter object given a File object.
    2FileWriter(File file, boolean append)This constructor creates a FileWriter object given a File object with a boolean indicating whether or not to append the data written.
    3FileWriter(FileDescriptor fd)This constructor creates a FileWriter object associated with the given file descriptor.
    4FileWriter(String fileName)This constructor creates a FileWriter object, given a file name.
    5FileWriter(String fileName, boolean append)This constructor creates a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

    Once you have FileWriter object in hand, then there is a list of helper methods, which can be used to manipulate the files.

    Sr.No.Method & Description
    1public void write(int c) throws IOExceptionWrites a single character.
    2public void write(char [] c, int offset, int len)Writes a portion of an array of characters starting from offset and with a length of len.
    3public void write(String s, int offset, int len)Write a portion of a String starting from offset and with a length of len.

    Example 1

    The following example shows the usage of Java FileWriter class. We’ve created a File reference with name Hello1.txt to be created in current directory. Then we’re creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we’re reading that file and its contents are printed.

    Open Compiler

    packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("Hello1.txt");// creates the file
    
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char&#91;] a =newchar&#91;50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}</code></pre>

    Output

    This
    is
    an
    example
    

    Example 2

    The following example shows the usage of Java FileWriter class. We've created a File reference with name Hello1.txt to be created in a provided directory. Then we're creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we're reading that file and its contents are printed.

    packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("F:/Test2/Hello1.txt");// creates the file
    
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char&#91;] a =newchar&#91;50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}</code></pre>

    Output

    This
    is
    an
    example
    
  • FileReader 

    Introduction

    The Java.io.FileReader class is a convenience class for reading character files.Following are the important points about FileReader −

    • The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.
    • FileReader is meant for reading streams of characters. For reading streams of raw bytes, use FileInputStream.

    Class declaration

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

    publicclassFileReaderextendsInputStreamReader

    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.FileReader class −

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

    Class constructors

    Sr.No.Constructor & Description
    1FileReader(File file)This constructor creates a new FileReader, given the File to read from.
    2FileReader(FileDescriptor fd)This constructor creates a new FileReader, given the FileDescriptor to read from.
    3FileReader(String fileName)This constructor creates a new FileReader, given the name of the file to read from.

    Once you have FileReader object in hand then there is a list of helper methods which can be used to manipulate the files.

    Sr.No.Method & Description
    1public int read() throws IOExceptionReads a single character. Returns an int, which represents the character read.
    2public int read(char [] c, int offset, int len)Reads characters into an array. Returns the number of characters read.

    Example 1

    The following example shows the usage of Java FileReader class. We’ve created a File reference with name Hello1.txt to be created in current directory. Then we’re creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we’re reading that file and its contents are printed.

    Open Compiler

    packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("Hello1.txt");// creates the file
    
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char&#91;] a =newchar&#91;50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}</code></pre>

    Output

    This
    is
    an
    example
    

    Example 2

    The following example shows the usage of Java FileReader class. We've created a File reference with name Hello1.txt to be created in a provided directory. Then we're creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we're reading that file and its contents are printed.

    packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("F:/Test2/Hello1.txt");// creates the file
    
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char&#91;] a =newchar&#91;50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}</code></pre>

    Output

    This
    is
    an
    example
    
  • FilePermission 

    Introduction

    The Java.io.FilePermission class represents access to a file or directory.It consists of a pathname and a set of actions valid for that pathname. Following are the important points about FilePermission −

    • The actions to be granted are passed to the constructor in a string containing a list of one or more comma-separated keywords. The possible keywords are “read”, “write”, “execute”, and “delete”.
    • Code can always read a file from the same directory it’s in (or a subdirectory of that directory); it does not need explicit permission to do so.

    Class declaration

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

    public final class FilePermission
       extends Permission
    
      implements Serializable

    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
    1FilePermission(String path, String actions)This creates a new FilePermission object with the specified actions.

    Class methods

    Sr.No.Method & Description
    1boolean equals(Object obj)This method checks two FilePermission objects for equality.
    2String getActions()This method returns the “canonical string representation” of the actions.
    3int hashCode()This method returns the hash code value for this object.
    4boolean implies(Permission p)This method checks if this FilePermission object “implies” the specified permission.
    5PermissionCollection newPermissionCollection()This method returns a new PermissionCollection object for storing FilePermission objects.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Permission
    • Java.io.Object
  • FileOutputStream 

    Introduction

    The Java.io.FileOutputStream class is an output stream for writing data to a File or to a FileDescriptor. Following are the important points about FileOutputStream −

    • This class is meant for writing streams of raw bytes such as image data.
    • For writing streams of characters, use FileWriter.

    Class declaration

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

    public class FileOutputStream
       extends OutputStream
    

    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
    1FileOutputStream(File file)This creates a file output stream to write to the file represented by the specified File object.
    2FileOutputStream(File file, boolean append)This creates a file output stream to write to the file represented by the specified File object.
    3FileOutputStream(FileDescriptor fdObj)This creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
    4FileOutputStream(String name)This creates an output file stream to write to the file with the specified name.
    5FileOutputStream(String name, boolean append)This creates an output file stream to write to the file with the specified name.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes this file output stream and releases any system resources associated with this stream.
    2protected void finalize()This method cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.
    3FileChannel getChannel()This method returns the unique FileChannel object associated with this file output stream.
    4FileDescriptor getFD()This method returns the file descriptor associated with this stream.
    5void write(byte[] b)This method writes b.length bytes from the specified byte array to this file output stream.
    6void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this file output stream.
    7void write(int b)This method writes the specified byte to this file output stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.OutputStream
    • Java.io.Object