Home > Archive > Java Help > June 2007 > Reading from a Random Access File
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Reading from a Random Access File
|
|
| christopher_board@yahoo.co.uk 2007-06-23, 4:19 am |
| 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
| |
|
| christopher_board@yahoo.co.uk 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
Do you have a question?
If so, do you have information to help us know what the question is, or how to
help? Such as, say, an SSCCE (Simple, Self-Contained Correct (compilable0
Example) that displays the behavior in question, and an actual *copied* and
*pasted* error message?
If "no" to either of those, thank you for your somewhat tangential information.
--
Lew
| |
| Lothar Kimmeringer 2007-06-23, 4:19 am |
| christopher_board@yahoo.co.uk wrote:
> This is what got outputted when I ran the program
>
> the length of the file6
[...]
> the file pointer has been moved 6
>
> The read error is: java.io.EOFException
Reading the seventh byte from a file of six bytes length
always lead to this kind of behavior.
Regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!
| |
| Jeff Higgins 2007-06-23, 4:19 am |
|
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");
}
}
| |
| Jeff Higgins 2007-06-23, 4:19 am |
| Here's yet another.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Random;
/**
* Alternating Byte Format
*
* Each (two byte) record consists
* of two (one byte) fields.
*
* Field 1 RoomNumber.
* Field 2 ComputerCount.
*
* Each field will represent an
* integer value between 0-255.
*
*/
public class RandomAccess
{
public static void main(String[] args)
{
writeData();
readData();
}
static void writeData()
{
File f = new File("Class Room Details.abf");
Random rnd = new Random();
byte[] fileContents = new byte[100];
rnd.nextBytes(fileContents);
try
{
FileOutputStream fos = new FileOutputStream(f);
fos.write(fileContents);
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
static void readData()
{
int roomNumber;
int computerCount;
try
{
File f = new File("Class Room Details.abf");
RandomAccessFile raf = new RandomAccessFile(f, "r");
while (raf.getFilePointer() < raf.length())
{
roomNumber = raf.readByte() & 0xFF;
computerCount = raf.readByte() & 0xFF;
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 + ".\tBye");
}
}
| |
|
|
| rhino 2007-06-27, 10:09 pm |
|
<christopher_board@yahoo.co.uk> wrote in message
news:1182511375.974898.237690@w5g2000hsg.googlegroups.com...
> 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
>
This question appears to be a followup to something you asked in an earlier
thread. It would make much more sense if you posted the followup question in
the ORIGINAL thread, where people already have the context, than to start a
new thread where people don't know what you're talking about.
If you MUST start a new thread - because the old one has gone cold for
whatever reason - you will need to give us the background of what your
problem is. Very few people will be bothered to try to find the original
thread to figure out what the background is.
--
Rhino
|
|
|
|
|