java.io.EOFException 每次读取 RandomAccessFile 时抛出
java.io.EOFException Thrown everytime I read a RandomAccessFile
所以我正在使用随机访问文件为学校项目创建游戏数据库。我很确定我掌握了基础知识并重新阅读了代码一百万次,我似乎无法找到正确读取随机访问文件字节有什么问题..我多次计算每个单词的值以加倍也检查...
我的主要方法:
public static void main(String[] args) {
RAFProcessing raf = new RAFProcessing();
try {
RandomAccessFile file = new RandomAccessFile(new File(
"src/FirstTry/database.txt"), "rw");
raf.writeRecord("Fifa", "EA", 2001, file);
raf.readRecord(file);
file.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
正在写入数据 + 中间的字节数:
public void writeRecord(String gameName, String publisher, int year, RandomAccessFile file) throws IOException
{
Long fileSize = file.length();
file.seek(fileSize);
file.writeUTF(gameName);
for(int loop=0; loop<20-gameName.length(); loop++ )
{
file.writeByte(20);
}
file.writeUTF(publisher);
for(int loop=0; loop<20-publisher.length(); loop++ )
{
file.writeByte(20);
}
file.writeInt(year);
}
SIZE 的值为 48 (22 + 22 + 4)
读取数据和字节:
public void readRecord(RandomAccessFile file) throws IOException
{
file.seek(0);
String wholeRecord ="";
int totalRecord = (int) (file.length()/SIZE);
System.out.print(totalRecord);
String gameName, publisher;
int year;
for(int loop=0; loop< totalRecord; loop++)
{
gameName = file.readUTF();
for(int innerloop=0; innerloop<20-gameName.length(); loop++ )
{
file.readByte();
}
publisher = file.readUTF();
for(int innerloop=0; innerloop<20-publisher.length(); loop++ )
{
file.readByte();
}
year = file.readInt();
wholeRecord += "|Game Name:" + gameName + " | Publisher: " + publisher + " | Year Published: " + year + "\n";
}
System.out.println( wholeRecord);
}
for (int innerloop = 0; innerloop < 20 - gameName.length(); loop++)
问题就在这里。应该是:
for (int innerloop = 0; innerloop < 20 - gameName.length(); innerloop++)
(两次)。
但是:
SIZE
是一个无效的假设。您不能假设字符串的 writeUTF()
表示使用的字节数与字符串中的字符数相同,长度字加上 2。
- 从编码的角度来看,在 写入字符串之前 填充字符串并在读取后 trim 填充字符串会更简单。但是您仍然不能假设每条记录的大小都相同。
- 包含
writeUTF()
和 writeInt()
数据的文件不是文本,不应具有 .txt
扩展名。
所以我正在使用随机访问文件为学校项目创建游戏数据库。我很确定我掌握了基础知识并重新阅读了代码一百万次,我似乎无法找到正确读取随机访问文件字节有什么问题..我多次计算每个单词的值以加倍也检查...
我的主要方法:
public static void main(String[] args) {
RAFProcessing raf = new RAFProcessing();
try {
RandomAccessFile file = new RandomAccessFile(new File(
"src/FirstTry/database.txt"), "rw");
raf.writeRecord("Fifa", "EA", 2001, file);
raf.readRecord(file);
file.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
正在写入数据 + 中间的字节数:
public void writeRecord(String gameName, String publisher, int year, RandomAccessFile file) throws IOException
{
Long fileSize = file.length();
file.seek(fileSize);
file.writeUTF(gameName);
for(int loop=0; loop<20-gameName.length(); loop++ )
{
file.writeByte(20);
}
file.writeUTF(publisher);
for(int loop=0; loop<20-publisher.length(); loop++ )
{
file.writeByte(20);
}
file.writeInt(year);
}
SIZE 的值为 48 (22 + 22 + 4)
读取数据和字节:
public void readRecord(RandomAccessFile file) throws IOException
{
file.seek(0);
String wholeRecord ="";
int totalRecord = (int) (file.length()/SIZE);
System.out.print(totalRecord);
String gameName, publisher;
int year;
for(int loop=0; loop< totalRecord; loop++)
{
gameName = file.readUTF();
for(int innerloop=0; innerloop<20-gameName.length(); loop++ )
{
file.readByte();
}
publisher = file.readUTF();
for(int innerloop=0; innerloop<20-publisher.length(); loop++ )
{
file.readByte();
}
year = file.readInt();
wholeRecord += "|Game Name:" + gameName + " | Publisher: " + publisher + " | Year Published: " + year + "\n";
}
System.out.println( wholeRecord);
}
for (int innerloop = 0; innerloop < 20 - gameName.length(); loop++)
问题就在这里。应该是:
for (int innerloop = 0; innerloop < 20 - gameName.length(); innerloop++)
(两次)。
但是:
SIZE
是一个无效的假设。您不能假设字符串的writeUTF()
表示使用的字节数与字符串中的字符数相同,长度字加上 2。- 从编码的角度来看,在 写入字符串之前 填充字符串并在读取后 trim 填充字符串会更简单。但是您仍然不能假设每条记录的大小都相同。
- 包含
writeUTF()
和writeInt()
数据的文件不是文本,不应具有.txt
扩展名。