关于.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蛮不错的。 基础算 来来来,小bug,大散分 不知道哪里的毛病,关于WOW密保卡读取密保卡卡码的小程序~~~ 高人,小弟求助! 如何使用JAVA实现.Z文件的解压操作? 高手指教,,怎么将string类型中的数字转化成int啊? 走过路过的,请进来看看。一个java新手的苦恼 请问:\u5C0F是一种什么编码格式? java中的规范命名一定要形容词在前么? 能提供Jbuilder 5的下载地址吗? 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中已成关键字,如果出现变异错误请把枚举对象改名。