File Input and Output
File Input and Output
File Input and Output
Objectives
After you have read and studied this chapter, you should be able to
Use File object to get info about a file Include a JFileChooser object in your program to let the user specify a file. Write bytes to a file and read them back from the file, using FileOutputStream and FileInputStream. Write values of primitive data types to a file and read them back from the file, using DataOutputStream and DataInputStream. Write text data to a file and read them back from the file, using FileWriter,PrintWriter and FileReader,BufferedReader Read a text file using Scanner
File
File is regarded as a collection of bytes When a file is read, computer delivers some of those bytes to the program. When a file is written, computer accepts some bytes from the program and saves them in part of the file. Computer makes no distinction between eg. image files and text files. Its all bytes to the hardware. What those bytes are used for is up to the software.
Types of File
All information in any file is kept in binary form.
Types of File
File can be categorised as text (ASCII) file or binary file. A text file is a file that contains bytes that represent:
characters 'A' through 'Z' characters 'a' through 'z' characters '0' through '9' the space character punctuation and symbols like . , : ; " + - $ (and others) a few control characters that represent end of lines, tabs and some other things.
using ASCII Codes A binary file is a file that contains bytes that represent others (eg. numbers, image, audio, formatted text etc)
Creates File object for the file test.dat in the directory C:\SamplePrograms using the generic file separator / and providing the full pathname.
if ( inFile.isFile() ) { if ( inFile.isDirectory() ) {
File directory = new File("C:/JavaPrograms/Ch12"); String filename[] = directory.list(); for (int i = 0; i < filename.length; i++) { System.out.println(filename[i]); }
if ( inFile.getName() ) {
if ( inFile.canRead() ) {
if ( inFile.canWrite() ) {
The JFileChooser Class A javax.swing.JFileChooser object allows the user to select a file.
JFileChooser chooser = new JFileChooser( );
chooser.showOpenDialog(null);
File selectedFile
= chooser.getSelectedFile();
I/O Streams To read data from or write data to a file, we must create one of the Java stream objects and attach it to the file. A stream is a sequence of data items, usually 8-bit bytes. Java has two types of streams: an input stream and an output stream. An input stream has a source form which the data items come, and an output stream has a destination to which the data items are going.
I/O Streams
I/O Streams
IO streams are either character-oriented or byteoriented. Character-oriented IO has special features for handling character data (text files). Byte-oriented IO is for all types of data (binary files)
Streams for Byte level Binary File I/O FileOutputStream and FileInputStream are two stream objects that facilitate file access. FileOutputStream allows us to output a sequence of bytes; values of data type byte. FileInputStream allows us to read in an array of bytes.
//data to save byte[] byteArray = {10, 20, 30, 40, 50, 60, 70, 80};
//write data to the stream outStream.write( byteArray ); //output done, so close the stream outStream.close();
Streams for Data-Level Binary File I/O FileOutputStream and DataOutputStream are used to output primitive data values FileInputStream and DataInputStream are used to input primitive data values To read the data back correctly, we must know the order of the data stored and their data types
Setting up DataOutputStream
A standard sequence to set up a DataOutputStream object:
Sample Output
import java.io.*; class Ch12TestDataOutputStream { public static void main (String[] args) throws IOException {
. . . //set up outDataStream
//write values of primitive data types to the stream outDataStream.writeInt(987654321); outDataStream.writeLong(11111111L); outDataStream.writeFloat(22222222F); outDataStream.writeDouble(3333333D); outDataStream.writeChar('A'); outDataStream.writeBoolean(true);
//output done, so close the stream outDataStream.close(); } }
Setting up DataInputStream
A standard sequence to set up a DataInputStream object:
Sample Input
import java.io.*; class Ch12TestDataInputStream { public static void main (String[] args) throws IOException {
. . . //set up inDataStream
//read values back from the stream and display them System.out.println(inDataStream.readInt()); System.out.println(inDataStream.readLong()); System.out.println(inDataStream.readFloat()); System.out.println(inDataStream.readDouble()); System.out.println(inDataStream.readChar()); System.out.println(inDataStream.readBoolean());
//input done, so close the stream inDataStream.close(); } }
To write data as a string to text file, use a FileWriter and PrintWriter object To read data from textfile, use FileReader and BufferedReader classes
From Java 5.0 (SDK 1.5), we can also use the Scanner class for reading textfiles