本文共 8698 字,大约阅读时间需要 28 分钟。
量,WEB-INF\classes其实也是,java工程的class文件目录也是。
发个例子大家自己看哈.
package control;import java.io.BufferedInputStream;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Properties;public class TestMain {
//根据key读取value public static String readValue(String filePath,String key) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); props.load(in); String value = props.getProperty (key); System.out.println(key+value); return value; } catch (Exception e) { e.printStackTrace(); return null; } } //读取properties的全部信息 public static void readProperties(String filePath) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); props.load(in); Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty (key); System.out.println(key+Property); } } catch (Exception e) { e.printStackTrace(); } }//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) { Properties prop = new Properties(); try { InputStream fis = new FileInputStream(filePath); //从输入流中读取属性列表(键和元素对) prop.load(fis); //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。 OutputStream fos = new FileOutputStream(filePath); prop.setProperty(parameterName, parameterValue); //以适合使用 load 方法加载到 Properties 表中的格式, //将此 Properties 表中的属性列表(键和元素对)写入输出流 prop.store(fos, "Update '" + parameterName + "' value"); } catch (IOException e) { System.err.println("Visit "+filePath+" for updating "+parameterName+" value error"); } }public static void main(String[] args) {
readValue("info.properties","url"); writeProperties("info.properties","age","21"); readProperties("info.properties" ); System.out.println("OK"); }发个例子大家自己看哈.
package control;
import java.io.BufferedInputStream;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Properties;public class TestMain {
//根据key读取value public static String readValue(String filePath,String key) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); props.load(in); String value = props.getProperty (key); System.out.println(key+value); return value; } catch (Exception e) { e.printStackTrace(); return null; } } //读取properties的全部信息 public static void readProperties(String filePath) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); props.load(in); Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty (key); System.out.println(key+Property); } } catch (Exception e) { e.printStackTrace(); } }//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) { Properties prop = new Properties(); try { InputStream fis = new FileInputStream(filePath); //从输入流中读取属性列表(键和元素对) prop.load(fis); //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。 OutputStream fos = new FileOutputStream(filePath); prop.setProperty(parameterName, parameterValue); //以适合使用 load 方法加载到 Properties 表中的格式, //将此 Properties 表中的属性列表(键和元素对)写入输出流 prop.store(fos, "Update '" + parameterName + "' value"); } catch (IOException e) { System.err.println("Visit "+filePath+" for updating "+parameterName+" value error"); } }public static void main(String[] args) {
readValue("info.properties","url"); writeProperties("info.properties","age","21"); readProperties("info.properties" ); System.out.println("OK"); } }自己实验:<依据key获取对应的value>2014-12-20-20:30
①student.properties文件放在项目的根目录下可以被下面的路径识别,放在src目录下有点问题,
②java.util.Properties对象在读取properties配置文件时,出现中文乱码,使用了下面的转码
public static void readPropertiesByKey() { Properties pro=new Properties(); File file=new File("student.properties"); InputStream is; try { is = new FileInputStream(file); pro.load(is); System.out.println(new String(pro.getProperty("name").getBytes("iso-8859-1"),"gbk")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public static void main(String[] args) { Properties properties = new Properties(); //main函数里可以写成下面这样 InputStream inputStream = PropertiesTest.class.getResourceAsStream("/student.properties"); BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream)); try { properties.load(bf); } catch (IOException e) { e.printStackTrace(); } System.out.println(properties.getProperty("name")); }======================================================================================================
下面文章来源:http://bu-choreography.iteye.com/blog/1136047
问题的提出:初用properties,读取java properties文件的时候如果value是中文,会出现读取乱码的问题
问题分析:开始以为是文件保存编码问题,把eclipse中所有的文件编码都修改成utf8,问题依然存在;把内容复制到notepad++进行utf8编码转换,问题依旧;上网搜索有人提议重写properties类或者用jdk自带的编码转换工具,嫌麻烦而且凭感觉jdk开发者不可能不考虑东亚几国的字符编码问题;因为properties文件操作的代码是参考百度文库里的一边文章的,分析其代码后,发现其用的是字节流来读取文件,具体代码如下: