Random Access Files in Java
Use of class RandomAccessFile
• As the name implies the class RandomAccessFile allows us to handle a
file randomly in contrast to sequentially in InputStream or
OutputStream classes
• It allows to move file pointer randomly
• Moreover, it allows read or write or read-write simultaneously.
Class: RandomAccessFile
Object
DataInput DataOutput
RandomAccessFile
Example: RandomAccessFile
import [Link].*; [Link]([Link]());
class RandomIO [Link]([Link]());
{ [Link](2); // Go to the second item
public static void main (String args[)) [Link]([Link]());
{ // Go to the end and append false to
RandomAccessFile file = null; the file
try { [Link]([Link]());
file = new [Link](false);
file. seek (4) ;
RandomAccessFile("[Link]","rw");
[Link]([Link]());
// Writing to the file
[Link]();
[Link]('X');
}
[Link](555);
catch(IOException e)
[Link](3.1412); {
[Link] (0);
// Go to the beginning [Link](e);
// Reading from the file }
[Link]([Link]()); }
}
Example: Appending to a RAF
import [Link].*;
class RandomAccess
{
static public void main(String args[])
{
RandomAccessFile rFile;
try
{
rFile = new RandomAccessFile("[Link]","rw");
[Link]([Link]()); // Go to the end
[Link]("MUMBAI\n"); //Append MUMBAI
[Link]();
}
catch(IOException ioe)
{
[Link](ioe);
}
}
}
Interactive Input-Output
Interactive input and output
// Writing to the file "[Link]"
import java. util. *; // For using StringTokenizer
import java. util. *; // For using StringTokenizer class
import [Link].*;
class Inventory {
class
[Link](code);
import [Link].*; static DataInputStream din = new DataInputStream([Link]);
[Link](items);
static StringTokenizer st;
public static void main (String args[J) throws
class Inventory { IOException
{
[Link](cost);
static DataInputStream din = new DataInputStream([Link]);
DataOutputStream dos = new DataOutputStream(new
FileOutputStream("[Link]"));
// Reading from console
[Link]();
static StringTokenizer st;
[Link]("Enter code number");
st = new StringTokenizer([Link]());
// Processing data from the file
int code = [Link]([Link]());
public static void main (String args[J)DataInputStream
throws
[Link]("Enter number of items");
st = new StringTokenizer([Link]());
dis=new DataInputStream(new
int items = [Link]([Link]());
IOException [Link]("Enter cost");
FileInputStream("[Link]"));
st = new StringTokenizer([Link]());
{
double cost = new
int codeNumber = [Link]();
Double([Link]()).doubleValue{};
DataOutputStream dos = new DataOutputStream(new
int totalItems = [Link]();
FileOutputStream("[Link]")); double itemCost = [Link]();
// Reading from console double totalCost = total Items * itemCost;
[Link]("Enter code number");
[Link]();
st = new StringTokenizer([Link]());
// Writing to console
// Writing to the file "[Link]"
[Link](code);
int code = [Link]([Link]());
[Link](items);
[Link](cost);
[Link](); [Link]();
[Link]("Enter number of items");
// Processing data from the file
[Link]("Code Number : " + codeNumber);
DataInputStream dis=new DataInputStream(new
FileInputStream("[Link]"));
st = new StringTokenizer([Link]());
[Link]("Item Cost : " + itemCost);
int codeNumber = [Link]();
int totalItems = [Link]();
double itemCost = [Link]();
int items = [Link]([Link]());
[Link]("Total Items : " + totalItems);
double totalCost = total Items * itemCost;
[Link]();
// Writing to console
[Link]("Enter cost"); [Link]("Total Cost : " + totalCost);
[Link]();
[Link]("Code Number : " + codeNumber);
st = new StringTokenizer([Link]());
[Link]("Item Cost : " + itemCost);
}
[Link]("Total Items : " + totalItems);
[Link]("Total Cost : " + totalCost);
double cost = new Double([Link]()).doubleValue{};
}
}
}
Graphical Input-Output
Graphical input and output
Graphical input and output
import [Link].*;
import [Link].*; number = new TextField(25);
class StudentFile extends Frame number = new TextField(25);
{ numLabel = new Label("Roll Number");
// Defining window components name = new TextField(25);
TextField number, name, marks; nameLabel = new Label ("Student Name");
Button enter, done; marks = new TextField(25);
Label numLabel, nameLabel, markLabel; markLabel = new Label("Marks");
DataOutputStream dos; enter = new Button("ENTER");
done = new Button("DONE");
// Initialize the Frame // Add the components to the Frame
public StudentFile() add(numLabel);
{ add(number);
super("Create Student File"); add(nameLabel);
} add(name);
// Setup the window add(markLabel);
public void setup() add(marks);
{ add(enter);
resize(400, 200); add(done);
setLayout(new GridLayout(4,2)); // Show the Frame
// Create the components of the Frame show();
Graphical input and output
// Open the file catch(IOException e) { }
try { // Clear the text fields
dos new DataOutputStream( new [Link](" ");
FileOutputStream("[Link]")); [Link](" ");
} [Link](" ");
catch(IOException e) { }
[Link]([Link]()); // Adding the record and clearing the
[Link](1); TextFields
} public void cleanup()
} {
// Write to the file if(![Link](). equals(" ")) {
public void addRecord() { addRecord();
int num; }
Double d; try {
num = (new [Link]();
Integer([Link]())).intValue(); [Link]();
try { }
[Link](num); catch(IOException e) { }
[Link]([Link]()); }
d = new Double([Link]());
[Link]([Link]());
}
Graphical input and output
// Processing the event if([Link] instanceof Button)
public boolean action(Event if([Link]("DONE")) {
event,Object o) cleanup();
{ [Link](0);
if([Link] instanceof return true;
Button) }
if([Link]("ENTER")) { return [Link](event);
addRecord(); }
return true; // Execute the program
} public static void main (String args[])
return [Link](event, o); {
} StudentFile student = new StudentFile();
public boolean handleEvent(Event [Link]();
event) }
{ }
Another graphical Input/Output
import [Link].*; number = new TextField(25);
import [Link].*; numLabel = new Label ("Roll Number");
class ReadStudentFile extends Frame name = new TextField(25);
{ nameLabel = new Label ("Student Name");
// Defining window components
marks = new TextField(25);
TextField number, name, marks;
markLabel = new Label("Marks");
Button next, done;
Label numLabel, nameLabel, markLabel; next new Button("NEXT");
DataInputStream dis; done = new Button("DONE");
boolean moreRecords = true; // Add the components to the Frame
add(numLabel);
// Initialize the Frame add(number);
public ReadStudentFile() add(nameLabel);
{ add(name);
// Setup the window add(markLabel);
public void setup()
add(marks);
{
add(next);
resize(400,200);
setLayout(new add(done);
GridLayout(4,2)); // Show the Frame
// Create the components of the Frame show();
// Open the file
Another graphical Input/Output
s = [Link]();
try {
d [Link]();
dis new DatalnputStream(new
[Link]([Link](n));
FilelnputStream("[Link]") [Link]([Link](s));
} [Link]([Link](d));
catch(IOException e) }
{ catch(IOException ioe) {
[Link]([Link]()); [Link]("IO ErrorN);
[Link](1); [Link](l);
} }
} }
// Read from the file // Closing the input file
public void readRecord() public void cleanup()
{ {
int n; try
String s; {
double d; [Link]();
try { }
n = [Link](); catch(IOException e) { }
}
Another graphical Input/Output
// Processing the event
public boolean action(Event event,Object o)
{
if([Link] instanceof Button)
if([Link]("NEXT"))
// Execute the program
readRecord(); public static void main (String
return true; args[])
} {
public boolean handleEvent(Event event) ReadStudentFile student =
{ new ReadStudentFile();
if([Link] instanceof Button) [Link]() ;
if ([Link] ("DONE") || moreRecords == }
false){ }
cleanup();
[Link](0);
return true;
}
return [Link](event);
}
Question to think…
• How Java helps programmers to develop
GUIs?
• How one can develop programs like Google?