将xxx.property文件转换成xxx.java
有些时候我们需要将配置文件转换成Java文件,配置成constant来使用,如果文件很大,一个一个的复制粘贴会很麻烦,所以可以用Java里的Porperty类配合FileInputStream来进行转换,代码如下:
public class convert {
public static void main(String[] args){
Properties properties = new Properties();
try {
//加载property文件
FileInputStream inputStream = new FileInputStream("xxx.propertity");
properties.load(inputStream);
//打开输出文件
FileWriter writer = new FileWriter("xxx.java");
writer.write("public class AuditParam{");
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
//转换成Java成员变量
writer.write(" private static final String " + key + " = \"" + value + "\";\n");
}
writer.write("}\n");
writer.close();
} catch (Exception e){
e.printStackTrace();
}
}
}