random access files csc 171 fall 2001 lecture 19
Post on 15-Jan-2016
216 views
TRANSCRIPT
![Page 1: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/1.jpg)
Random Access FilesRandom Access Files
CSC 171 FALL 2001
LECTURE 19
![Page 2: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/2.jpg)
History: History: Douglas EngelbartDouglas Engelbart Developed NLS @ SRI -
an exploratory vehicle for research into the "knowledge worker/organization."
first hypertext system First video conferencing 1964 the "mouse" The concept of windows,
2D editing, etc. Engelbart received the
IEEE Computer Society Pioneer Award in 1992.
![Page 3: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/3.jpg)
Denning ArticleDenning Article
The concept of a “knowledge worker”In the context of the “information age”In relation to the 4 IT “dilemmas”
– Skills– Breadth vs. Depth– Design– Licensing
![Page 4: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/4.jpg)
Sequential File AccessSequential File Access
Sequential access– Data in files are accessed one item after another– The 4th item cannot be read without reading the
first 3 items– Imagine updating the 1000000th item and then
updating the 999999th
![Page 5: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/5.jpg)
Random/Direct AccessRandom/Direct Access
The middle of the file can be – Retrieved– Modified– Rewritten
without reading/writing other dataGood for data base applications
![Page 6: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/6.jpg)
![Page 7: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/7.jpg)
![Page 8: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/8.jpg)
File StructureFile Structure
The key to random access is file structureMost commonly
– Fixed length records consisting of Fixed length items
Example: Inventory control (16 byte record)– Product ID code (int – 4 bytes)– Quantity in stock (int – 4 bytes)– Price (double – 8 bytes)
![Page 9: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/9.jpg)
![Page 10: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/10.jpg)
RandomAccessFile ClassRandomAccessFile Class
RandomAccessFile raf = new RandomAccessFile(“products.dat”,”rw”);
– File name– Mode
“r” for read only “rw” for read & write
![Page 11: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/11.jpg)
Pointer PositionPointer Position
Each random access stream establishes an internal pointer position
The pointer keeps track of where the next byte is to be accessed
The seek(long i) method permits the programmer to move to any byte position– 1st byte @ position 0
![Page 12: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/12.jpg)
Example: Reverse a fileExample: Reverse a file
Consider the problem of reversing a file with sequential access
![Page 13: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/13.jpg)
RandomAccessFile raf = new RandomAccessFile(fileName,"rw");
last = raf.length();
position = last - SIZEOFCHAR;while (position >= 0) { raf.seek(position); ch = (char)raf.readByte(); System.out.print(ch+"|"); position = position - SIZEOFCHAR;}raf.close();
}}
![Page 14: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/14.jpg)
test.dat
This is a test.
OUTPUT
cd d:/courses/CSC171/CSC171FALL2001/code/
d:/devenv/jdk1.3/bin/javaw DisplayReversed
.|t|s|e|t| |a| |s|i| |s|i|h|T|
Process DisplayReversed finished
![Page 15: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/15.jpg)
Example: InventoryExample: Inventory
Inventory control (16 byte record)– Product ID code (int – 4 bytes)– Quantity in stock (int – 4 bytes)– Price (double – 8 bytes)
![Page 16: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/16.jpg)
// set up the keyboard for string input
InputStreamReader isr =
new InputStreamReader(System.in);
BufferedReader br =
new BufferedReader(isr);
![Page 17: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/17.jpg)
for(int i = 1; i <= 5; i++)
{
System.out.print("Enter the identification number: ");
acctstring = br.readLine();
acct = Integer.parseInt(acctstring);
raf.writeInt(acct);
System.out.print("Enter the quantity in stock: ");
amtstring = br.readLine();
amt = Integer.parseInt(amtstring);
raf.writeInt(amt);
System.out.print("Enter the price: ");
pricestring = br.readLine();
price = Double.parseDouble(pricestring);
raf.writeDouble(price);
}
![Page 18: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/18.jpg)
Read & Print the FileRead & Print the FileSystem.out.println(" Quantity");
System.out.println("ID. No. In Stock Price");
System.out.println("------- -------- ------");
// read and print the data
for(int i = 1; i <= 5; i++){
acct = raf.readInt();
amt = raf.readInt();
price = raf.readDouble();
System.out.println(" " + acct + " "
+ amt + " $" + price);
}
![Page 19: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/19.jpg)
OUTPUTOUTPUTcd d:/courses/CSC171/CSC171FALL2001/code/bronson/d:/devenv/jdk1.3/bin/javaw ReadRandom QuantityID. No. In Stock Price------- -------- ------ 1001 476 $28.35 1002 240 $32.56 1003 517 $51.27 1004 284 $23.75 1005 165 $32.25 Process ReadRandom finished
![Page 20: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/20.jpg)
Modify The DatabaseModify The DatabaseSet up KeyboardOpen fileLoop as long as user wants to modify
– Querry for ID number– Look up & display quantity– Querry for modification– Write modified value
Close file
![Page 21: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/21.jpg)
Loop & querry IDLoop & querry ID
while (!acctstring.equals("999")) {
recnum = Integer.parseInt(acctstring) - BASEREC;
position = (recnum - 1) * RECLEN;
![Page 22: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/22.jpg)
Move to the recordMove to the recordraf.seek(position); acct = raf.readInt();
//save loc ready to read/write amntsetbytepos = raf.getFilePointer(); amt = raf.readInt();System.out.println("The current quantity in stock is: " + amt);
![Page 23: Random Access Files CSC 171 FALL 2001 LECTURE 19](https://reader036.vdocument.in/reader036/viewer/2022070412/56649d4b5503460f94a2822b/html5/thumbnails/23.jpg)
UPDATEUPDATE
System.out.print("Enter the new quantity: ");
amtstring = br.readLine();
amt = Integer.parseInt(amtstring);
raf.seek(setbytepos); //reset loc
raf.writeInt(amt);