FILE STRUCTURES
Building a simple file sequentially
NOTE...every time you open a file for output, you destroy the
file if it already existed
FileOutputStream fileout;
// declare objects
DataOutputStream dataout;
fileout=new FileOutputStream("a:\\folder1\testfile");
dataout=new DartaOutputStream(fileout);
fot(int a=1;a<=100;a++)
{
dataout.writeInt(num);
// write int field 4 bytes
dataout.writeChar(genderCode);
// write a char field 2 bytes
dataout.writeUTF(address);
// write a string field of length x
// the number of character written
// is stored in a 2 byte field immediately
// in front of the string value written
/*
IMPORTANT the number of bytes written
to a file is indicated by the type of field written
eg, char,short,long,float, etc
*/
}
fileout.close();
***************************************************************
Reading Sequentially from a File
FileInputStream filein;
// declare objects
DataInputStream datain;;
filein=new FileOutputStream("a:\\folder1\testfile");
datain=new DartaOutputStream(filein);
fot(int a=1;a<=100;a++)
{
num=datain.readIntInt();
// read int field 4 bytes
genderCode=datain.readChar();
// read a char field 2 bytes
address=datain.readUTF();
// read a string field of length x
// the number of character to be read
// are stored in a 2 byte field immediately
// in front of the string value. This field
// is automatically used by the readUTF.
/*
IMPORTANT the number of bytes read
from a file is indicated by the type of fields
in the read commands. UTF fields (Strings)
are different because their length can vary.
That is why a control field is placed in front
of the actual value -- so the readUTF will
know how many bytes to read
eg, char,short,long,float, etc
*/
}
filein.close();
************************************************************
Random Access Files
RandomAccessFile test;
test = new RandomAccessFile("a://testfile","mode"); //
mode is r or w or rw
// for read, write, read/write
All the read and write methods discussed above still apply
to this class.
The main difference is that there is a seek method
that allows the programmer
to jump to wherever they want in the file to do a
read, write, or read/write.
Records must be of the same length in order to use a seek to go to a specific
record. The seek is followed by the number of bytes to jump to relative
the beginning of the file. The programmer must know the record size and have
an understanding of the data structure of the records to use this class
properly.
ALL UTF fields have a 2 byte field in front of them telling
the compiler
the length of the string in the file. This is very important. It is figured into
the record size automatically when you do a seek. The records below are
28 bytes of data plus 2 for the hidden field --- therefore each record is really 30 bytes in length. Notice that the name was padded to a size of 20
I will explain this in detail in class.
EXAMPLE
// example of building a file sequentially and coming back
and
// opening it as a RandomAccessFile ofject and using a seek to get to the 3rd
record
// Each record was 30 bytes in length
import javax.swing.UIManager;
import java.io.*;
import javax.swing.*;
public class Application1 {
FileOutputStream fileOut;
DataOutputStream dataOut;
RandomAccessFile qq;
public Application1() {
try{
fileOut= new FileOutputStream("M://SAMPLEFILE");
dataOut= new DataOutputStream(fileOut);
for(int a=1; a<=25; a++){
dataOut.writeUTF ("Robert Rotruck
"
dataOut.writeInt (21);
dataOut.writeChar ('W');
dataOut.writeShort (120);
}
fileOut.close ();
qq = new RandomAccessFile("M://SAMPLEFILE", "r");
qq.seek (60);
String s= qq.readUTF ();
JOptionPane.showMessageDialog(null, s);
qq.close();
}
catch(IOException e){
}
}
public static void main(String[] args) {
new Application1();
System.exit (0);
}
}