普通网友 2010-04-08 22:44
浏览 340
已采纳

properties文件内容的unicode转换(提问)

[size=medium]在开发过程中,为了使开发出来的代码更健壮、更灵活,我们常常把一些常变的变量写
入到配置文件中。而在这些配置文件中,properties文件是使用的比较多的,它不仅配置起
来简单(一个key=value形式即可),而且在对文件的操作中也比较方便。
 
    由于字符的编码问题以及properties文件在国际化的使用过程中,我们常常需要将文件
内容转换为unicode码。以免从文件里取出的内容为乱码。

    解决上面问题,我们一般采用一些如 properties editor 形式的插件。现在我写了一段
类似properties editor的转码程序(转换为unicode码)。大致功能是:从一个properties文件里逐行读出一字符串(有可能是注释,也有可能是 'key=value' 形式的属性配置)。然后将属性配置行的value转换为unicode码,转换完后写入到另一个文件里。

    现在的问题是,上面功能能完成,即可以把properties文件转换成一个内容为unicode的
properties文件(后面称unicode文件)。但是,在从unicode文件读文件配置内容时候,就会报错,或根本就不能通过Properties.load(in)的形式加载文件。希望您能花点时间,帮我解决这个问题。

    也许我描述的不太清楚,现在我贴出部分代码,您先看看,知道我的大概意思,然后把整
个工程下载到你本地,你帮我调试一下。先说明一下,对这个问题,我研究了好几天,实在找
不到解决问题的方法,才贴出的。请大家别责怪我不思考就贴出问题来。

package com.tja.code;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Queue;

import com.tja.util.StringUtil;
import com.tja.util.file.CREATE_METHOD;
import com.tja.util.file.FileUtil;

/**

  • 将properties文件内容换成Unicode码形式的properties文件
  • *
    */
    public class UnicodeUtil {

    /**

    • unicode码前缀 / private static final String UNICODE_PREFIX = "\u"; /*
    • 文件内容注释标志 / private static final String COMMENTS_PREFIX = "#"; /*
    • 属性(properties)文件中的等于 '=' 符号 */ private static final String EQUAL_SIGNAL = "=";

    public static void propFile2Unicode(String fileName) throws FileNotFoundException, IOException {
    //Assert.notNull(fileName);
    propFile2Unicode(new File(fileName),null);
    }

    /**

    • 将属性文件里内容转换为unicode码(只将文件里的value转换为unicode,而key值不变)。
    • 实现逻辑: 逐行读取文件内容,对每行内容进行转码(转成unicode形式),对转码的内容
    • 又存入一队列中。最后使用队列先进先出的特点将所有内容写入一新的文件里,
    • 即fileName.
    • @param file
    • 属性文件(properties文件)
    • @param fileName
    • 记录转码后内容的文件名
    • @throws FileNotFoundException
    • @throws IOException */ public static void propFile2Unicode(File file,String fileName) throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader(file)); String line; String unicodeValue; Queue<String> queue = new LinkedList<String>(); while((line = br.readLine())!=null) { unicodeValue = value2unicode(line); //对读出的一行内容进行转码 queue.add(unicodeValue); } br.close(); //如果没指定文件名,则将内容写入原文件里 String filePath = StringUtil.hasLength(fileName)?fileName:file.getAbsolutePath(); write2file(filePath,queue); }

    /**

    • 将一行字符串转换成unicode形式。注意,分了注释和非注释的处理,注释
    • 的内容也要转换成unicode形式
    • @param str
    • 字符串
    • @return */ public static String value2unicode(String str) { return str.startsWith(COMMENTS_PREFIX)?dealWithComments(str):dealWithNonComments(str); }

    /**

    • 对注释内容进行转换成unicode,注释符号'#'不作处理
    • @param comments
    • 注释字符串,形式为 '#abccom好'
    • @return */ public static String dealWithComments(String comments) { //Assert.notNull(comments); if(!comments.startsWith(COMMENTS_PREFIX)) { throw new IllegalArgumentException("the parameter["+comments+"] is not comments!"); } String comment = comments.substring(1); return COMMENTS_PREFIX+GBK2Unicode(comment); }

    /**

    • 对非注释的一行字符串进行unicode转码,该字符串格式要求为key=value形式
    • 即如:userName=xiaoxin。否则会抛出异常
    • @param non_comments
    • 非注释字符串
    • @return */ public static String dealWithNonComments(String non_comments) { if(null == non_comments || 0 == non_comments.trim().length()) { return ""; } if(non_comments.startsWith(COMMENTS_PREFIX)) { throw new IllegalArgumentException("the parameter["+non_comments+"] is comments!"); } int index = non_comments.indexOf(EQUAL_SIGNAL); if(index == -1) { throw new IllegalArgumentException("the parameter["+non_comments+"] has no '=' !"); } String key = non_comments.substring(0,index+1); String value = non_comments.substring(index+1); return key+GBK2Unicode(value); }

    /**

    • 将队列内容写入指定文件里
    • @param filePath
    • @param queue
    • @throws IOException */ private static void write2file(String filePath,Queue<String> queue) throws IOException { //Assert.hasText(filePath); FileUtil.createFullPathFile(filePath, CREATE_METHOD.OVERWRITE); while(!queue.isEmpty()) { String content = queue.poll(); FileUtil.write2File(filePath, true,null, content+"\n"); } }

    /**

    • 字符串类型转换为Unicode
    • @param str
    • @return */ public static String GBK2Unicode(String str) { if(null == str) { return ""; } StringBuffer buf = new StringBuffer(UNICODE_PREFIX); for(int i = 0;i<str.length();i++) { buf.append(char2Unicode(str.charAt(i))); if(i != str.length()-1) { buf.append(UNICODE_PREFIX); } } return buf.toString(); }

    /**

    • char类型转换为Unicode
    • @param ch
    • @return */ public static String char2Unicode(char ch) { return Integer.toString(ch, 16); }

    public static void main(String[] args) throws FileNotFoundException, IOException {
    String str = "D:/bb.properties";
    String str1 = "D:/cc.properties";
    String ss = "项目额度";
    UnicodeUtil.propFile2Unicode(new File(str),str1);
    String g = UnicodeUtil.GBK2Unicode(ss);
    System.out.println(g);
    InputStream in = new FileInputStream(new File(str1));
    Properties p = new Properties();
    p.load(in);

    System.out.println(p.getProperty("FMS.loader.project"));
    System.out.println(p.getProperty("FMS.chinese"));
    

    }
    }


      请大家有时间帮忙看看。哦运行报的错误如下:

       Exception in thread "main" java.lang.IllegalArgumentException: Malformed \uxxxx encoding.
    at java.util.Properties.loadConvert(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at com.tja.code.UnicodeUtil.main(UnicodeUtil.java:175)[/size]
    问题补充

    helin 写道
    load的时候指定编码



    恕小弟愚笨,请问load的时候怎么指定编码呢?能给点例示代码吗?
  • 写回答

5条回答

  • waterdh1 2010-04-11 11:08
    关注

    所以问题就是出在你读取原始properties文件上了。
    这部分调试下吧,确保文件中的内容读出来不乱码就好了,这应该不是什么难的问题吧。
    另外如果用readLine读的话,别丢掉了换行符。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline