In this example we will show how to get all keys from properties file.
Source Code
package com.beginner.examples;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
public class GetAllKeysFromPropertiesFile {
public static void main(String[] args) {
try {
Properties properties = new Properties();
File file = new File("tmp.properties");
InputStream inputStream = new FileInputStream(file);
properties.load(inputStream);
/*
* Use the method keySet() which will be returned in the form of set object.
*/
Set set = properties.keySet();
for(Object key : set) {
String keyString = key.toString();
System.out.println(keyString);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
tmp.properties:
db.username = Alice
db.userage = 17
Output:
db.username
db.userage
References
Imported packages in Java documentation: