Re: Reading from a Random Access File




christopher_board wrote:
Hi and thanks for your help. I used the code that you provided me and
it comes up with the following output messages. I have changed the
system.out a little to make it clear for me what information was being
outputted to the screen.

This is what got outputted when I ran the program

the length of the file6

the file pointer is located at 0

the file pointer is now located at 2

the first byte read is: 6

the file pointer has now been moved to 3

the file pointer has been moved 6

The read error is: java.io.EOFException


Thanks for your help in this matter


OK, although I'm still not sure what you're attempting.
Perhaps this is closer to what you're looking for.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class finalTest
{
public static void main(String[] args)
{
writeData();
readData();
}

/**
* Since you haven't shown us the contents of your file, I'll make one up
here.
* It consists of lines of characters. Each line starts with a character
* representation of an integer, which represents the room number.
Followed by
* a space character used as a field delimiter. Followed by a character
* representation of an integer which represents the number of computers
in
* the room. Followed by a newline character(s), used as a record
delimiter.
* (Platform dependant.)
*/
public static void writeData()
{
File f = new File("Class Room Details.rsh");
try
{
FileWriter fw = new FileWriter(f);
int[] roomNumber = new int[]
{ 101, 102, 103, 104, 105 };
int[] computerCount = new int[]
{ 1, 2, 3, 4, 5 };
StringBuilder fileContents = new StringBuilder();
for (int loopIndex = 0; loopIndex < roomNumber.length; loopIndex++)
{
fileContents.append(roomNumber[loopIndex]);
fileContents.append(" ");
fileContents.append(computerCount[loopIndex]);
fileContents.append("\n");
}
fw.write(fileContents.toString());
fw.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}

/**
* I haven't understood why you insist on a RandomAccessFile and using
* readByte() and skipByte() so I didn't attempt to use them here,
especially
* since you haven't provided a clue as to what your file format is.
*/
public static void readData()
{
String dataLine = null;
int roomNumber;
int computerCount;
try
{
File f = new File("Class Room Details.rsh");
RandomAccessFile raf = new RandomAccessFile(f, "r");
System.out.println("The length of the file is: " + raf.length()
+ " bytes.");
System.out.println("the file pointer is located at: "
+ raf.getFilePointer());
while (raf.getFilePointer() < raf.length())
{
dataLine = raf.readLine();
String[] data = dataLine.split(" ");
roomNumber = Integer.valueOf(data[0]);
computerCount = Integer.valueOf(data[1]);
shutdownComputers(roomNumber, computerCount);
}
raf.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

static void shutdownComputers(int room, int count)
{
System.out.println("Shutdown " + count + " computers in Room " + room
+ ". Bye");
}
}


.



Relevant Pages

  • Re: Read Backwards in a text file
    ... much use in this regard. ... The problem with moving the file pointer in a text file is that you want to move to a specific character in the file, but the file pointer doesn't know about characters, it only knows about bytes. ... You could create your own line based stream reader that uses the fact that the line break is of a known size regardless of the encoding used. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: fwrite problem
    ... > I am updating a file by inserting date in it as a character string. ... i seek the file pointer to the proper ...
    (microsoft.public.vc.mfc)
  • Re: Offset by 1 in CStdiofile Seek while reading 0xa
    ... I am trying to read the file character by character until I encounter ... CStdiofile and Seek; ... where nByte = 1 ... file pointer is positioned at G. ...
    (microsoft.public.vc.language)
  • Re: fwrite problem
    ... This character string is part of a structure and i am rewriting the whole structure into the file. ... Before updating the file, i seek the file pointer to the proper postion and also after updating the file, the file pointer is pointing to the overwritten character, next to structure. ...
    (microsoft.public.vc.language)
  • Re: EOL Detection (Anyone?)
    ... I just posted another reply to Simon where I suggested the number of blank ... Also the reason i used fgetc is that it keeps track of the file pointer, ... next time the function is called it carries on reading chars from where it ... int GetLine ...
    (microsoft.public.vc.language)