Beginner Java Question

Hi there,

I'm trying to obtain the road speeds at various junctions on the M25 from the website below (bottom left table on the website).

I have all of the data I need in a scraped format (Junction & Mph) from the below website:

http://www.frixo.com/m25-anticlockwise.asp

Eg:
Seq 0
Junction 1
Mph 38

Seq 1
Junction 2
Mph 67

I now need to export the data to a file so to record the data on an automated basis however initially I'd simply like to be able to export the data once. I've tried using some tutorial code however have never worked with Java before and would really appreciate some help. The code I'm currently using looks like this:

FileWriter out = null;

try
{
session.log( "Writing data to a file." );

// Open up the file to be appended to.
out = new FileWriter( "junctions.txt", true );

// Write out the data to the file.
out.write( ( "Junction" ) + "\t" );
out.write( ( "MPH" ) + "\t" );

out.write( "\n" );

// Close up the file.
out.close();
}
catch( Exception e )
{
session.log( "An error occurred while writing the data to a file: " + e.getMessage() );
}

Where:
1. junctions.txt will be the output file
2. Junction is a token in Search scrapable file/Junction extractor pattern
3. MPH is a token in Search scrapable file/Junction extractor pattern
"(

~@Junction@~  ~@MPH@~mph

)"

The above gives me an output of just a list off simply Junction and MPH as per below with no data attached:

Junction MPH
Junction MPH
Junction MPH
Junction MPH
Junction MPH

Ideally I would like the above with the respective junction number and speed. Thanks in advance for any support available. If you need any more info please just ask as I'd really like to get this resolved asap.

Kind Regards,

Harcastle

Harcastle, Give this a

Harcastle,

Give this a try...

FileWriter out = null;

FileName = "junctions.txt";

try
{
session.log( "Writing data to a file." );

// Open up the file to be appended to.
File file = new File(FileName);

out = new FileWriter( FileName, true );

if(!file.exists())
{
        // Write out the data to the file.
        out.write( "Junction" + "\t" );
        out.write( "MPH" );
       
        out.write( "\n" );
}

//Where:
//1. junctions.txt will be the output file
//2. Junction is a token in Search scrapable file/Junction extractor pattern
//3. MPH is a token in Search scrapable file/Junction extractor pattern

out.write( dataRecord.get("Junction") + "\t" );
out.write( dataRecord.get("MPH") + "mph");

out.write( "\n" );

// Close up the file.
out.close();
}
catch( Exception e )
{
session.log( "An error occurred while writing the data to a file: " + e.getMessage() );
}

-Scott

Thanks Scott will give this a

Thanks Scott will give this a try