import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Main1 {
private static Properties props = new Properties();
public static void main(String[] args) throws IOException {
InputStream infile = Main1.class.getResourceAsStream("test.conf");
props.load(infile);
String url=props.getProperty("url");
String username=props.getProperty("username");
String password=props.getProperty("password");
System.out.println(url + "\t" + username + "\t" + password);
}
}
或者采用
ResourceBundle resource = ResourceBundle.getBundle("readProperties/test");//参数是包路径,test对应的是test.properties
resource.getString("url")
test.conf(与上面的类在同一个目录下)文件的内容
url=url
username=username
password=password
import java.io.InputStream;
import java.util.Properties;
public class Main1 {
private static Properties props = new Properties();
public static void main(String[] args) throws IOException {
InputStream infile = Main1.class.getResourceAsStream("test.conf");
props.load(infile);
String url=props.getProperty("url");
String username=props.getProperty("username");
String password=props.getProperty("password");
System.out.println(url + "\t" + username + "\t" + password);
}
}
或者采用
ResourceBundle resource = ResourceBundle.getBundle("readProperties/test");//参数是包路径,test对应的是test.properties
resource.getString("url")
test.conf(与上面的类在同一个目录下)文件的内容
url=url
username=username
password=password