Properties读写资源文件

jopen 9年前

Java中读写资源文件最重要的类是Properties,功能大致如下:
1. 读写Properties文件
2. 读写XML文件
3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要符合key=value格式即可.

注意:资源文件中含有中文时的处理方法 
1. 将中文字符通过工作转成utf8编码,可以通过Java自带的nativetoascii或Eclipse中的属性编辑器。
2. 直接调用 new String(youChineseString.getBytes("ISO-8859-1"), "GBK");或者new String(youChineseString.getBytes("ISO-8859-1"), "UTF-8");

附:WEB程序中加载资源文件的方法
Properties prop = null; 
1. prop = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename");
2. prop = this.getClass().getClassLoader().getResourceAsStream("filename");

下面是文件操作的代码:

import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.InputStream;  import java.io.OutputStream;  import java.io.UnsupportedEncodingException;  import java.util.Properties;  import com.liuyazhuang.utl.StringUtils;    public class PropertiesUtil {            // 读取资源文件,并处理中文乱码      public static String readPropertiesFile(String filename,String key) {          if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())) return null;          Properties properties = new Properties();          try {              InputStream inputStream = new FileInputStream(filename);              properties.load(inputStream);              inputStream.close(); // 关闭流          } catch (IOException e) {              e.printStackTrace();          }          String value = properties.getProperty(key);          try {              value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); // 处理中文乱码          } catch (UnsupportedEncodingException e) {              e.printStackTrace();          }          return value;      }        // 读取XML文件,并处理中文乱码      public static String readPropertiesFileFromXML(String filename,String key) {          if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())) return null;          Properties properties = new Properties();          try {              InputStream inputStream = new FileInputStream(filename);              properties.loadFromXML(inputStream);              inputStream.close();          } catch (IOException e) {              e.printStackTrace();          }          return properties.getProperty(key);      }        // 写资源文件,含中文      public static void writePropertiesFile(String filename,String key,String value) {          if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())||StringUtils.isEmpty(value.trim())) return;          Properties properties = new Properties();          try {              OutputStream outputStream = new FileOutputStream(filename);              properties.setProperty(key, value);              properties.store(outputStream, null);              outputStream.close();          } catch (IOException e) {              e.printStackTrace();          }      }        // 写资源文件到XML文件,含中文      public static void writePropertiesFileToXML(String filename,String key,String value) {          if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())||StringUtils.isEmpty(value.trim())) return;          Properties properties = new Properties();          try {              OutputStream outputStream = new FileOutputStream(filename);              properties.setProperty(key, value);              properties.storeToXML(outputStream, null);              outputStream.close();          } catch (IOException e) {              e.printStackTrace();          }      }            //main方法测试      public static void main(String[] args) {          writePropertiesFile("d:/test.properties", "hello", "world");          System.out.println(readPropertiesFile("d:/test.properties", "hello"));      }  }