คุณสมบัติ เป็นคลาสย่อยของคลาส Hashtable และแสดงถึงชุดคุณสมบัติที่คงอยู่ คุณสมบัติ สามารถบันทึกลงในสตรีมหรือโหลดจากสตรีมได้ แต่ละคีย์และค่าที่สอดคล้องกันในรายการคุณสมบัติคือสตริง
คุณสมบัติ ไฟล์สามารถใช้ใน Java เพื่อกำหนดค่าภายนอกและจัดเก็บ คู่คีย์-ค่า . Properties.load() วิธีการ ของคลาส Properties สะดวกในการโหลด คุณสมบัติ ไฟล์ในรูปแบบ คีย์-ค่า คู่ .
ไวยากรณ์
public class Properties extends Hashtable
ไฟล์ credentials.properties

ตัวอย่าง
import java.io.*;
import java.util.*;
public class ReadPropertiesFileTest {
public static void main(String args[]) throws IOException {
Properties prop = readPropertiesFile("credentials.properties");
System.out.println("username: "+ prop.getProperty("username"));
System.out.println("password: "+ prop.getProperty("password"));
}
public static Properties readPropertiesFile(String fileName) throws IOException {
FileInputStream fis = null;
Properties prop = null;
try {
fis = new FileInputStream(fileName);
prop = new Properties();
prop.load(fis);
} catch(FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
} finally {
fis.close();
}
return prop;
}
} ผลลัพธ์
username: admin password: admin@123