首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

DataInputStream和DataOutputStream报出EOFException异常

2014-01-26 
DataInputStream和DataOutputStream报出EOFException异常:package src import java.io.DataInputStream i

DataInputStream和DataOutputStream报出EOFException异常:

package src;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class DataInputStreamTest {


public static void main(String[] args) throws Exception{
String fname = "D:\\Myworkspace\\IOTest\\src\\student.txt";
new Student("dawei").save(fname);
new Student("zhengyuan").save(fname);
Student.display(fname);
}
}
class Student{

static int count = 0;
int number = 1;
String name;
Student(){
this("");
}
Student(String name){
this.count++;
this.number = this.count;
this.name = name;
}
// 保存 数据
public void save(String fname)throws Exception{
File f = new File(fname);
f.createNewFile();
FileOutputStream fo = new FileOutputStream(fname,true);
DataOutputStream dout = new DataOutputStream(fo);
dout.writeInt(this.number);
dout.writeChar('\t');
dout.writeChars(this.name);
dout.writeChar('\n');
dout.close();
}
// 显示数据
public static void display(String fname)throws Exception{
FileInputStream fi = new FileInputStream(fname);
DataInputStream dfi = new DataInputStream(fi);
int i = dfi.readInt();
while(i!=-1){
System.out.println("i="+i);
char ch;
while ((ch = dfi.readChar())!= '\n'){  // 这句话报出异常
System.out.println(ch);
i = dfi.readInt();
}
}
dfi.close();
}

}

------解决方法--------------------------------------------------------
EOF:end of file

应该是你的while的判断条件的问题。

\n可能不行吧。换成\r\n看看
------解决方法--------------------------------------------------------
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class DataInputStreamTest {


public static void main(String[] args) throws Exception{
String fname = "D:\\student.txt";
new Student("dawei").save(fname);
new Student("zhengyuan").save(fname);
Student.display(fname);
}
}
class Student{

static int count = 0;
int number = 1;
String name;
Student(){
this("");
}
Student(String name){
this.count++;
this.number = this.count;
this.name = name;
}
// 保存 数据
public void save(String fname)throws Exception{
File f = new File(fname);
f.createNewFile();
FileOutputStream fo = new FileOutputStream(fname,true);
DataOutputStream dout = new DataOutputStream(fo);
dout.writeInt(this.number);
dout.writeChar( '\t');
dout.writeUTF(this.name);
dout.writeChar( '\n');
dout.close();
}
// 显示数据
public static void display(String fname)throws Exception{
FileInputStream fi = new FileInputStream(fname);
DataInputStream dfi = new DataInputStream(fi);
int i = dfi.readInt();
while(i!=-1){
System.out.print(dfi.readChar());
System.out.print(dfi.readUTF());
System.out.print(dfi.readChar());
try{
i = dfi.readInt();
}catch(Exception e){
break;
}
}
dfi.close();
}

}
------解决方法--------------------------------------------------------
i!=-1是用来判断是不是文件末尾的,i=-1说明是文件末尾。
------解决方法--------------------------------------------------------
建议你用对象的序列化来保存

------解决方法--------------------------------------------------------
对于readChar抛出EOFException异常,Java Doc中的解释是:
"EOFException if this input stream reaches the end before reading two bytes"

现结合你的程序分析一下抛出EOFException异常的原因:
1. int i = dfi.readInt();
2. while (i != -1) {
3. System.out.println("i=" + i);
4. char ch;
5.  while ((ch = dfi.readChar()) != '\n') { // 这句话报出异常
6.  System.out.println(ch);
7.  i = dfi.readInt();
8.  }
9. }
第一行readInt()是读取四个字节整数,也就是在save中写入的整数:
 

Java code        

热点排行