java 读写properties文件,不会出现中文乱码 -凯发k8国际

`

java 读写properties文件,不会出现中文乱码

    博客分类:
  • java

public class test {

 public static void main(string[] args) {

  /*测试读取properties文件
   * operateproperties op=new operateproperties();
  op.readproperties("f:/test.properties");
  map map=op.processproperties();
  
  set keys=map.keyset();
  string key=null;
  for(object k:keys){
   key=k "";
   system.out.println(" " key "  " map.get(key));
  }*/
  
  
  /*  写入一个新的properties文件,save方法会把中文转换成unicode编码,不会出现中文乱码
  operateproperties op2=new operateproperties();
  op2.writeproperties("f:/testsave.properties");*/
  
  /*
   * 使用list方法写入一个properties文件,不会出现中文乱码,不会转换成unicode编码,正常显示中文
   * operateproperties opx=new operateproperties();
  opx.writeproperties(new file("f:/testlist.properties"));*/
  
  //写入一个properties文 件,store()方法,也会把中文转换成unicode编码,不会出现乱码,推荐使用store()
  operateproperties op2=new operateproperties();
  op2.writeproperties("f:/teststore.properties");
 }
}

 

 

public class operateproperties {

 private static properties prop;
 
 static{
  prop=new properties(); 
 }
 
 /**
  * 读取properties文件
  *
  * @param filename
  */
 void readproperties(string filename){
  fileinputstream is=null;
  try{
   // bufferedinputstream bis=null;
   is=new fileinputstream(filename); // 字节流容易出现中文乱码
   // bis=new bufferedinputstream(is);
   prop.load(is);
   // prop.load(bis);
  } catch (filenotfoundexception e) {
   e.printstacktrace();
  }
   catch (ioexception e) {
   e.printstacktrace();
  }
  finally{
    try {
     is.close();
    } catch (ioexception e) {
     e.printstacktrace();
    }
  }
  
 }
 
 /**
  * 把properties的值存储到map
  * @return map
  */
 map processproperties(){
  map map=new hashtable();
  set keys=prop.keyset();
  string key=null;
  string str="";
  for(object k:keys){
   key=k "";
   map.put(key, prop.getproperty(key));
   
   str=prop.getproperty(key);
  }
  return map;
 }
 
 // 解决中文乱码的方法(读):使用jdk/bin下的native2ascii命令把中文转换成unicode编码
 
 /**
  * 写入properties文件,会覆盖以前的内容
  * @param filename 为properties文件
  */
 void writeproperties(string filename){
  
  fileoutputstream fos=null;
  try{
   file file=new file(filename);
   if(!file.exists()){
     file.createnewfile();
   }
   fos=new fileoutputstream(file);
   
   prop.setproperty("what.is.you.name", "wangwu");
   prop.setproperty("your.sex", "男");
//   prop.save(fos, "注释");  //写入的中文“注释”为乱码,但是属性值,如果是中文 会转换成unicode,不会出现乱码,推荐使用
   prop.store(fos, "我是一只小鸟");
   
   //prop.storetoxml(fos, "comment"); // 属性值如果是中文,会出现中文乱码,不推荐使用
  }
  catch(filenotfoundexception e){
   e.printstacktrace();
  }
  catch(ioexception e){
   e.printstacktrace();
  }
  finally{
   try {
    fos.close();
   } catch (ioexception e) {
    e.printstacktrace();
   }
  }
 }
 /**
  * 写入一个properties文件,会覆盖以前的内容
  * 正常显示中文 ,不会把中文转换成unicode
  * @param file
  */
 void writeproperties(file file){
  printstream ps=null;
  try{
   if(!file.exists()){
     file.createnewfile();
   }
   ps=new printstream(file);
   
   prop.setproperty("what.is.you.name", "wangwu");
   prop.setproperty("your.sex", "男");
   prop.list(ps);
  }
  catch(filenotfoundexception e){
   e.printstacktrace();
  }
  catch(ioexception e){
   e.printstacktrace();
  }
  finally{
    ps.close();
  }
 }
}

分享到:
|
评论
1 楼 xingcxb 2015-10-13  
经过测试,假的,依旧会出现中文乱码!!!!store方法里面采用的是硬编码iso8859-1,麻烦博主抄袭也得要认真一点

相关推荐

    java读写properties配置文件

    java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题

    java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题

    主要介绍了java读写.properties文件解决中文乱码问题,非常具有实用价值,需要的朋友可以参考下

    null 博文链接:https://thinktothings.iteye.com/blog/1536044

    ava中有个比较重要的类properties(java.util.properties),主要用于读取java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去...

    java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件

    java源码读写properties文件.rar

    java读写文件避免中文乱码.docx

    c#读写文本文件(不会产生乱码)[初学者用]

    java对properties配置文件的读和写

    java读写properties文件解决中文乱码问题.

    java 读写ini文件,java读写ini文件的项目小例子。 运行类文件在根目录ini文件夹下src/com文件夹中start.java文件 相关jar包在根目录ini文件夹下bin文件夹中

    properties文件读写操作

    最近在用java的io包下面的randomaccessfile类读写中文文件时遇到了乱码问题,解决不了。于是索性自己写个类,可以进行中文文件读写而不乱码。 压缩包里提供了.jar包和源代码。 ************************************...

    java读写excel文件,新手参考高手绕道。

    java简单的读写文本文件的代码java简单的读写文本文件的代码

    java读写文件避免中文乱码.pdf

    java 读写文件文本文件的示例

global site tag (gtag.js) - google analytics
网站地图