Hi Guys (and Girls),
Problem: Negative seek offset with respect to a RandomAccessFile's seek method.
private void writeRecord()
{
String v[] = inputUI.getValues();
raR = new RaRecord(Integer.parseInt(v[GeneralUI.RECORD]),
v[GeneralUI.TOOLNAME],
Integer.parseInt(v[GeneralUI.QUANTITY]),
Double.parseDouble(v[GeneralUI.COST]));
try {
file.seek(raR.getRecordNumber()-1 * RaRecord.size());
} catch (IOException e1) {
e1.printStackTrace();
}
try {
raR.write(file);
} catch (IOException e) {
System.err.println("IOException: writeRecord: raR.write(file)");
}
inputUI.clearFields();
}
}
This is the data portion of the Record object.
public class Record implements Serializable
{
private int recordNumber;
private String toolName;
private int quantity;
private double price;
In another class RaRecord which extends class Record I use the following code:
public static int size()
{
return 46;
}
private String padName(RandomAccessFile f) throws IOException
{
char ch[], temp;
ch = new char[15];
for (int i = 0; i < 15; i++)
{
temp = f.readChar();
ch[i] = temp;
}
return (new String(ch).replace('\0', ' '));
}
public void write(RandomAccessFile f) throws IOException
{
f.writeInt(getRecordNumber());
writeString(f, getToolName());
f.writeInt(getQuantity());
f.writeDouble(getPrice());
}
public void writeString(RandomAccessFile f, String str) throws IOException
{
StringBuffer buf;
if (str != null)
buf = new StringBuffer(str);
else
buf = new StringBuffer(15);
f.writeChars(buf.toString());
}
Please help. Negative Seek Offset. WTF is that?