关于.properties文件的问题 问个菜鸟级的问题.properties文件的作用是不是初始化参数啊?能不能描述一下它的基本功能和用法吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个类是用来获取系统属性的Properties p=System.getProperties( ); p.list(System.out); 你执行看一下就知道这里有啥东东了。 .properties文件通常是指以key=value方式存放配置参数的文件。 import java.util.Properties;import java.io.FileOutputStream;import java.io.*;public class TestProperty { static public void main(String[] args) { Properties prop = new Properties(); prop.setProperty("head", "头部文件"); prop.setProperty("title", "头部文件"); prop.setProperty("form", "表单文件"); prop.setProperty("body", "身体文件"); prop.setProperty("table", "表个文件"); prop.setProperty("tr", "行文件"); prop.setProperty("td", "单元文件"); FileOutputStream fos1 = null; FileOutputStream fos2 = null; try { fos1 = new FileOutputStream("aaa.properties"); fos2 = new FileOutputStream("bbb.xml"); prop.store(fos1, null); prop.storeToXML(fos2, "", "UTF-8"); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (fos1 != null) { fos1.close(); } if (fos2 != null) { fos2.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } } }} 这个是读取保存操作:import java.util.Arrays;import java.io.IOException;import java.util.Properties;import java.util.Enumeration;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;public class PropertyFileSort { public static void sortPropertyFile(String fileNameAndPath) { FileInputStream fis = null; FileOutputStream fos = null; Properties properties = new Properties(); try { fis = new FileInputStream(fileNameAndPath); if (fis != null) { properties.load(fis); properties = sortPropertyName(properties); } fos = new FileOutputStream(fileNameAndPath); properties.store(fos, null); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } } } private static Properties sortPropertyName(Properties properties) { String[] name = null; StringBuffer nameUnite = null; Properties prop = new Properties(); Enumeration enum = properties.keys(); while (enum.hasMoreElements()) { Object key = enum.nextElement(); String extensePropertyName = (String) key; Object extensePropertyValue = properties.get(key); name = extensePropertyName.split(","); Arrays.sort(name); nameUnite = new StringBuffer(); for (int i = 0; i < name.length; i++) { if (i == name.length - 1) { nameUnite.append(name[i]); } else { nameUnite.append(name[i]).append(","); } } prop.put(nameUnite.toString(), extensePropertyValue); } return prop; } public static void main(String[] args) { sortPropertyFile("d:\\a.properties"); }}注:enum在jdk1.5中已成关键字,如果出现变异错误请把枚举对象改名。 读写用ResourceBundle蛮不错的。 ROSE 反向工程 限制了JTextField如何还原限制 类中可以套类吗? Console类? 哪位高人请帮我看看这个为什么不过啊,谢谢~~ 求助!!!!!!!! 读取xml文件问题! 使用Tomcat的连接池,可不可以用singleton模式调用? junit4测试异常 一个用JBuilder生成jar的问题? java读入二进制文件,怎么区分文件末尾和值-1的字节?急!!!! 在网络上用URL多一个3g大小的log文件,每次到结尾处都会报Connection Reset错误,请问该怎样处理?
Properties p=System.getProperties( );
p.list(System.out);
你执行看一下就知道这里有啥东东了。
import java.util.Properties;
import java.io.FileOutputStream;
import java.io.*;public class TestProperty { static public void main(String[] args) { Properties prop = new Properties(); prop.setProperty("head", "头部文件");
prop.setProperty("title", "头部文件");
prop.setProperty("form", "表单文件");
prop.setProperty("body", "身体文件");
prop.setProperty("table", "表个文件");
prop.setProperty("tr", "行文件");
prop.setProperty("td", "单元文件"); FileOutputStream fos1 = null;
FileOutputStream fos2 = null; try {
fos1 = new FileOutputStream("aaa.properties");
fos2 = new FileOutputStream("bbb.xml"); prop.store(fos1, null);
prop.storeToXML(fos2, "", "UTF-8");
}
catch (IOException ioe) {
ioe.printStackTrace();
}
finally { try {
if (fos1 != null) {
fos1.close();
}
if (fos2 != null) {
fos2.close();
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.util.Properties;
import java.util.Enumeration;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;public class PropertyFileSort { public static void sortPropertyFile(String fileNameAndPath) { FileInputStream fis = null;
FileOutputStream fos = null;
Properties properties = new Properties(); try {
fis = new FileInputStream(fileNameAndPath); if (fis != null) {
properties.load(fis);
properties = sortPropertyName(properties);
}
fos = new FileOutputStream(fileNameAndPath);
properties.store(fos, null);
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
finally { try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
} private static Properties sortPropertyName(Properties properties) { String[] name = null;
StringBuffer nameUnite = null;
Properties prop = new Properties();
Enumeration enum = properties.keys(); while (enum.hasMoreElements()) { Object key = enum.nextElement(); String extensePropertyName = (String) key;
Object extensePropertyValue = properties.get(key); name = extensePropertyName.split(",");
Arrays.sort(name); nameUnite = new StringBuffer(); for (int i = 0; i < name.length; i++) { if (i == name.length - 1) {
nameUnite.append(name[i]);
}
else {
nameUnite.append(name[i]).append(",");
}
} prop.put(nameUnite.toString(), extensePropertyValue);
} return prop;
} public static void main(String[] args) { sortPropertyFile("d:\\a.properties");
}
}注:enum在jdk1.5中已成关键字,如果出现变异错误请把枚举对象改名。