当前位置: 代码迷 >> Java相关 >> java.io.EOFException
  详细解决方案

java.io.EOFException

热度:1025   发布时间:2007-06-25 22:46:12.0
java.io.EOFException

服务端:
public class t10_5_s extends JFrame {
private ServerSocket server;
private Socket sock;
private DataInputStream dis;
private DataOutputStream dos;
private FileInputStream fis;
private PrintWriter pw;

public t10_5_s()
{

try {
server=new ServerSocket(8888);
sock=server.accept() ;
dos=new DataOutputStream (sock.getOutputStream() );
dos.writeUTF( "ok!");
dis=new DataInputStream (sock.getInputStream() );
pw=new PrintWriter (new FileWriter("d1.txt"));
String str=null;
while(true){

str=dis.readUTF();
System.out.println(str);
pw.print(str);

}
} catch (IOException e1) {
e1.printStackTrace();
}finally{
pw.close();
try {
sock.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
public static void main(String[] args) {
new t10_5_s();
}
}
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at t10_5_s.<init>(t10_5_s.java:44)
at t10_5_s.main(t10_5_s.java:65)

客户端:
public class t10_5_c extends JFrame{
private Socket sock;
private DataInputStream dis;
private DataOutputStream dos;
private FileOutputStream fos;
private FileInputStream fis;
public t10_5_c()
{

try {
sock=new Socket("127.0.0.1",8888);
dis=new DataInputStream (sock.getInputStream() );
System.out.println(dis.readUTF());
fis=new FileInputStream("data.txt");
dos=new DataOutputStream(sock.getOutputStream());

int sum=fis.available();
int i=0;
while(i<sum){
if((sum-i) >100){
byte s[]=new byte[100];
fis.read(s,0,100);
String str=new String(s);
System.out.println(str);
dos.writeUTF( str);
}


else
{
byte s1[]=new byte[sum-i];
fis.read(s1,0,(sum-i));
String str=new String(s1);
System.out.println(str);
dos.writeUTF( str);

}
i+=100;

}



} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{try {
sock.close();
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}}

}

public static void main(String[] args) {
new t10_5_c();
}
}
红色部分是错误!想了好久没搞不懂!

搜索更多相关的解决方案: java  EOFException  

----------------解决方案--------------------------------------------------------
当然会出异常了

一个文件你一直读,读到最后,就抛出了这个异常

EOF就是End Of File的意义,表示已经到文件的末尾了
----------------解决方案--------------------------------------------------------
应该是服务端那里出错的吧!那样具体应该怎样解决!急!!!拜托了
----------------解决方案--------------------------------------------------------
干吗人影也不见多个啊!
----------------解决方案--------------------------------------------------------
while(true){

str=dis.readUTF();
System.out.println(str);
pw.print(str);}
死循环了?应该判断是否为文件的结尾?
----------------解决方案--------------------------------------------------------

谢谢你的回答!原因我也知道了.我不懂的是应该用什么方法或条件去判断...贴我都发了几天了....还是没人有办法吗?


----------------解决方案--------------------------------------------------------
while((str=dis.readUTF()!)=null){


System.out.println(str);
pw.print(str);

}
看看行不
----------------解决方案--------------------------------------------------------

遗憾!不行!


----------------解决方案--------------------------------------------------------
你应该去捕获那个异常,并处理它

另外,因为你发的是文本,你可以用BufferedReader里面的readLine方法啊

BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(xx.txt)));
String temp=null;
while((temp=br.readLine())!=null){

//把读到的temp发送过去
}
----------------解决方案--------------------------------------------------------
  相关解决方案