Question

I am trying a create a .dat file that is split into 5 columns each with the heading, "timestamp", "deviceId", "testId", "Availability" and "Latency". This file is going to be pointed to a database where there are tests being ran at the moment.

The idea is that the data from these tests will be collected and stored into this file that I have just created.

I have been told to use and work with RandomAccessFile.

Only problem is I have no idea how to write a file through Java. I have made somewhat of an attempt but it is by no means much at all;

public long timestamp;
public int deviceId;
public int testId;
public byte availability;
public int latency;

public static void main (String [] args) throws FileNotFoundException
{
    String fileName = "hopeToJaysusThisWorks.dat";

    RandomAccessFile file = new RandomAccessFile(fileName, "rw"); 
}

As you can see, its not much at all!

I was told not to worry about getting into the technical side of things yet i.e the collection of data. At the moment all I need is the layout of each column and its 5 headings.

Can anyone help me or give me some advice on where to go from here, it would be much appreciated.

Était-ce utile?

La solution

Well the next step for you, would be to make sure the file you just tried to open for reading/writing (that's what the "rw" option means, if you give it just a r or just a w, it's for only reading, or only writing, respectively), is actually open/available. You can do this by simply running a check to see if your 'file' variable is null. Ok great! now you need to actually start writing some information into the file. This can take MANY forms, I recommend checking out the RandomaccessFile api to see exactly what methods are available, and what form you data has to be in to write it.

It'll look something like:

if (file != null){
    file.write(myByteArray[]);
}

now, you may have your data structured however, but that's the general gist of it.

Good luck!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top