Handling Primitive Types

The basic input and output streams provide read and write methods that can only be used for reading/writing bytes or characters. If we want to read and write the primitive data types such as integers and doubles, we can use filter classes as wrappers on existing input and output streams to filter data in the original stream. The two filter classes used for creating data streams for handling primitive data types are DataInputStream and DataOutputStream.

A data stream for input can be created as follows:

DataInputStream dis = new DataInputStream(new FileInputStream(inFile));

Similarly, a data stream for output can be created as follows:

DataOutputStream dos = new DataOutputStream(new FileOutputStream(oFile));

The following program (File09.java) demonstrates the use of data streams for reading and writing primitive data types. The program first creates test.text file and then writes a few primitive data types into it using DataOutputStream. At the end of writing, the stream is closed. The program also creates a DataInputStream, connects it to test.text file and displays them on the screen. Finally, it closes the stream. See the program below:

File09.java

import java.io.*;

public class FileTest09 {

public static void main(String[] args) throws IOException {
File f = new File("test.txt");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));

// Write primitive data
dos.writeInt(2019);
dos.writeBoolean(true);
dos.writeChar('S');
dos.writeDouble(99.95);
dos.close();

DataInputStream dis = new DataInputStream(new FileInputStream(f));

// Read primitive data
System.out.println(dis.readInt());
System.out.println(dis.readBoolean());
System.out.println(dis.readChar());
System.out.println(dis.readDouble());
dis.close();
}
}
Output:
C:\file>java File09
2019
true
S
81.95

 

 

Filing using BufferedReader and BufferedWriter

Using BufferedReader and BufferedWriter classes we can read and write data in blocks to minimize I/O overhead. These classes are chained to low-level streams such as FileReader and FileWriter. Initially, we take keyboard input using BufferedReaderInputStreamReader combination. Then, we perform the write and read operation on files. See the program (File09a.java) below:

File09a.java

import java.io.*;

public class FileTest09a {

public static void main(String[] args) throws IOException {

// Take keyboard input
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter an integer: ");
Integer a = Integer.parseInt(input.readLine());
System.out.println("Enter a boolean: ");
Boolean b = Boolean.parseBoolean(input.readLine());
System.out.println("Enter a char: ");
Character c = input.readLine().charAt(0);
System.out.println("Enter a decimal: ");
Double d = Double.parseDouble(input.readLine());

File f = new File("testing.txt");

// Perform write operation to a file
BufferedWriter br = new BufferedWriter(new FileWriter(f));
br.write(a.toString()+"\n");
br.write(b.toString()+"\n");
br.write(c.toString()+"\n");
br.write(d.toString()+"\n");
br.close();

// Perform read operation from the same file
System.out.println("\nFile Contents:~ ");
BufferedReader in = new BufferedReader(new FileReader(f));
String line;
while ((line = in.readLine()) != null){
System.out.println(line);
}
in.close();
}
}
Output:
C:\file>java File09a
Enter an integer:
25
Enter a boolean:
false
Enter a char:
S
Enter a decimal:
75.95

File Contents:~
25
false
S
75.95