服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

读写指定的属性文件演示


代码:
import java.io.*;
import java.util.properties;

public class propertiesdemo {
  public static void main(string[] args)
  {
    string pfilename = system.getproperty("user.dir")
      + system.getproperty("file.separator") + "test.properties"; // 构造文件名
    properties p = new properties();  

    try
    {
      fileinputstream in = new fileinputstream(pfilename);  // 构造文件的输入流
      p.load(in);           // 读入属性
      in.close();
    }
    catch(exception e)
    {
      system.out.println("error of create input stream");
    }

    system.out.println(p.getproperty("property1"));
    p.setproperty("property1","new value"); // 给property1赋新的值

    try
    {
      fileoutputstream out = new fileoutputstream(pfilename);
      p.store(out,"this file is a test"); // 设置属性文件的文件头信息
      out.flush();
      out.close();
    }
    catch(exception e)
    {
      system.out.println("error of write input stream");
    }
  }
}

扫描关注微信公众号