eofexceptionjava.io.EOFException这是个什么异常应该怎么解决

eofexception  时间:2021-09-02  阅读:()

出现java.io.EOFException 求教!!

你断点调试一下,你的构造 方法里面的if(fips.read()==-1)里面的语句是得不到执行的。

所以,那个文件里面根本没有写入空的List,你用readObject去取就会出错。

我运行了一下,断点后,发现了一个错误: 在你load之前,你首先实例化了一个output,可能这里出错了。

下面是正常运行的,你自己对比下: Person类我就没有贴了。

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; class TestFile { String f = null; FileInputStream fips = null; ObjectOutputStream oops = null; public TestFile() throws Exception { f = "d:" + File.separator + "resource.txt"; File fi = new File(f); if (!fi.exists()){ fi.createNewFile(); oops = new ObjectOutputStream(new FileOutputStream(fi)); List l = new ArrayList(); oops.writeObject(l); oops.flush(); oops.close(); } } public void save(Object o) throws Exception { @SuppressWarnings("unchecked") List per = (List) this.load(); per.add((Person) o); oops = new ObjectOutputStream(new FileOutputStream(f)); oops.writeObject(per); oops.close(); } public Object load() throws Exception { ObjectInputStream oips = new ObjectInputStream(new FileInputStream(f)); @SuppressWarnings("unchecked") List per2 = (List) oips.readObject(); oips.close(); return per2; } public static void main(String[] args) throws Exception { TestFile f = new TestFile(); f.save(new Person("小明", 12)); f.save(new Person("小红", 11)); } }

java程序运行时,出现EOFException异常,但是为什么我捕获后编译器报错??

顺序错了。

写成catch (EOFException e) { }catch(FileNotFoundException exc){ }catch( IOException ex){ } 就可以了。

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

错误修正: /* * 数据的读取 */ 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(); } } } }

java.io.EOFException这是个什么异常应该怎么解决

1,内部类BlockDataInputStream.的方法peekByte抛出的异常,意思就是到达了文件的末尾,程序却没有正常结束读取文件内容,你可以单步调试一下看看到达文件末尾之后程序为什么没有停下来 2,从异常来看,你是不是使用了一个内部类BlockDataInputStream?错误就在BlockDataInputStream.peekByte()方法. java.io.EOFException:当输入过程中意外到达文件或流的末尾时,抛出此异常。

此异常主要被数据输入流用来表明到达流的末尾。

TMTHosting:夏季优惠,美国西雅图VPS月付7折,年付65折,美国服务器95折AS4837线路

tmthosting怎么样?tmthosting家本站也分享过多次,之前也是不温不火的商家,加上商家的价格略贵,之到斯巴达商家出现,这个商家才被中国用户熟知,原因就是斯巴达家的机器是三网回程AS4837线路,而且也没有多余的加价,斯巴达家断货后,有朋友发现TMTHosting竟然也在同一机房,所以大家就都入手了TMTHosting家的机器。目前,TMTHosting商家放出了夏季优惠,针对VPS推...

GreenCloudVPS$20/年多国机房可选,1核@Ryzen 3950x/1GB内存/30GB NVMe/10Gbps端口月流量2TB

GreencloudVPS此次在四个机房都上线10Gbps大带宽VPS,并且全部采用AMD处理器,其中美国芝加哥机房采用Ryzen 3950x处理器,新加坡、荷兰阿姆斯特丹、美国杰克逊维尔机房采用Ryzen 3960x处理器,全部都是RAID-1 NVMe硬盘、DDR4 2666Mhz内存,GreenCloudVPS本次促销的便宜VPS最低仅需20美元/年,支持支付宝、银联和paypal。Gree...

Budgetvm12核心 16G 500 GB SSD 或者 2 TB SATA 10GB  20 TB  99美金

Budgetvm(原EZ机房),2005年成立的美国老品牌机房,主打美国4个机房(洛杉矶、芝加哥、达拉斯、迈阿密)和日本东京机房的独立服务器和VPS业务,而且不限制流量,默认提供免费的1800G DDoS防御服务,支持IPv6和IPMI,多种免费中文操作系统可供选择,独立服务器主打大硬盘,多硬盘,大内存,用户可以在后台自行安装系统等管理操作!内存可定制升级到1536G,多块硬盘随时加,14TBSA...

eofexception为你推荐
小型企业网络方案中小型企业适合哪几种网络营销方法连接池什么叫做“连接池机制”java队列java中的队列都有哪些,有什么区别?支付宝账单查询支付宝每个月的账单怎么查看支付宝账单查询支付宝账单怎么查0x800ccc0f您的服务器意外终止了连接。其可能原因包括服务器出错、网络出错或长时间处于非活动状态。 0x800CCC0F快照优化网站快照优化需要注意什么t320平板电脑三星 galaxy tab pro t320怎么样code查询怎么查code?乐辞清谈的含义及意义?
广西虚拟主机 国外免费域名网站 technetcal php探针 ubuntu更新源 免费ddos防火墙 彩虹ip 个人免费空间 秒杀预告 softbank邮箱 qq云端 香港新世界中心 linux使用教程 什么是web服务器 双线空间 汤博乐 japanese50m咸熟 sonya ncp是什么 apache启动失败 更多