为何会出现异常,请高手指教!

shunde_saint 2004-08-11 05:12:00
写入数据:
import java.io.*;

public class Write5000Double {
public static void main(String[] args) {
double d;
try {
FileOutputStream fos = new FileOutputStream("e:\\temp\\abc");
DataOutputStream dos = new DataOutputStream(fos);
for (int i = 0; i < 5000; i++) {
//double randy=Math.random()*200;
dos.writeDouble(Math.random() * 200);
}
dos.close();
fos.close();
}
catch (IOException x) {
System.out.println("Caught IOException");
}
}
}

读文件:
import java.io.*;

public class Read5000Double3 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("e\\temp\\abc");
DataInputStream dis = new DataInputStream(fis);
boolean readBad = false;
while (true) {
double b = dis.readDouble();
if (b == -1) {
break;
}
if (b < 0 || b > 200) {
readBad = true;
System.out.println("Read bad double:" + b);
}
}
if (!readBad) {
System.out.println("File is valid!");
}
dis.close();
fis.close();
}
catch (IOException x) {
System.out.println("Caught IOException!");
}
}
}
何解每次运行都是:Caught IOException!请不吝赐教!
...全文
137 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
drugon 2004-08-12
  • 打赏
  • 举报
回复
我试了你上面的程序的,两个流的关闭没有什么问题,其问题是在你创建的文件流读文件时出现了问题,也就是文件流在读完文件的地方出现了问题。根据我测试的结果是:DataInputStream dis 这个文件流用dis.readDouble();这个方法遇一文件尾并不是返回-1,不信你在你的 if (a == -1)
{
break; //当b值等于-1的时候,到达文件末尾,跳出循环。
}
里面加上一个输出语句,一定看不到这个输出语句的。就是因为文件流遇到文件尾,你没有处理好,所以它就抛出异常java.io.EOFException。
shunde_saint 2004-08-12
  • 打赏
  • 举报
回复
ntzls(三星堆),你好!我模仿你写的类,把它修改了少少,可能会更易理解一点,现呈上,请指教:
import java.io.*;

public class Read5000Double3 {
public static void main(String[] args) {
int n = 0;
//n是计数器,统计有多少个double浮点数。
try {
FileInputStream fis = new FileInputStream("e:temp\\efg");
DataInputStream dis = new DataInputStream(fis);
boolean readBad = false;
while (true) {
//当括号里的值为真时,执行循环。
double b = dis.readDouble();
n += 1;
if (b == -1) {
break;
//当b值等于-1的时候,到达文件末尾,跳出循环。
}
if (b < 0 || b > 200) {
readBad = true;
System.out.println("Read bad double:" + b);
}
}
if (!readBad) {
System.out.println("File is valid!");
}
dis.close();
fis.close();
}
catch (EOFException x) {
System.out.println("READ OVER,It's " + n + " Double float!");
}
catch (IOException x) {
System.out.println("Caught IOException!");
}
}
}

另外,我还有一问题,如果我这样修改为何会出现很多小于0大于200的双精度浮点数,而且n值等于4444
try {
FileInputStream fis = new FileInputStream("e:temp\\efg");
DataInputStream dis = new DataInputStream(fis);
boolean readBad = false;
while (true) {
//当括号里的值为真时,执行循环。
double b = dis.readDouble();
n += 1;
int a = fis.read();
if (a == -1) {
break;
//当b值等于-1的时候,到达文件末尾,跳出循环。
}
if (b < 0 || b > 200) {
readBad = true;
System.out.println("Read bad double:" + b);
}
}
if (!readBad) {
System.out.println("File is valid!");
}
dis.close();
fis.close();
}

还请不吝赐教!
shunde_saint 2004-08-12
  • 打赏
  • 举报
回复
ntzls(三星堆),你好!按你所说readDouble();从流中读取8个字节转为double。DataInputStream无法也不可能识别流中存放的是否是double,你要readDouble()他就按顺序读8个字节再转换成一个double。但为何
for (int i = 0; i < 5000; i++) {
double randy = dos.readDouble();
if (randy < 0 || randy > 200) {
readBad = true;
System.out.println("Read bad double:" + randy);
}
}
却可以正常运行,结果是File is valid!
ntzls 2004-08-12
  • 打赏
  • 举报
回复
1、如何判断文件尾,dis.readDouble();读不到8个字节即视为到达文件尾,通过捕获EOFException异常进行判断。
catch (EOFException x) {
System.out.println("READ OVER,It's " + n + " Double float!");
}
2、4444问题
int a = fis.read();
此句又从流中读取了1个字节
5000*8/(8+1)=4444
shunde_saint 2004-08-12
  • 打赏
  • 举报
回复
应该如何判定到达文件的末尾?
ntzls 2004-08-11
  • 打赏
  • 举报
回复
readDouble();从流中读取8个字节转为double。DataInputStream无法也不可能识别流中存放的是否是double,你要readDouble()他就按顺序读8个字节再转换成一个double。把abc换成其他文件试试即知。

import java.io.*;
public class Read5000Double {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("abc");
DataInputStream dis = new DataInputStream(fis);
boolean readBad = false;
while (true) {
double b = dis.readDouble();
if(false)break;
System.out.println("Read bad double:" + b);
}
dis.close();
fis.close();
}
catch (EOFException x) {
System.out.println("READ OVER");
}
catch (IOException x) {
System.out.println("Caught IOException!");
}
}
}
shunde_saint 2004-08-11
  • 打赏
  • 举报
回复
采用while语句运行时的结果
Read bad double:-1.2298337816426245E-73

Read bad double:-5.934776981882343E242

Read bad double:1.241220292531919E184

Read bad double:-2.10435012526134528E18

Read bad double:-1.7556877611751508E170

Read bad double:-3.2030593332044954E-37

Read bad double:-1.7623152480556113E-96

Read bad double:-9.461322678971958E-274

Read bad double:1.310177202917745E193

Read bad double:9.484424003409213E118

Read bad double:-3.312297128942324E46

Read bad double:6.562281871060984E276

Read bad double:1.2661998951151956E300

Read bad double:2.0400828182457754E59

Read bad double:1.7447096939025064E144

Read bad double:-1.7976896025470867E-278

Read bad double:2.0141363993386725E273

Caught IOException!
何解?????????????????
shunde_saint 2004-08-11
  • 打赏
  • 举报
回复
iversonxk(艾弗森),我把while改成for循环程序可以运行,也没有抛出错误,但偏离我编写这个程序的想法,我假设不知道有多少个双精度浮点数在文件里。我也试过把while(true)改成boolean a=true;
while(a){
double b = dis.readDouble();
if (b == -1) {
a=false;}
运行时仍然抛出IOException。顺便补充一句,读文件的代码中,FileInputStream fis = new FileInputStream("e\\temp\\abc");这句有错,应该差一冒号,但修改以后仍然有IOException抛出。

下面是for循环语句的代码:

import java.io.*;

public class Read5000Double {
public static void main(String[] args) {
try {
FileInputStream fos = new FileInputStream("f:temp\\efg");
DataInputStream dos = new DataInputStream(fos);
boolean readBad = false;
for (int i = 0; i < 5000; i++) {
double randy = dos.readDouble();
if (randy < 0 || randy > 200) {
readBad = true;
System.out.println("Read bad double:" + randy);
}
}
if (!readBad) {
System.out.println("File is vald!");
}
}
catch (IOException x) {
System.out.println("Caught IOException!");
}
}
}

恳请高手之指教!
iversonxk 2004-08-11
  • 打赏
  • 举报
回复
但无论如何也不能写while(true)
不信你把这句改改运行绝对没问题
shunde_saint 2004-08-11
  • 打赏
  • 举报
回复
qazxsw1982103(波斯猫),按照你所说把fos.close()与fis.close()屏蔽了,还是抛出IOException,和解?
qazxsw1982103 2004-08-11
  • 打赏
  • 举报
回复
fis.close();也是多余的。
qazxsw1982103 2004-08-11
  • 打赏
  • 举报
回复
import java.io.*;

public class Write5000Double {
public static void main(String[] args) {
double d;
try {
FileOutputStream fos = new FileOutputStream("e:\\temp\\abc");
DataOutputStream dos = new DataOutputStream(fos);
for (int i = 0; i < 5000; i++) {
dos.writeDouble(Math.random() * 200);
}
dos.close();
fos.close();
}
catch (IOException x) {
System.out.println("Caught IOException");
}
}
}

读文件:
import java.io.*;

public class Read5000Double3 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("e\\temp\\abc");
DataInputStream dis = new DataInputStream(fis);
boolean readBad = false;
while (true) {
double b = dis.readDouble();
if (b == -1) {
break;
}
if (b < 0 || b > 200) {
readBad = true;
System.out.println("Read bad double:" + b);
}
}
if (!readBad) {
System.out.println("File is valid!");
}
dis.close();
fis.close();
}
catch (IOException x) {
System.out.println("Caught IOException!");
}
}
}
----------------------------------------

我没有进行测试,不过问题应该处在你的fos.close();
因为在你关闭外包装流dos.close();的同时,底层的操作系统会释放流对象,这个流对象对应
你程序中的fos,所以你的fos.close();是多余的。

shunde_saint 2004-08-11
  • 打赏
  • 举报
回复
Read5000Double3类中我想如果到达文件的结尾(double b = dis.readDouble();)时,b的值是-1,然后跳出循环!我觉得好像有点问题,但不知问题错在哪里?


高手啊,你们在哪里啊?
iversonxk 2004-08-11
  • 打赏
  • 举报
回复
能将while写成这样吗 :while (true)
循环什么时候中止啊
shunde_saint 2004-08-11
  • 打赏
  • 举报
回复
可否具体一点,程序应该修改那里,才不会出现异常?
xuyang821225 2004-08-11
  • 打赏
  • 举报
回复
你的输入流那有问题啦

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧