JAVA IO流中不能文件中的偶数行的问题

abo1234567 2009-12-27 07:53:54
cc.sex文件中的内容
aaaaaaaaaa
bbbbbbbbbbb
ccccccccccccc
dddddddddddddd
eeeeeeeeeeeeeee
ffffffffffffffff
gggggggggggggggggggg

源程序:
/* 读取文件中的字符串 */
public void readString(String path) {

try {
/*创建文件目录*/
File file=new File(path);
FileInputStream f=new FileInputStream(file);
/* 创建读取对象 */
InputStreamReader fileReader = new InputStreamReader(f,"UTF-8");
/* 创建缓存区 */
BufferedReader read = new BufferedReader(fileReader);
/* 读取文件 */
String line=null;
do{
System.out.println("读取成功:" + read.readLine());
}while((line=read.readLine())!=null);
/* 关闭对象 */
read.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
其中path="F:/IO/cc.sex";

输出结果是:
读取成功:aaaaaaaaaa
读取成功:ccccccccccccc
读取成功:eeeeeeeeeeeeeee
读取成功:gggggggggggggggggggg

为什么偶数行没有输出来?
...全文
177 10 打赏 收藏 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
javahuazai 2010-01-13
  • 打赏
  • 举报
回复
为什么要用do{
}while();呢?
mrying 2009-12-28
  • 打赏
  • 举报
回复
喷三楼!谁都有失误的时候!
crazylaa 2009-12-28
  • 打赏
  • 举报
回复
do{
System.out.println( "读取成功: " + read.readLine());
}while((line=read.readLine())!=null);
改成:
while((line=read.readLine())!=null){
System.out.println( "读取成功: " +line);
}
即可
hy158753228 2009-12-28
  • 打赏
  • 举报
回复
我看完LZ的代码,我笑了,太搞笑了,呵呵(没其它意思)。
有while(){}你不用,非要用那个do{}while();
老张-AI 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mrying 的回复:]
喷三楼!谁都有失误的时候!
[/Quote]

...喷三楼干什么?
System.out.println( "读取成功: " + read.readLine());
老张-AI 2009-12-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhoupuyue 的回复:]
Java codepublicvoid readString(String path) {try {/*创建文件目录*/
File file=new File(path);
FileInputStream f=new FileInputStream(file);/* 创建读取对象*/
InputStreamReader fileReader=new ?-
[/Quote]
.
谈祥庆 2009-12-27
  • 打赏
  • 举报
回复
呵呵,够白痴的。
阿_布 2009-12-27
  • 打赏
  • 举报
回复

public void readString(String path) {

try {
/*创建文件目录*/
File file=new File(path);
FileInputStream f=new FileInputStream(file);
/* 创建读取对象 */
InputStreamReader fileReader = new InputStreamReader(f, "UTF-8 ");
/* 创建缓存区 */
BufferedReader read = new BufferedReader(fileReader);
/* 读取文件 */
String line=null;
while((line=read.readLine())!=null){
System.out.println( "读取成功:"+line);
}
/* 关闭对象 */
read.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
amdgaming 2009-12-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sunnylyy 的回复:]
do{
System.out.println( "读取成功: "  +  read.readLine());
}while((line=read.readLine())!=null);

读了两次啊
[/Quote]
正解 啊
sunnylyy 2009-12-27
  • 打赏
  • 举报
回复
do{
System.out.println( "读取成功: " + read.readLine());
}while((line=read.readLine())!=null);

读了两次啊

62,620

社区成员

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

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