read from property file java

Solutions on MaxInterview for read from property file java by the best coders in the world

showing results for - "read from property file java"
Isabel
18 Oct 2020
1package crunchify.com.tutorial;
2 
3import java.io.FileNotFoundException;
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.Date;
7import java.util.Properties;
8 
9/**
10 * @author Crunchify.com
11 * 
12 */
13 
14public class CrunchifyGetPropertyValues {
15	String result = "";
16	InputStream inputStream;
17 
18	public String getPropValues() throws IOException {
19 
20		try {
21			Properties prop = new Properties();
22			String propFileName = "config.properties";
23 
24			inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
25 
26			if (inputStream != null) {
27				prop.load(inputStream);
28			} else {
29				throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
30			}
31 
32			Date time = new Date(System.currentTimeMillis());
33 
34			// get the property value and print it out
35			String user = prop.getProperty("user");
36			String company1 = prop.getProperty("company1");
37			String company2 = prop.getProperty("company2");
38			String company3 = prop.getProperty("company3");
39 
40			result = "Company List = " + company1 + ", " + company2 + ", " + company3;
41			System.out.println(result + "\nProgram Ran on " + time + " by user=" + user);
42		} catch (Exception e) {
43			System.out.println("Exception: " + e);
44		} finally {
45			inputStream.close();
46		}
47		return result;
48	}
49}
50