黑马程序员技术交流社区

标题: 中文字符乱码问题 [打印本页]

作者: 陈志伟    时间: 2012-4-11 09:39
标题: 中文字符乱码问题
我有这样两个代码,FileOutputStreamTest类是读取指定文件的数据,FileOutputStream类往指定文件中写入数据,当执行的时候,FileOutputStreamTest类得到结果如图所示,而打开执行FileOutputStream类所得到的文件如图所示,问题是,为什么读取数据中文字符会出现乱码,而写入数据却不会呢?哪位高手帮帮忙!
其中FileInputStreamTest.java代码如下:
  1. package com.test;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;

  5. public class FileInputStreamTest {

  6.         /**
  7.          * @param args
  8.          */
  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.                 FileInputStream fin=null;
  12.                 try {
  13.                         fin=new FileInputStream("D:\\java\\123.txt");
  14.                         System.out.println("可读取的字节数:"+fin.available());
  15.                         int i=fin.read();
  16.                         while(i!=-1)
  17.                         {
  18.                                 System.out.print((char)i);
  19.                                 i=fin.read();
  20.                         }               
  21.                 } catch (FileNotFoundException e) {
  22.                         // TODO: handle exception
  23.                         e.printStackTrace();
  24.                 }
  25.                 catch (IOException e) {
  26.                         // TODO: handle exception
  27.                 e.printStackTrace();
  28.                 }
  29.                 finally
  30.                 {
  31.                         try {
  32.                                 if (null!=fin) {
  33.                                         fin.close();
  34.                                 }
  35.                         } catch (IOException e) {
  36.                                 // TODO: handle exception
  37.                                 e.printStackTrace();
  38.                         }
  39.                 }
  40.         }
  41. }
复制代码
123.txt文件内容如图:

执行代码结果:



FileOutputStreamTest.java代码如下:
  1. package com.test;
  2. import java.io.*;

  3. public class FileOutputStreamTest {

  4.         /**
  5.          * @param args
  6.          */
  7.         public static void main(String[] args) {
  8.                 // TODO Auto-generated method stub
  9.                 FileOutputStream out=null;
  10.                 try {
  11.                         out=new FileOutputStream("D:\\java\\456.txt",true);
  12.                         out.write('#');
  13.                         out.write("hello,java".getBytes());
  14.                         out.write("1345".getBytes());
  15.                         out.write("我是一名Java程序员".getBytes());
  16.                     out.flush();
  17.                 } catch (FileNotFoundException e) {
  18.                         // TODO: handle exception
  19.                         e.printStackTrace();
  20.                 }
  21.                 catch (IOException e) {
  22.                         // TODO: handle exception
  23.                         e.printStackTrace();
  24.                 }
  25.                 finally
  26.                 {
  27.                         if (out!=null) {
  28.                                 try {
  29.                                         out.close();
  30.                                 } catch (IOException e) {
  31.                                         // TODO: handle exception
  32.                                         e.printStackTrace();
  33.                                 }
  34.                         }
  35.                 }
  36.         }
  37. }
复制代码
执行代码后打开456.txt文件,结果如图:

问题:为什么读取数据中文字符会出现乱码,而写入数据却不会呢?哪位高手帮帮忙!
作者: 金兴    时间: 2012-4-11 09:58
出现乱码的原因是::你记事本的编码格式和你IO流读取的编码格式不一样 ,导致你读取的时候出现乱码,
你可以这样解决::在你的读取流当中指定编码格式::  fin=new FileInputStream("D:\\java\\123.txt","UTF-8");
避免乱码的情况发生 应该把::
1.源字符串编码
2.传输过程中的编码
3.结果字符串编码
只要这三种做到一致,一般就不会有问题了。
在从流中读数据时,最好使用byte数组,这样的好处是可以支持任何编码格式。

作者: 袁计艳    时间: 2012-4-11 10:22
出现乱码的原因是这样的:
FileOutputStream  中的 read 方法 读取数据是一个字节一个字节读取的
当你读取中文的时候 因为中文是两个字节 所以会出现乱码
读取文件数据 你可以选择其他的方式 比如:InputStreamReader等等
作者: 莫运飞    时间: 2012-4-11 10:24
import java.io.*;

class FileInputStreamTest {

        
        public static void main(String[] args) {
               
                FileInputStream fin=null;
                try {
                        fin=new FileInputStream("x.txt");
                        System.out.println("可读取的字节数:"+fin.available());

                                byte[] buf=new byte[1024];                                    int i=0;
                        while((i=fin.read(buf))!=-1)
                        {       
                                System.out.print(new String(buf,0,i));
                               i=fin.read();
                        }               
                } catch (FileNotFoundException e) {
                        
                        e.printStackTrace();
                }
                catch (IOException e) {
                        
                e.printStackTrace();
                }
                finally
                {
                        try {
                                if (null!=fin) {
                                        fin.close();
                                }


程序已经改好,运行良好 改动位置在红色部分
                        } catch (IOException e) {
                                
                                e.printStackTrace();
                        }
                }
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2