[edit] Reader Class
|
|
This class defines the model of streaming character input in Java. This class also gives the IOException whenever there is nay error in the Stream.
Few of the methods of this class are explained below in the section:
1. abstract void close( ) : This method will close the input source and once it is closed if we try to read it we will get an IOException as it becomes an error since we are trying to read a closed source.
2. void mark(int numChars) : This method functions for placing a mark at the current point in the input stream that will remain valid until numChars characters are read.
3. boolean markSupported( ) : This method since has Boolean in the strating of writing this method will return the output as either true or false. It returns “True “if mark( )/reset( ) are supported on this stream else returns “false”
4.int read( ) : This method returns an integer representation of the next available character from the invoking input stream. We get –1 as the output if we reach the end of the file.
5. int read(char buffer[ ]) : it tries to read up to buffer.length characters into buffer and returns the actual
number of characters that were successfully read and gives –1 as value when we reach the end of the file.
|
[edit] Writer Class
|
|
This class defines the streaming character output. We get a return value as void value and get an IOException in the case of errors in all its methods.
Few of the methods in this class are :
1.abstract void close( ) : This method will close the output stream and will give the IOException. If we try to access the closed stream as it becomes an error tpo try to access a closed output Stream.
2.abstract void flush( ): It finalizes the output state and makes sure that buffers
.
3.void write(int ch) : This method as defined will write a single character to the invoking output stream.
4. void write(char buffer[ ]) : This method writes a complete array of characters to the invoking output stream.
|
[edit] FileReader Class
|
|
This class creates a Reader which is used to read the contents of a file. Both the constructors which are defined below can throw the exception FileNotFoundException if file is not found.
The two most common constructors of this class are :
1.FileReader(String filePath)
2.FileReader(File fileObj)
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExample {
public static void main(String[] args) throws IOException {
FileReader fileReader;
try {
fileReader = new FileReader("myfile.txt");
int content ;
while((content = fileReader.read()) > 0) {
System.out.print((char)content);
}
}
catch (FileNotFoundException e) {
System.out.println("File Does not exists in " +
"the current directory. Please create a new " +
"file named myfile.txt in the current directory");
}
}
}
We use FileReader for reading streams of characters but if you want to read streams of raw bytes then consider using a FileInputStream.
|
[edit] Java FileWriter
|
|
This class will create a Writer which will be used to write to a file. All the methods of this class will return the Exception as IOException in case of getting errors. When we create a FileWriter , it creates the file before opening it when we create the object and it doesn’t waste the time in looking for the existence of the particular file.
The most common constructors of this class are
1.FileWriter(String filePath)
2.FileWriter(String filePath, boolean append)
3.FileWriter(File fileObj)
4.FileWriter(File fileObj, boolean append)
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) throws IOException {
// the constructor takes two parameters.
// 1. Name of the file
// 2. Whether to open the file in append mode. In append mode
// contents will be appended instead of getting over written.
FileWriter fileWriter = new FileWriter("myfile.txt", false);
// We cannot write String directly using
// FileInputStream. But using FileWriter
// we can.
fileWriter.write("Welcome to Meshplex. " +
"It is a great place to learn.");
fileWriter.close();
}
}
|
[edit] Java CharArrayReader
|
|
This class in implemented from an input stream which uses a character array as the source.
It has two constructors which are :
1.CharArrayReader(char array[ ])
2.CharArrayReader(char array[ ], int start, int numChars)
In both these constructors we take array as the input source.
|
[edit] Java CharArrayWriter
|
|
This class very much like the CharArrayReader implemnts the output stream that uses an array as the final destination the only difference in both is that it takes the input Stream and it takes the Output Source.
It also has two constructors which are defined below:
1.CharArrayWriter( )
This method will create a buffer with the default size.
2.CharArrayWriter(int numChars)
This will create a buffer equal to the size of numChars Variable. If required the buffer size will automatically increase if required.
|
[edit] Java BufferedReader
|
|
The bufferedReader Class like any other buffer improves the performance as it buffers the input.
This class has two constructors which are as under:
1.BufferedReader(Reader inputStream)
This method will create a buffered character stream using a default buffer size.
2.BufferedReader(Reader inputStream, int bufSize)
This method takes the size from the value passed in variable bufSize.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderExample {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("You typed : " + reader.readLine());
}
}
|
[edit] PushbackReader
|
|
This class returns one or more characters to the input stream and also helps us to have the idea of what is going to be the next character in the Input Stream.
It also like other classes has few constructors:
1. PushbackReader(Reader inputStream)
This method pushes one character at a time in the Input Stream.
2.PushbackReader(Reader inputStream, int bufSize)
This method will have the size of pushback buffer equal to the value which is passes in bufSize variable.
This class has three forms which are mentioned below :
1.void unread(int ch)
This pushes back the character passed in ch.
2.void unread(char buffer[ ])
This will return the characters in buffer.
3.void unread(char buffer[ ], int offset, int numChars)
This form will push back numChars characters beginning at offset from buffer.
We will get an IOException if we try to access the pushback which is full.
|
[edit] PrintWriter
|
|
This class is basically a character-oriented version of PrintStream.
It also supports the formatting of stream in print( ) and println( ) methods
This class has four constructors:
1.PrintWriter(OutputStream outputStream)
2.PrintWriter(OutputStream outputStream, boolean flushOnNewline)
3.PrintWriter(Writer outputStream)
4.PrintWriter(Writer outputStream, boolean flushOnNewline)
In these constructors the first and third constructor do not have the feature of automatically flusing the stream while second and fourth one can do it as they both have Boolean flushOnNewline which will either have true or false value and if it is true it will flush else will not.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class PrintWriterExample {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileOutputStream fileOutput = new FileOutputStream("mynewfile.txt");
PrintWriter writer = new PrintWriter(fileOutput);
writer.println("This is first line of the file.");
writer.println("This is second line of the file.");
writer.println("This is third line of the file.");
writer.println("DVD Player : " + 150.00 + " Audio Player : " + 100.00);
writer.flush();
writer.close();
fileOutput.close();
}
}
|
[edit] InputStreamReader
|
| This class is a bridge between character oriented I/O classes and byte oriented I/O classes for reading purpose. Using this class we can make Reader classes to read data from InputStream classes.
This class has four constructors:
1. InputStreamReader(InputStream in)
2. InputStreamReader(InputStream in, Charset cs)
3. InputStreamReader(InputStream in, CharsetDecoder dec)
4. InputStreamReader(InputStream in, String charsetName)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderExample {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("You typed : " + reader.readLine());
}
}
In the above example BufferedReader class is reading data from InputStream.
|
[edit] OutputStreamWriter
|
| This class is a bridge between character oriented I/O classes and byte oriented I/O classes for writing purpose. Using this class we can make Writer classes to write data onto OutputStream classes.
This class has four constructors:
1. OutputStreamWriter(OutputStream out)
2. OutputStreamWriter(OutputStream out, Charset cs)
3. OutputStreamWriter(OutputStream out, CharsetEncoder enc)
4. OutputStreamWriter(OutputStream out, String charsetName)
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class OutputStreamWriterExample {
public static void main(String[] args) throws FileNotFoundException,
IOException {
String str = "Welcome to MeshPlex.";
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(byteArrayOutputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(str);
bufferedWriter.close();
System.out.println(byteArrayOutputStream.toString());
}
}
|