为什么会出现乱码?

飞行的兔子 2004-07-21 02:26:17
一下程序得输出为什么会出现乱码呢?第一次还很好的,第二次运行就出现了乱码?
1: import java.io.*;
2: public class IOStreamDemo{
3: public static void main(String args[])throws IOException{
4:
5: BufferedReader in=new BufferedReader(new FileReader("IOStreamDemo.java"));
6: String s,s2=new String();
7: while((s=in.readLine())!=null)
8: s2+=s+"\n";
9: in.close();
10: BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
11: System.out.print("Enter a line:");
12: System.out.println(stdin.readLine());
13: StringReader in2=new StringReader(s2);
14: int c;
15: while((c=in2.read())!=-1)
16: System.out.print((char)c);
17: try{
18: DataInputStream in3=new DataInputStream(new ByteArrayInputStream(s2.getBytes()));
19: while(true)
20: System.out.print((char)in3.readByte());
21: }
22: catch(EOFException e){
23: System.out.println(e.toString());
24: }
25: try{
26: BufferedReader in4=new BufferedReader(new StringReader(s2));
27: PrintWriter out1=new PrintWriter(new BufferedWriter(new FileWriter("IODemo.txt")));
28: int lineCount=1;
29: while((s=in4.readLine())!=null)
30: out1.println(lineCount+++": "+s);
31: out1.close();
32: }
33: catch(EOFException e3){
34: e3.printStackTrace();
35: }
36: try{
37: DataOutputStream out2=new DataOutputStream(
38: new BufferedOutputStream(
39: new FileOutputStream("Data.txt")));
40: out2.writeDouble(3.14159);
41: out2.writeChars("\nThat was pi\n");
42: out2.writeBytes("That was pi\n");
43:
44: out2.close();
45: DataInputStream in5=new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));
46: BufferedReader in5br=new BufferedReader(new InputStreamReader(in5));
47: System.out.println(in5.readDouble());
48: System.out.println(in5br.readLine());
49: System.out.println(in5br.readLine());
50: }catch(EOFException e4){
51: e4.printStackTrace();
52: }
53: }
54: }


第一次能正确输出:3.14159
但第二次运行是就出现乱码:◎ 浣
...全文
161 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
朋友别哭 2004-07-22
  • 打赏
  • 举报
回复
呵呵
同感
up
loveisbug 2004-07-21
  • 打赏
  • 举报
回复
程序看得太累。
飞行的兔子 2004-07-21
  • 打赏
  • 举报
回复
程序前面得标号在程序里面没有,我只是为了方便查错,将整个程序导入到一个文本文件中,再加上标记得!

62,622

社区成员

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

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