Java Notes
Java Notes
It is an abstract class for writing to character streams. The methods that a subclass must
implement are write(char[], int, int), flush(), and close(). Most subclasses will override some
of the methods defined here to provide higher efficiency, functionality or both.
Fields
Constructor
Methods
Modifier and
Method Description
Type
It appends the specified character to this
Writer append(char c)
writer.
It appends the specified character sequence
Writer append(CharSequence csq)
to this writer
append(CharSequence csq, int It appends a subsequence of the specified
Writer
start, int end) character sequence to this writer.
abstract void close() It closes the stream, flushing it first.
abstract void flush() It flushes the stream.
void write(char[] cbuf) It writes an array of characters.
abstract void write(char[] cbuf, int off, int len) It writes a portion of an array of characters.
void write(int c) It writes a single character.
void write(String str) It writes a string.
void write(String str, int off, int len) It writes a portion of a string.
try {
w.write(content);
w.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
Output:
Done
output.txt:
I love my country
Java Reader
Java Reader is an abstract class for reading character streams. The only methods that a
subclass must implement are read(char[], int, int) and close(). Most subclasses, however, will
override some of the methods to provide higher efficiency, additional functionality, or both.
Fields
Constructor
Modifier Constructor Description
It creates a new character-stream reader whose critical sections
protected Reader()
will synchronize on the reader itself.
Reader(Object It creates a new character-stream reader whose critical sections
protected
lock) will synchronize on the given object.
Methods
Modifier and
Method Description
Type
It closes the stream and releases any system
abstract void close()
resources associated with it.
void mark(int readAheadLimit) It marks the present position in the stream.
It tells whether this stream supports the mark()
Boolean markSupported()
operation.
int read() It reads a single character.
int read(char[] cbuf) It reads characters into an array.
read(char[] cbuf, int off,
abstract int It reads characters into a portion of an array.
int len)
It attempts to read characters into the specified
int read(CharBuffer target)
character buffer.
Boolean ready() It tells whether this stream is ready to be read.
void reset() It resets the stream.
long skip(long n) It skips characters.
Example
import java.io.*;
try {
System.out.print((char) data);
data = reader.read();
}
reader.close();
System.out.println(ex.getMessage());
file.txt:
I love my country
Output:
I love my country
Unlike FileOutputStream class, you don't need to convert string into byte array because it
provides method to write string directly.
package com.javatpoint;
import java.io.FileWriter;
try{
fw.write("Welcome to javaTpoint.");
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
Output:
Success...
testout.txt:
Welcome to javaTpoint.
package com.javatpoint;
import java.io.FileReader;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
Here, we are assuming that you have following data in "testout.txt" file:
Welcome to javaTpoint.
Output:
Welcome to javaTpoint.
Java - RandomAccessFile
This class is used for reading and writing to random access file. A random access file behaves
like a large array of bytes. There is a cursor implied to the array called file pointer, by moving
the cursor we do the read write operations. If end-of-file is reached before the desired number
of byte has been read than EOFException is thrown. It is a type of IOException.
Constructor
Constructor Description
Creates a random access file stream to read from, and
RandomAccessFile(File file,
optionally to write to, the file specified by the File
String mode)
argument.
RandomAccessFile(String name, Creates a random access file stream to read from, and
String mode) optionally to write to, a file with the specified name.
Method
Modifier and
Method Method
Type
It closes this random access file stream and releases any
void close()
system resources associated with the stream.
It returns the unique FileChannel object associated with
FileChannel getChannel()
this file.
int readInt() It reads a signed 32-bit integer from this file.
String readUTF() It reads in a string from this file.
void seek(long pos) It sets the file-pointer offset, measured from the
beginning of this file, at which the next read or write
occurs.
It converts the double argument to a long using the
writeDouble(double doubleToLongBits method in class Double, and then
void
v) writes that long value to the file as an eight-byte quantity,
high byte first.
It converts the float argument to an int using the
floatToIntBits method in class Float, and then writes that
void writeFloat(float v)
int value to the file as a four-byte quantity, high byte
first.
void write(int b) It writes the specified byte to this file.
int read() It reads a byte of data from this file.
long length() It returns the length of this file.
Example
import java.io.IOException;
import java.io.RandomAccessFile;
try {
} catch (IOException e) {
e.printStackTrace();
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "r");
file.seek(position);
file.read(bytes);
file.close();
return bytes;
throws IOException {
file.seek(position);
file.write(data.getBytes());
file.close();
The myFile.TXT contains text "This class is used for reading and writing to random access
file."