简体   繁体   中英

Java: How do I access my properties file?

So I have a property file in my project. I need to access it.

Here's the tree structure:

+ Project Name
|--+ folder1
|--+ propertyfolder
   |--+ file.properties

Or: Project/propertyfolder/file.properties

Here's what I've tried so far (one at a time, not all at once):

// error: java.io.File.<init>(Unknown Source)
File file = new File(System.getProperty("file.properties"));
File file = new File(System.getProperty("propertyfolder/file.properties"));
File file = new File(System.getProperty("propertyfolder\\file.properties"));
File file = new File(System.getProperty("../../propertyfolder/file.properties"));

And:

InputStream inputStream = getClass().getResourceAsStream("file.properties");
InputStream inputStream = getClass().getResourceAsStream("../../propertyfolder/file.properties");
InputStream inputStream = getClass().getResourceAsStream("propertyfolder/file.properties");
InputStream inputStream = getClass().getResourceAsStream("propertyfolder\\file.properties");

And all variations within getClass() , such as getClass().getClassLoader() , etc.

The error I'm getting is a NullReferenceException . It's not finding the file. How do I find it correctly?

(taken from comment to answer as OP suggested)

Just use File file = new File("propertyfolder/file.properties") but you do need to know where is java process working directory, if you cannot control it try an absolute path /c:/myapp/propertyfolder/file.properties .

You may also use /myapp/propertyfolder/file.properties path without C: disk letter to avoid windows-only mapping. You may use / path separator in Java apps works in Win,Linux,MacOSX. Watch out for text file encoding, use InputStreamReader to given an encoding parameter.

File file = new File("propertyfolder/file.properties");
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader reader = new BufferedReader(isr);
..read...
reader.close(); // this will close underlaying fileinputstream

Inorder to use getClass().resourceAsStream("file.properties") you need to make sure the file is there in the classpath.

That is if your Test.java file is compiled into bin/Test.class then make sure to have file.properties in the bin/ folder along with the Test.class

Otherwise you can use the Absolute Path, which is not advisable.

Did you set System properties to load file.properties from

1) Command line using -Dpropertyname=value OR

2) System.setProperty() API OR

3) System.load(fileName) API?

If you have n't done any one of them, do not use System.getProperty() to load file.properties file.

Assuming that you have not done above three, the best way to create file InputStream is

InputStream inputStream = getClass().getResourceAsStream("<file.properties path from classpath without />");

Properties extends Hashtable so, Each key and its corresponding value in the property list is a string.

Properties props = new Properties();
// File - Reads from Project Folder.
InputStream fileStream = new FileInputStream("applicationPATH.properties");
props.load(fileStream);

// Class Loader - Reades Form src Folder (Stand Alone application)
ClassLoader AppClassLoader = ReadPropertyFile.class.getClassLoader();
props.load(AppClassLoader.getResourceAsStream("classPATH.properties"));
    for(String key : props.stringPropertyNames()) {
        System.out.format("%s : %s \n", key, props.getProperty(key));
    }

// Reads from src folder.
ResourceBundle rb = ResourceBundle.getBundle("resourcePATH");// resourcePATH.properties
Enumeration<String> keys = rb.getKeys();
    while(keys.hasMoreElements()){
        String key = keys.nextElement();
        System.out.format(" %s = %s \n", key, rb.getString(key));
    }

// Class Loader - WebApplication : src folder (or) /WEB-INF/classes/
ClassLoader WebappClassLoader = Thread.currentThread().getContextClassLoader();
props.load(WebappClassLoader.getResourceAsStream("webprops.properties"));

To read properties from specific folder. Construct path form ProjectName

InputStream fileStream = new FileInputStream("propertyfolder/file.properties");

If Key:value pairs specified in .txt file then,

public static void readTxtFile_KeyValues() throws IOException{
    props.load(new FileReader("keyValue.txt") );

    // Display all the values in the form of key value
    for (String key : props.stringPropertyNames()) {
        String value = props.getProperty(key);
        System.out.println("Key = " + key + " \t Value = " + value);
    }
    props.clear();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM