Reading in chunks of data

P

Paul.Lee.1971

Hi,
I'm reading up on the use of the RandomAccessFile (and other file I/O)
classes in Java but I haven't found anything that can help me. Is
there any way to read in data structured in a text file, as follows:

Apr 10
"The Book of the Dead"
£17.95
Apr 13
"Unconvention Highlights"
£4.99
Apr 14
"Miserable Quotients"
£0.02

etc. etc.

So that I can do the following (in pseudo code):
read first three lines: first line assigned to date value, second to
book title, third to cost variable
read next three lines: first line assigned to date value, second to
book title, third to cost variable
etc.

The only other way I thought about doing it is to read in the file
line-by-line, but, by using line counting
and some arithmetic with modulo operators (?), know that the 1st, 4th,
7th lines are date values,
lines 2, 5 and 8 are titles etc., but this seems fiddly. There must be
an easy way to do this...

With best wishes

Paul
 
A

Arved Sandstrom

Hi,
I'm reading up on the use of the RandomAccessFile (and other file I/O)
classes in Java but I haven't found anything that can help me. Is
there any way to read in data structured in a text file, as follows:

Apr 10
"The Book of the Dead"
£17.95
Apr 13
"Unconvention Highlights"
£4.99
Apr 14
"Miserable Quotients"
£0.02

etc. etc.

So that I can do the following (in pseudo code):
read first three lines: first line assigned to date value, second to
book title, third to cost variable
read next three lines: first line assigned to date value, second to
book title, third to cost variable
etc.
[ SNIP ]

*********************************

Try java.util.Scanner

AHS
 
S

Stefan Ram

lines 2, 5 and 8 are titles etc., but this seems fiddly. There must be
an easy way to do this...

You have not even mentioned the need to properly handle all
the possibly I/O and encoding exceptions that also might be
thrown when reading from a file. Welcome to computer programming!

You could use property files or XML, because there already are
customized readers for those types of data that will return
a data str^H^H^H^H^H^H^H^H^H^Hn object with all the data from
the file. But these require that you rewrite your text file to
such a format as the property file format or XML.

(The rest of this post is self-advertising.)

There also is the library »ram.jar«, with support for reading
data structures from a text file.

For this, the source file »source.uno« needs to be in the
format »Unotal«. For example, your file might look as follows.

< < date=< month=Apr day=10 >
name=[The Book of Programming]
price=< unit=[£] value=17.95 >>

< date=< month=Apr day=11 >
name=[Unconvention Highlights]
price=< unit=[£] value=4.99 >>

< date=< month=Apr day=12 >
name=[Miscellaneous Quotations]
price=< unit=[£] value=0.02 >>>

Then, reading and parsing is as easy as

room( new File( "source.uno" ))

A complete example program:

import java.io.File;
import de.dclj.ram.notation.unotal.Room;
import static de.dclj.ram.notation.unotal.RoomFromModule.room;

public final class Main
{ final static java.lang.String nl =
java.lang.System.getProperty( "line.separator" );

public static void main( final java.lang.String argv[] )
{ final Room room = room( new File( "source.uno" ));

java.lang.System.out.println
( room.get( 1 )+ nl +
room.getRoom( 1 ).get( "price" )+ nl +
room.getRoom( 1 ).getRoom( "price" ).get( "value" )); }}

< date =< day =11 month =Apr >name =[Unconvention Highlights] price =< unit =[£] value =4.99 >>
< unit =[£] value =4.99 >
4.99

http://www.purl.org/stefan_ram/pub/junotal_tutorial
 
S

Stefan Ram

You have not even mentioned the need to properly handle all
the possibly I/O and encoding exceptions that also might be
thrown when reading from a file. Welcome to computer programming!

I have made up the following example program to teach how
to read the first line of a text file »example.txt«.

It looks somewhat bloated, because I have take extra care to
encapsulate the handling of each resource type within a
dedicated method and because I wanted to specify in detail the
possible exceptions thrown. While this increases the number of
lines (characters) of the source file, I believe the resulting
structure to be more elegant (readable, maintainable, ...).

I also follow the advice not to use the convenience class
»FileReader«, because it usually is more robust to specify an
encoding than to rely on an unknown default encoding.
FileReader still has no constructor with an encoding parameter.
Here, I assume the encoding of the source file to be »UTF-8«.

public class Main
{
public static void main( final java.lang.String[] args )
throws
java.io.FileNotFoundException,
java.io.UnsupportedEncodingException,
java.io.IOException
{ final java.lang.String sourceFileName = "example.txt";
printFirstLineOf( sourceFileName ); }

public static void printFirstLineOf
( final java.lang.String sourceFileName )
throws
java.io.FileNotFoundException,
java.io.UnsupportedEncodingException,
java.io.IOException
{ final java.io.File sourceFile
= new java.io.File( sourceFileName );
printFirstLineOf( sourceFile ); }

public static void printFirstLineOf
( final java.io.File sourceFile )
throws
java.io.FileNotFoundException,
java.io.UnsupportedEncodingException,
java.io.IOException
{ /* throws java.io.FileNotFoundException */
final java.io.FileInputStream fileInputStream
= new java.io.FileInputStream( sourceFile );
try
{ printFirstLineOf( fileInputStream ); }
finally
{ fileInputStream.close(); }}

public static void printFirstLineOf
( final java.io.FileInputStream fileInputStream )
throws
java.io.UnsupportedEncodingException,
java.io.IOException
{ /* throws java.io.UnsupportedEncodingException */
final java.io.InputStreamReader inputStreamReader
= new java.io.InputStreamReader( fileInputStream, "UTF-8" );
try
{ printFirstLineOf( inputStreamReader ); }
finally
{ inputStreamReader.close(); }}

public static void printFirstLineOf
( final java.io.InputStreamReader inputStreamReader )
throws java.io.IOException
{ final java.io.BufferedReader bufferedReader
= new java.io.BufferedReader( inputStreamReader );
try
{ printFirstLineOf( bufferedReader ); }
finally
{ bufferedReader.close(); }}

public static void printFirstLineOf
( final java.io.BufferedReader bufferedReader )
throws java.io.IOException
{ /* throws java.io.IOException */
final java.lang.String line = bufferedReader.readLine();
java.lang.System.out.println( line ); }}
 
T

Tom Anderson

I'm reading up on the use of the RandomAccessFile (and other file I/O)
classes in Java but I haven't found anything that can help me. Is
there any way to read in data structured in a text file, as follows:

Apr 10
"The Book of the Dead"
£17.95
Apr 13
"Unconvention Highlights"
£4.99
Apr 14
"Miserable Quotients"
£0.02

etc. etc.

So that I can do the following (in pseudo code):
read first three lines: first line assigned to date value, second to
book title, third to cost variable
read next three lines: first line assigned to date value, second to
book title, third to cost variable
etc.
[...]

That seems an entirely plausible outline. In what way
does it fail to meet your needs?

BufferedReader reader = new BufferedReader(...);
while (true) {
String date = reader.readLine();
if (date == null) break; // end of file
String title = reader.readLine();
String cost = reader.readLine();
Thing thing = new Thing(date, title, cost);
doSomethingWith(thing);
}

My guess would be thast Paul hadn't thought of doing multiple readLines
inside the loop. I imagine he was thinking along the lines of:

int lineNumber = 0 ;
String date = null ;
String title = null ;
String price = null ;
while (true) {
String line = reader.readLine() ;
switch (lineNumber % 3) {
case 0 : {
date = line ;
} break ;
case 1 : {
title = line ;
} break ;
case 2 : {
price = line ;
Book book = new Book(date, title, price) ;
doSomethingWith(book) ;
} break ;
default: {
throw new IllegalStateException() ;
}
}
++lineNumber ;
}

Which is indeed a bit fiddly.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top