不多说,直接上代码.
class PropertiesDemo
{
public static void main(String[] args)
{
loadDemo();
}
public static void loadDemo()
{
Properties prop = new Properties();
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream("info.txt");
prop.load(fis); //将info.txt的内容读取到内存中.
prop.setProperty("front","other");
//所以不可以把这一行放在load方法之前,否则后果就是, 只有之后setProperty()里存放的键值了...
fos = new FileOutputStream("info.txt"); //创建(覆盖)一个新的info.txt(因为输出流就是这么霸道).
prop.store(fos,"biaozhu");
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
if (fos != null)
{
fos.close();
}
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
|
|