Random Access Files - PowerPoint PPT Presentation

About This Presentation
Title:

Random Access Files

Description:

Sequential File Access. Sequential access. Data in files are accessed one item after another ... Because files can be very large a file pointer is of type ... – PowerPoint PPT presentation

Number of Views:1019
Avg rating:3.0/5.0
Slides: 30
Provided by: thaddeusf
Category:
Tags: access | file | files | random

less

Transcript and Presenter's Notes

Title: Random Access Files


1
Random Access Files
  • CSC 171 FALL 2004
  • LECTURE 23

2
Sequential 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

3
Random/Direct Access
  • The middle of the file can be
  • Retrieved
  • Modified
  • Rewritten
  • without reading/writing other data
  • Good for data base applications

4
(No Transcript)
5
(No Transcript)
6
  • In a ___________________ file access, a file is
    processed a byte at a time, in order.

7
  • In a ____sequential____ file access, a file is
    processed a byte at a time, in order.

8
  • ______________ access allow access at arbitrary
    locations in the file, without first reading the
    bytes preceding the access location.

9
  • __Random______ access allow access at arbitrary
    locations in the file, without first reading the
    bytes preceding the access location.

10
  • A file _____________ is a position in a
    random-access file.

11
  • A file ____pointer___________ is a position in a
    random-access file.

12
  • Because files can be very large a file pointer is
    of type ____________.

13
  • Because files can be very large a file pointer is
    of type ___long___.

14
File Structure
  • The key to random access is file structure
  • Most 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)

15
(No Transcript)
16
RandomAccessFile Class
  • RandomAccessFile raf
  • new RandomAccessFile(products.dat,rw)
  • File name
  • Mode
  • r for read only
  • rw for read write

17
Pointer 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 _at_ position 0

18
Example Reverse a file
  • Consider the problem of reversing a file with
    sequential access

19
  • RandomAccessFile raf
  • new RandomAccessFile(fileName,"rw")
  • last raf.length()
  • position last - SIZEOFCHAR
  • while (position gt 0)
  • raf.seek(position)
  • ch (char)raf.readByte()
  • System.out.print(ch"")
  • position position - SIZEOFCHAR
  • raf.close()

20
  • test.dat
  •  
  • This is a test.
  •  
  • OUTPUT
  •  
  • cd d/courses/CSC171/CSC171FALL2001/code/
  • d/devenv/jdk1.3/bin/javaw DisplayReversed
  •  
  • .tset a si sihT
  • Process DisplayReversed finished

21
Example Inventory
  • Inventory control (16 byte record)
  • Product ID code (int 4 bytes)
  • Quantity in stock (int 4 bytes)
  • Price (double 8 bytes)

22
  • // set up the keyboard for string input
  • InputStreamReader isr
  • new InputStreamReader(System.in)
  • BufferedReader br
  • new BufferedReader(isr)
  •  

23
  • for(int i 1 i lt 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)

24
Read Print the File
  • System.out.println(" Quantity")
  • System.out.println("ID. No. In Stock Price")
  • System.out.println("------- --------
    ------")
  •  
  • // read and print the data
  • for(int i 1 i lt 5 i)
  • acct raf.readInt()
  • amt raf.readInt()
  • price raf.readDouble()
  • System.out.println(" " acct " "
  • amt " " price)

25
OUTPUT
  • cd d/courses/CSC171/CSC171FALL2001/code/bronson/
  • d/devenv/jdk1.3/bin/javaw ReadRandom
  •  
  • Quantity
  • ID. 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

26
Modify The Database
  • Set up Keyboard
  • Open file
  • Loop as long as user wants to modify
  • Querry for ID number
  • Look up display quantity
  • Querry for modification
  • Write modified value
  • Close file

27
Loop querry ID
  • while (!acctstring.equals("999"))
  • recnum Integer.parseInt(acctstring) - BASEREC
  • position (recnum - 1) RECLEN

28
Move to the record
  • raf.seek(position)
  • acct raf.readInt()
  • //save loc ready to read/write amnt
  • setbytepos raf.getFilePointer()
  •  
  • amt raf.readInt()
  • System.out.println("The current quantity in stock
    is " amt)

29
UPDATE
  • System.out.print("Enter the new quantity ")
  • amtstring br.readLine()
  • amt Integer.parseInt(amtstring)
  • raf.seek(setbytepos) //reset loc
  • raf.writeInt(amt)
Write a Comment
User Comments (0)
About PowerShow.com