Creating a property file and loading data into class file in java
Create a text file and save it with some name example data.txt.
And the text file contains the data
name = google
In java class file create a method called property with an argument of type String
Code:
public class ABC{
public static String configFilePath = "C:/data.txt";
public String propert(String paramName){
String url="";
try {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(configFilePath);
prop.load(fis);
url = prop.getProperty(paramName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return url;
}
}
The above method can be used in any class.
for example:
public class xyz {
static ABC pro = new ABC();
public static void name(){
System.out.println(":::::::::: name" +pro.propert("name"));
}
Also, instead of FileInputStream we can use the below code
InputStream is = this.getClass().getResourceAsStream("/propertiesFile/commonProperties.properties");
And the text file contains the data
name = google
In java class file create a method called property with an argument of type String
Code:
public class ABC{
public static String configFilePath = "C:/data.txt";
public String propert(String paramName){
String url="";
try {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(configFilePath);
prop.load(fis);
url = prop.getProperty(paramName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return url;
}
}
The above method can be used in any class.
for example:
public class xyz {
static ABC pro = new ABC();
public static void name(){
System.out.println(":::::::::: name" +pro.propert("name"));
}
Also, instead of FileInputStream we can use the below code
InputStream is = this.getClass().getResourceAsStream("/propertiesFile/commonProperties.properties");
Comments