JAVAIO

走在路上的人吧 2013-11-18 11:04:18

package javaIO;
import java.io.*;
public class IOBug {

/**
* 功能描述: <br>
* 〈功能详细描述〉
*
* @param args
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data3.txt")));
out.writeDouble(3.1415926);
out.writeBytes("that was the value of pi");
out.writeBytes("this is pi/2 "+"\n");
out.writeDouble(3.1415926);
out.close();
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("data3.txt")));
BufferedReader inbr = new BufferedReader(new InputStreamReader(in));
System.out.println(in.readDouble());
System.out.println(inbr.readLine());
System.out.println(inbr.readLine()); ///此行
System.out.println(in.readDouble());

}

}


java编程思想4的一个例子,在标注的那一行每次都会把文件里面的对应的那一行内容和后面的double一起读取,产生EOF违例该怎么办
...全文
161 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdgdf566 2013-11-18
  • 打赏
  • 举报
回复
因为第21和22行都是Buffered型的reader
  • 打赏
  • 举报
回复
引用 1 楼 rumlee 的回复:
out.writeBytes("that was the value of pi"); out.writeBytes("this is pi/2 "+"\n"); 你这两句写入的只有一行,你可以修改成 out.writeBytes("that was the value of pi\n"); out.writeBytes("this is pi/2 "+"\n");
说的对呀,现在就和java编程思想里面提到的问题一样了,这样的程序还是有问题,还会产生EOF的违例,书上说的原因是数据是在文本之前写入的,但是我看了自己的data3里面的顺序是对的,可是读取时候就是会产生这样的违例,不晓得为什么
rumlee 2013-11-18
  • 打赏
  • 举报
回复
out.writeBytes("that was the value of pi"); out.writeBytes("this is pi/2 "+"\n"); 你这两句写入的只有一行,你可以修改成 out.writeBytes("that was the value of pi\n"); out.writeBytes("this is pi/2 "+"\n");
水诺善兮 2013-11-18
  • 打赏
  • 举报
回复
这么麻烦,路过
jdgdf566 2013-11-18
  • 打赏
  • 举报
回复
闲着没事干了吧
  • 打赏
  • 举报
回复
引用 3 楼 jdgdf566 的回复:
因为第21和22行都是Buffered型的reader
对对,呵呵,我想知道buffer型reader会产生的效果

62,614

社区成员

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

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