我有一个text文件,格式如下:
000001  中文名1
000002  中文名2
...     ...利用Properties类,load之后读取,结果中文都是乱码或者问号,请问这个该如何解决?
text文件另存为UTF-8或者其他格式都无效。读出的字符串也试过了各种编码和解码都不能解决。

解决方案 »

  1.   

    使用 JDK 中的 native2ascii 工具转一下就可以了,以后使用“新文件”,
    老文件需要保留着,便于以后更改,更改之后重新生成一下就可以了,建议
    可以做个 bat 文件。native2ascii <源文件> <新文件>
      

  2.   

    Properties 默认是按ISO-8859-1读取的,除了1楼的方法,还可以像下面这样,看你的Properties干什么用了,不同的需要可能需要的不一样。
             /**
     * 将ISO-8859-1转化为GBK
     * 
     * @param para
     * @return
     */
    public static String isoToGbk(String para) {
    try {
    return new String(para.getBytes("ISO-8859-1"), "GBK");
    } catch (UnsupportedEncodingException e) {
    return "";
    } catch (Exception e) {
    return "";
    }
    }
      

  3.   

    native2ascii我试过了,还是不行
    3楼的方法,我这个环境不支持GBK编码,我试过gb2312,也是乱码。现在情况是: 
                  txt文件是存为UTF-8
                java.util.Properties.load(stream);
                String oriName = properties.getProperty(id);
                String name = new String(oriName.getBytes("ISO8859-1"),"gb2312");            上面的参数中,我尝试过多种charset,文件也用NATIVE2ASCII转换过,并且文本文件
    也尝试另存为其他格式比如Unicode,统统不行,痛苦。            
      

  4.   

    你是从txt文件读还是名字是text的Properties文件啊
    要是txt文件不如直接读呢那样就没问题了
      

  5.   

    是text文件,用Properties类是因为方便而且效率高。
    text文件格式就是我前面说的,上千行:00001       中文名1
    00002       中文名2
    ....如果直接读,请问怎么效率高一些
      

  6.   

    native2ascii会不行?
    这样试试,自己用输入输出流控制
    Properties p = new Properties();
    InputStreamReader isr = new InputStreamReader(new FileInputStream("your_property_file"), "UTF-8"); //用utf-8流读入文件
    int len = 1048;
    char[] buf = new char[len];
    PipedOutputStream pos = new PipedOutputStream();//输出管道
    PrintStream ps = new PrintStream(pos, false, "UTF-8");//输出流PipedInputStream pis = new PipedInputStream(pos); //输入管道
    while (irs.read(buf, 0, len) != -1) {
        ps.print(buf); //把读入文件输出到管道,从而得到新的管道输入流
    }
    ps.flash();
    p.load(pis); //用新的管道输入流载入property具体行不行不知道,试试看吧
      

  7.   

    使用 JDK 中的 native2ascii 比如 在cmd下native2ascii -encoding gb2312  ApplicationResources_temp.properties ApplicationResources_zh_CN.properties
      

  8.   

    比如encode.bat
    [code=BatchFile]
    set path=%path%;C:\jdk1.5\bin
    native2ascii -encoding gb2312  ApplicationResources_temp.properties ApplicationResources_zh_CN.propertiesnative2ascii -encoding gb2312  displaytag_temp.properties displaytag_zh_CN.propertiesnative2ascii -encoding gb2312  eduadminResources_temp.properties eduadminResources_zh_CN.propertiesnative2ascii -encoding gb2312  studentResources_temp.properties studentResources_zh_CN.properties
    [/code]
      

  9.   

    改改Properties类    public synchronized void load(InputStream inStream) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "8859_1"));改成
        public synchronized void load(InputStream inStream) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "GBK"));
      

  10.   

    是text文件,用Properties类是因为方便而且效率高。 
    text文件格式就是我前面说的,上千行: 00001       中文名1 
    00002       中文名2 
    .... 如果直接读,请问怎么效率高一些
    _____________________不同问题,不同分析,你要把这些读到什么地方,用这些做什么?
      

  11.   

    第一步:
    native2ascii.exe -encoding utf-8 .\src\Movision\Resource.properties .\classes\Movision\Resource.properties
    (注意:properties文本用GB2312编码)
    第二步:
    public static void init() {
    if (resources == null) {
    resources = ResourceBundle.getBundle(sResourcesName);
    }
    } /**
     * @param sKeyName 被读取的Key=Value对
     * @return 返回sKeyName对应的值
     */
    public static String getString(String sKeyName) {
    String sResult = null;
    try {
    init();
    sResult = resources.getString(sKeyName);
    }
    catch (Exception e) {
    e.printStackTrace();
    sResult = null;
    }
    return sResult;
    }以上代码通过实践检验可行。
      

  12.   

    丢掉Properties的load,改用InputStream读取,就可以了.
      

  13.   

    存进去的是UTF-8  取出来的格式  取出来的时候再怎么变都是乱码
        先把存进去的格式变换为GB2312或者GBK 
       再取出来的时候就不会是乱码了
      

  14.   

     Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
                fso.CreateTextFile(Server.MapPath(File1.Value), true, true);
                string str = TextBox1.Text.Trim();
                StreamWriter sw = new StreamWriter(File1.Value, false, Encoding.UTF8);
                sw.Write(str);
                sw.Dispose();