第一种:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test01 {
public static void main(String[] args) {
Properties properties = new Properties();
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("a.properties");
try {
properties.load(stream);
} catch (IOException e) {
e.printStackTrace();
}
String name = properties.getProperty("name");
System.out.println(name);
}
}
注意:文件路径的格式为:包名/文件名
第二种:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test02 {
public static void main(String[] args) {
Properties properties = new Properties();
InputStream stream = Test02.class.getResourceAsStream("/a.properties");
try {
properties.load(stream);
} catch (IOException e) {
e.printStackTrace();
}
String name = properties.getProperty("name");
System.out.println(name);
}
}
注意:文件路径的格式:
如果在当前包中,直接写文件名,如果不在当前包中,/包名/文件名。
第三种:
import java.util.Properties;
import java.util.ResourceBundle;
public class Test03 {
public static void main(String[] args) {
Properties properties = new Properties();
ResourceBundle rb = ResourceBundle.getBundle("a");
String name = rb.getString("name");
System.out.println(name);
}
}
注意:不用写文件的后缀