案例:
package demo1;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class T {
public static void main(String[] args) {
Properties properties = new Properties();
FileReader fr = null;
try {
//fr = new FileReader("C:\\Users\\ASDS\\Desktop\\class1.txt");
fr = new FileReader("src\\demo1\\class2.txt");
properties.load(fr); //从class.txt文件中获取键值对
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String username = properties.getProperty("username");//get class.text className value
String password = properties.getProperty("password");//get class.text methodName value
System.out.println(username);
System.out.println(password);
}
}
class2.txt 文件内容:
username = 123
password = 0000