首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java里面输入输出流的一个乱码有关问题

2012-01-28 
java里面输入输出流的一个乱码问题我运行这个代码后,写入的文件里面全部是乱码,高手门,如何解决啊,谢谢imp

java里面输入输出流的一个乱码问题
我运行这个代码后,写入的文件里面全部是乱码,高手门,如何解决啊,谢谢
import   java.io.*;
public   class   Main   {
        public   static   void   main(String[]   args)   {
                int   ch   ;
                InputStreamReader   iin   =   new   InputStreamReader(System.in);
                BufferedReader   bin     =   new   BufferedReader(iin);
                File   file1   =   new   File( "D:\\test\\111.txt ");
                try
                {
                        FileOutputStream   fout   =   new   FileOutputStream(file1);
                        DataOutputStream   dout   =   new   DataOutputStream(fout);
                        System.out.println( "输入整数 ");
                        int   i   =   Integer.parseInt(bin.readLine());
                        System.out.println( "输入浮点数 ");
                        float   f   =   Float.parseFloat(bin.readLine());
                        System.out.println( "输入布尔量 ");
                        boolean   b   =   new   Boolean(bin.readLine()).booleanValue();
                        dout.writeInt(i);
                        dout.writeFloat(f);
                        dout.writeBoolean(b);
                        dout.close();

                }
                catch   (FileNotFoundException   e)
                {
                        System.out.println(e);
                }
                catch   (IOException   e)
                {
                        System.out.println(e);
                }

        }
}


[解决办法]
乱码是正常的,不影响你写入的内容,这一点如果你把你写入的内容重新读回来的话,就知道了
[解决办法]
public static void main(String[] args) {
InputStreamReader iin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(iin);


File file1 = new File( "E:/111.txt ");
try {
FileWriter fw = new FileWriter(file1);
BufferedWriter bw = new BufferedWriter(fw);

System.out.println( "输入整数 ");
bw.write(bin.readLine());
bw.newLine();
System.out.println( "输入浮点数 ");
bw.write(bin.readLine());
bw.newLine();
System.out.println( "输入布尔量 ");
bw.write(bin.readLine());
bw.newLine();
bw.close();
fw.close();
bin.close();
iin.close();

} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}

}

使用
FileWriter fw = new FileWriter(file1);
BufferedWriter bw = new BufferedWriter(fw);

热点排行