java中map就是一个对象对应着另一个对象
name = asas
id = 001
像这样。
在java中要使用的话要用到Properties。
首先要确定路径
public class Gongju {
public static void main(String[] args) {
String path = "D:/i.txt";
try {
FileInputStream fis = new FileInputStream(path);
Properties pro = new Properties();
pro.load(fis);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
先用path确定路径,然后打开流,将流中了内容放到Properties中,然后就可以用getProperty方法查找想要的内容了
public class Gongju {
public static void main(String[] args) {
String path = "D:/i.txt";
try {
FileInputStream fis = new FileInputStream(path);
Properties pro = new Properties();
pro.load(fis);
String name = pro.getProperty("name");
String id = pro.getProperty("id");
System.out.println(name);
System.out.println(id);
fis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
我i.txt中的内容是
name = asas
id = 001
运行结果
asas
001
Process finished with exit code 0
如果你文件中放了多个name和id,那么会打印出最后一个name,因为文件中包含了两个相同的键"name",但对应的值不同。在Properties
对象中,后面出现会覆盖前面的值。