Java Files
Java Files
BufferedReader,BufferedWriter
DataInputStream, DataOutputStream
DataOutputStream and DataInputStream
DataOutputStream and DataInputStream enable you to
write or read primitive data to or from a stream.
They implement the DataOutput and DataInput
interfaces, respectively.
These interfaces define methods that convert primitive values
to or from a sequence of bytes.
These streams make it easy to store binary data, such as
integers or floating-point values, in a file.
DataOutputStream
DataOutputStream(OutputStream outputStream)
final void writeDouble(double value) throws IOException
final void writeBoolean(boolean value) throws IOException
final void writeInt(int value) throws IOException
DataInputStream
DataInputStream(InputStream inputStream)
double readDouble( ) throws IOException
boolean readBoolean( ) throws IOException
int readInt( ) throws IOException
Example
class DataIODemo {
public static void main(String args[]) throws IOException {
FileOutputStream fout = new FileOutputStream("Test.dat");
DataOutputStream out = new DataOutputStream(fout);
out.writeDouble(98.6);
out.writeInt(1000);
out.writeBoolean(true);
out.close();
FileInputStream fin = new FileInputStream("Test.dat");
DataInputStream in = new DataInputStream(fin);
double d = in.readDouble();
int i = in.readInt();
boolean b = in.readBoolean();
System.out.println("Here are the values: " +
d + " " + i + " " + b);
in.close();
}
}
Reader and Writer
Reader is an abstract class that defines Java’s model of
streaming character input. It implements the Closeable and
Readable interfaces.
Writer is an abstract class that defines streaming character
output. It implements the Closeable, Flushable, and
Appendable interfaces
FileReader and FileWriter
The FileReader class creates a Reader that you can use to
read the contents of a file. Its two most commonly used
constructors are shown here:
FileReader(String filePath)
FileReader(File fileObj)
FileWriter creates a Writer that you can use to write to a
file. Its most commonly used constructors are shown here:
FileWriter(String filePath)
FileWriter(String filePath, boolean append)
FileWriter(File fileObj)
FileWriter(File fileObj, boolean append)
Reader methods
Writer Methods
File Reader Example
import java.io.*;
class FileReaderDemo {
public static void main(String args[]) throws IOException {
FileReader fr = new FileReader("FileReaderDemo.java");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
}
}
File Writer Example
class FileWriterDemo {
public static void main(String args[]) throws IOException {
String source = "Now is the time for all good men\n"
+ " to come to the aid of their country\n"
+ " and pay their due taxes.";
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);
FileWriter f0 = new FileWriter("file1.txt");
for (int i=0; i < buffer.length; i += 2) {
f0.write(buffer[i]);
}
f0.close();
FileWriter f1 = new FileWriter("file2.txt");
f1.write(buffer);
f1.close();
FileWriter f2 = new FileWriter("file3.txt");
f2.write(buffer,buffer.length-buffer.length/4,buffer.length/4);
f2.close();
}
}
BufferedWriter Class
Java BufferedWriter class is used to provide buffering for
Writer instances. It makes the performance fast. It
inherits Writer class. The buffering characters are used for
providing the efficient writing of single arrays, characters,
and strings.
Constructor Description