Record Class for Random Access File

Verified

Added on  2019/09/13

|2
|410
|256
Homework Assignment
AI Summary
This Java code defines a 'Record' class specifically designed for storing data in a Java RandomAccessFile. The class includes attributes for 'name' (a String with a maximum length of 16 characters) and 'id' (a String with 4 digits). It provides methods for reading and writing data to a RandomAccessFile, as well as getter methods for accessing the 'name' and 'id' attributes. The class also includes helper methods for reading and writing strings of specific lengths to the file. The 'toString' method provides a formatted string representation of the record's data. This class is useful for managing structured data within a random access file in Java.
Document Page
This class defines a record which is specifically for storing in
a Java random access file. See API of RandomAccessFile class in Java.
Attributes:
name - String, the max length is 16 chars
id - String, the number of digits in id is 4.
*
*/
import java.util.*;
import java.io.*;
public class Record
{
private String name;
private String id;
private int NAMELENGTH=16;
private int IDLENGTH=4;
public Record()
{
id = " ";
name = " ";
}
public Record(String newId, String newName)
{
id = newId;
name = newName;
}
//Read a 'name' and the related 'id' from the current position of
//the given file to the instance variables 'name' and 'id'
public void read(RandomAccessFile file) throws IOException
{
id = readString(file, IDLENGTH);
name = readString(file, NAMELENGTH);
}
//Write the value of 'this' object to the given file
public void write(RandomAccessFile file) throws IOException
{
writeStr(file, id, IDLENGTH);
writeStr(file, name, NAMELENGTH);
}
public String getId()
{
return id;
}
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
public String getName()
{
return name;
}
//Help method: read a string with length of strLength from the
file
// Return the string to the caller
private String readString (RandomAccessFile file, int strLength)
throws IOException
{
char[] chs = new char[strLength];
for (int i = 0; i < chs.length; i++)
{
chs[i] = file.readChar();
}
String theStr = new String(chs);
return theStr;
}
//Help method: write a string with length of strLength to the
file
private void writeStr( RandomAccessFile file, String str, int
strLength ) throws IOException
{
StringBuffer buffer = null;
if ( str != null )
buffer = new StringBuffer( str );
else
buffer = new StringBuffer( strLength );
buffer.setLength( strLength );
file.writeChars( buffer.toString() );
} // end method writeName
public String toString()
{
return "\nID: " + id + "\nName: " + name;
}
}
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]