import java.io.*;
import java.util.Properties;
import java.util.Enumeration;
public class TestPro {
public static void main(String[] args)
{
Properties prop = new Properties();
try
{
FileInputStream fis = new FileInputStream("DB.properties");
prop.load(fis);
String dbUrl = prop.getProperty("url");
String dbPassword = prop.getProperty("password");
System.out.println("根据key读取value:");
System.out.println(dbUrl);
System.out.println(dbPassword);
System.out.println("------------------------------");
prop.setProperty("password","456");
System.out.println( "修改后的密码:"+prop.getProperty("password"));
System.out.println("------------------------------");
System.out.println("动态写入properpties文件属性:author=zhaozhi3758");
prop.setProperty("author","zhaozhi3758");
prop.store(new FileOutputStream("DB.properties"),null);
System.out.println("------------------------------");
Enumeration en = prop.propertyNames();
System.out.println("读取properties的全部信息:");
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = prop.getProperty (key);
System.out.println(key+"="+Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
-----------------------------
import java.io.*;
import java.util.*;
public class ResourceBundleTest {
public static void main(String[] args)
{
Locale locale=Locale.getDefault();
ResourceBundle rb= ResourceBundle.getBundle("DB",locale);
String read=rb.getString("url");
System.out.println(read);
System.out.println("-------------------------------");
Enumeration en = rb.getKeys();//读取properties的全部信息
System.out.println("读取properties的全部信息:");
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = rb.getString(key);
System.out.println(key+"="+Property);
}
}
}
操作Properties文件信息
最新推荐文章于 2022-03-21 10:17:07 发布
