web1.properties
、web2.properties
、web3.properties
のような3つのプロパティファイルがあります。単一のプロパティオブジェクトを使用して、一度にすべてのプロパティファイルをロードしたいと思います。Javaを使用して複数のプロパティファイルをロードして読み込む方法
Properties prop = new Properties();
prop.load("web1.properties");
prop.load("web2.properties");
prop.load("web3.properties");`
私は以下のコードを複数のプロパティファイルをロードしようとしています。
public class ReadFileExtension {
static Properties prop = new Properties();
public static void main(String[] args) throws IOException {
String projectFolder = System.getProperty("user.dir");
File f1 = new File(projectFolder);
File[] listOfFiles = f1.listFiles();
System.out.println("Length:"+listOfFiles.length);
for(int i=0;i<listOfFiles.length;i++){
if (listOfFiles[i].isDirectory()) {
//System.out.println("File " + listOfFiles[i].getAbsolutePath());
String prop1 = listOfFiles[i].getAbsolutePath();
if(prop1.contains("properties")){
System.out.println(prop1);
File folder = new File(prop1);
File[] listOfFiles1 = folder.listFiles();
for (int j = 0; j < listOfFiles1.length; j++) {
if (listOfFiles1[j].isFile()) {
//System.out.println("Feature File: " + listOfFiles1[j].getAbsolutePath());
//prop.load(new FileInputStream(listOfFiles1[j].getAbsolutePath()));
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(listOfFiles1[j].getAbsolutePath()));
}
}
}
}
}
System.out.println("Prop 1: "+prop.getProperty("ApplicationURL"));
System.out.println("Prop 2: "+prop.getProperty("Application"));
}
}
[this](http://stackoverflow.com/questions/5093432/multiple-properties-files)のようなものを使用できます。 – Abhishek
投稿したコードを実行すると、何が得られましたか? –
@Abhishek NullPointerException - そのリンクでコードを実行したとき。 – tsivarajan