IO+Properties联合应用详解
- 以后经常改变的数据,可以单独写到一个文件中,使用程序动态读取,将来只用改变文件中数据,就可拿到动态内容。
类似以上机制的文件称为配置文件。并且当配置文件中的内容格式是key1=value key2=value
的时候,我们称这种配置文件为属性配置文件。 - java规范中,属性配置文件建议以.properties结尾,但这不是必须的。
- 属性配置文件的key重复的话,value会自动覆盖。在属性配置文件中#号是注释。
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class IoProperties {
public static void main(String[] args) {
FileInputStream fis=null;
try {
fis=new FileInputStream("D:\\eclipse\\userinfo.properties");
Properties pro=new Properties();
pro.load(fis);
String username=pro.getProperty("username");
System.out.println(username);
String password=pro.getProperty("password");
System.out.println(password);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}