java.io.EOFException怎么解决,同时文件怎么显示乱码

importjava.io.*;publicclassManageRecord{Filefile;publicstaticRecord[]Top=newRecord[50... import java.io.*;
public class ManageRecord {
File file;
public static Record[] Top = new Record[50];
public static int count = 0;
public static void main(String args[]){
ManageRecord man = new ManageRecord();
man.insert("x", 4);
man.insert("s", 7);
man.insert("c", 2);
man.insert("e", 5);
//man.insert("d", 12);
//man.insert("b", 11);
//man.insert("z", 15);
//man.insert("t", 11);
for(int i = 0; i < count; i++){
System.out.println(i+": "+Top[i].getSname()+" , "+Top[i].getOverTime());
}
try {
man.save();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ManageRecord(){
file = new File("D:/top.txt");
for(int i = 0; i < Top.length; i++){
Top[i] = new Record();
}
try {
load(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 数据的读取
* */
public void load(File file)throws Exception{
//文件输入流
FileInputStream inputStream = new FileInputStream(file);
//数据输入流
DataInputStream input = new DataInputStream(inputStream);
//读出
String tempname;
int temptime;
while( (tempname = (String)input.readUTF()) != null){
temptime = (int)input.readInt();
insert(tempname, temptime);
}
input.close();
}
/*
* 数据的保存
* */
public void save()throws Exception{
//文件输出流
FileOutputStream outputStream = new FileOutputStream(file);
//数据输出流
DataOutputStream output = new DataOutputStream(outputStream);
//ObjectOutputStream output = new ObjectOutputStream(outputStream);
//给top.txt这个文件写入数据
for(int i = 0; i < count; i++){
//output.writeObject(Top[i]);
output.writeUTF(Top[i].getSname());
output.writeInt(Top[i].getOverTime());
}
//关闭流
output.flush();
output.close();
for(int i = 0; i < count; i++){
Top[i] = new Record();
}
count = 0;
}
/*
* 插入数据到数组
* */
public void insert(String sname, int overtime){
if(count == 50){
if(Top[49].getOverTime() < overtime){
Top[49].setSname(sname);
Top[49].setOverTime(overtime);
}
}else{
Top[count] = new Record();
Top[count].setSname(sname);
Top[count].setOverTime(overtime);
count++;
}
sort();
}
/*
* 对数组排序
* */
public void sort(){
for(int i = 0; i < count; i++){
Record tempRecord = new Record();
int tempTime = Top[i].getOverTime();
int tempTop = i;
for(int j = i + 1; j < count; j++){
if(tempTime < Top[j].getOverTime()){
tempTime = Top[j].getOverTime();
tempTop = j;
}
}
if(tempTop != i){
tempRecord = Top[i];
Top[i] = Top[tempTop];
Top[tempTop] = tempRecord;
}}}}

运行的结果:
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at MoveGame.tools.ManageRecord.load(ManageRecord.java:57)
at MoveGame.tools.ManageRecord.<init>(ManageRecord.java:39)
at MoveGame.tools.ManageRecord.main(ManageRecord.java:11)
展开
 我来答
百度网友dd372b566
2013-03-19 · TA获得超过299个赞
知道小有建树答主
回答量:201
采纳率:0%
帮助的人:202万
展开全部
错误修正:

/*
* 数据的读取
*/
public void load(File file) {
// 文件输入流
FileInputStream inputStream;
DataInputStream input = null;
try {
inputStream = new FileInputStream(file);
// 数据输入流
input = new DataInputStream(inputStream);
// 读出
String tempname;
int temptime;
while (input.available() > 0 && (tempname = (String) input.readUTF()) != null) {
temptime = (int) input.readInt();
insert(tempname, temptime);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式