当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java访问配置文件实现类
发布时间:2010/11/8 15:34:02 来源:城市学习网 编辑:ziteng
  import java.io.File;
  import java.io.FileInputStream;
  import java.net.URI;
  import java.net.URISyntaxException;
  import java.util.Properties;
  public class ConfigurationRead {
  private String PFILE = "";
  private Properties m_props = null;
  public ConfigurationRead(String filePath) {
  this.PFILE=filePath;
  }
  public boolean loadFile() {
  try {
  m_props = new Properties();
  m_props.load(new FileInputStream(getFile()));
  return true;
  }
  catch (URISyntaxException e)
  { System.err.println("文件路径不正确");
  e.printStackTrace();
  }
  catch (Exception e)
  { System.err.println("文件读取异常");
  e.printStackTrace();
  }
  return false;
  }
  private File getFile() throws URISyntaxException
  {
  URI fileUri = this.getClass().getClassLoader().getResource(PFILE).toURI();
  File m_file = new File(fileUri); return m_file;
  }
  public String getConfigItem(String name) {
  m_props.clear();
  try {
  m_props.load(new FileInputStream(getFile()));
  }
  catch (Exception e) {
  System.err.println("文件重新读取异常");
  e.printStackTrace();
  }
  String val = m_props.getProperty(name); return val;
  }
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved