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
1
InputStreamReader(InputStream in)This creates an InputStreamReader that uses the default charset.
2
InputStreamReader(InputStream in, Charset cs)This creates an InputStreamReader that uses the given charset.
3
InputStreamReader(InputStream in, CharsetDecoder dec)This creates an InputStreamReader that uses the given charset decoder.
4
InputStreamReader(InputStream in, String charsetName)This creates an InputStreamReader that uses the named charset.
Class methods
Sr.No.
Method & Description
1
void close()This method closes the stream and releases any system resources associated with it.
2
String getEncoding()This method returns the name of the character encoding being used by this stream.
3
int read()This method reads a single character.
4
int read(char[] cbuf, int offset, int length)This method reads characters into a portion of an array.
5
boolean ready()This method tells whether this stream is ready to be read.
Methods inherited
This class inherits methods from the following classes −
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
1
InputStream()Single Constructor
Class methods
Sr.No.
Method & Description
1
int 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.
2
void close()This method closes this input stream and releases any system resources associated with the stream.
3
void mark(int readlimit)This method marks the current position in this input stream.
4
boolean markSupported()This method tests if this input stream supports the mark and reset methods.
5
abstract int read()This method reads the next byte of data from the input stream.
6
int read(byte[] b)This method reads some number of bytes from the input stream and stores them into the buffer array b.
7
int 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.
8
void reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.
9
long 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 −
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
1
FilterOutputStream(OutputStream out)This creates an output stream filter built on top of the specified underlying output stream.
Class methods
Sr.No.
Method & Description
1
void close()This method closes this output stream and releases any system resources associated with the stream.
2
void flush()This method flushes this output stream and forces any buffered output bytes to be written out to the stream.
3
void write(byte[] b)This method writes b.length bytes to this output stream.
4
void 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.
5
void write(int b)This method writes the specified byte to this output stream.
Methods inherited
This class inherits methods from the following classes −
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
1
protected 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
1
int 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.
2
void close()This method closes this input stream and releases any system resources associated with the stream.
3
void mark(int readlimit)This method marks the current position in this input stream.
4
boolean markSupported()This method tests if this input stream supports the mark and reset methods.
5
int read()This method reads the next byte of data from this input stream.
6
int read(byte[] b)This method reads up to byte.length bytes of data from this input stream into an array of bytes.
7
int 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.
8
void reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.
9
long 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 −
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
1
FileWriter(File file)This constructor creates a FileWriter object given a File object.
2
FileWriter(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.
3
FileWriter(FileDescriptor fd)This constructor creates a FileWriter object associated with the given file descriptor.
4
FileWriter(String fileName)This constructor creates a FileWriter object, given a file name.
5
FileWriter(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
1
public void write(int c) throws IOExceptionWrites a single character.
2
public 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.
3
public 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[] a =newchar[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[] a =newchar[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>
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
1
FileReader(File file)This constructor creates a new FileReader, given the File to read from.
2
FileReader(FileDescriptor fd)This constructor creates a new FileReader, given the FileDescriptor to read from.
3
FileReader(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
1
public int read() throws IOExceptionReads a single character. Returns an int, which represents the character read.
2
public 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[] a =newchar[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[] a =newchar[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>
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
1
FilePermission(String path, String actions)This creates a new FilePermission object with the specified actions.
Class methods
Sr.No.
Method & Description
1
boolean equals(Object obj)This method checks two FilePermission objects for equality.
2
String getActions()This method returns the “canonical string representation” of the actions.
3
int hashCode()This method returns the hash code value for this object.
4
boolean implies(Permission p)This method checks if this FilePermission object “implies” the specified permission.
5
PermissionCollection newPermissionCollection()This method returns a new PermissionCollection object for storing FilePermission objects.
Methods inherited
This class inherits methods from the following classes −
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
1
FileOutputStream(File file)This creates a file output stream to write to the file represented by the specified File object.
2
FileOutputStream(File file, boolean append)This creates a file output stream to write to the file represented by the specified File object.
3
FileOutputStream(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.
4
FileOutputStream(String name)This creates an output file stream to write to the file with the specified name.
5
FileOutputStream(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
1
void close()This method closes this file output stream and releases any system resources associated with this stream.
2
protected 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.
3
FileChannel getChannel()This method returns the unique FileChannel object associated with this file output stream.
4
FileDescriptor getFD()This method returns the file descriptor associated with this stream.
5
void write(byte[] b)This method writes b.length bytes from the specified byte array to this file output stream.
6
void 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.
7
void write(int b)This method writes the specified byte to this file output stream.
Methods inherited
This class inherits methods from the following classes −