关于JAVA读取文件流的问题

LBreathy 2009-09-25 11:18:26
在练习JAVA IO读取和写入文件流的时候发现了一个问题,小弟像大家请教,写入的时候完全正常,在读取String类型的文件的时候也正常,但是在读取int型文件的时候发现,读取出来的数据,不是写入的数据,请问各位大侠是哪里出错了?
代码如下:
写入的代码:

PrintWriter outputStream=null;
try {
outputStream=new PrintWriter(new FileOutputStream("f:\\number.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("OutPutStream is begining");
outputStream.println(1);
outputStream.println(2);
outputStream.println(3);
outputStream.close();
System.out.println("OutPutStream is closed");


读取的代码:

BufferedReader inputStreams=null;
try {
inputStreams=new BufferedReader(new FileReader("f:\\sally.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// String line="";
int number=0;
int count=0;
System.out.println("BufferedReader is begining");
try {
while(inputStreams.ready())
{
number=inputStreams.read();
System.out.println(number);
count++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
inputStreams.close();
System.out.println("inputStreams is closed");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(line);
System.out.println("Has "+count+" lines");




控制台输出的时候:
OutPutStream is begining
OutPutStream is closed
BufferedReader is begining
39532
19996
38451
65292
22823
31528
34507
13
10
39532
19996
38451
65292
22823
31528
34507
13
10
97
100
109
105
110
13
10
97
112
116
101
99
104
13
10
39532
19996
38451
65292
22823
31528
34507
13
10
97
100
109
105
110
13
10
97
112
116
101
99
104
13
10
39532
19996
38451
65292
22823
31528
34507
13
10
97
100
109
105
110
13
10
97
112
116
101
99
104
13
10
39532
19996
38451
65292
22823
31528
34507
13
10
97
100
109
105
110
13
10
97
112
116
101
99
104
13
10
39532
19996
38451
65292
22823
31528
34507
13
10
97
100
109
105
110
13
10
97
112
116
101
99
104
13
10
39532
19996
38451
65292
22823
31528
34507
13
10
97
100
109
105
110
13
10
97
112
116
101
99
104
13
10
39532
19996
38451
65292
22823
31528
34507
13
10
97
100
109
105
110
13
10
97
112
116
101
99
104
13
10
inputStreams is closed
Has 177 lines




谢谢各位大侠了~~
...全文
217 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
写一个文件读另一个文件,当然内容不一样了。
lovecj6185 2009-09-25
  • 打赏
  • 举报
回复
while(inputStreams.ready())
{
number=inputStreams.read();
System.out.println(number);
count++;
}

估计上面这一段有问题,不知道你count++是用来做什莫的

建议这样来读
BufferedReader br = new BufferedReader(new FileReader("f:\\sally.txt"));
String temp = "";
while((temp = br.readLine())!=null){
System.out.println(temp);
}
bigbug9002 2009-09-25
  • 打赏
  • 举报
回复
你写入的是f:\number.txt
你读出的是f:\sally.txt
LBreathy 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bigbug9002 的回复:]
你写入的是f:\number.txt
你读出的是f:\sally.txt
[/Quote]



我这.....发出来才看到的,呵呵~~~~

62,615

社区成员

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

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