文件流操作

/**
     * 把 版权信息 写入到 根目录下的 copyInfo.txt文件中
     * @param str
     */
    public void writer(String infoPath, String str) {
       
       PrintWriter pw;
       try {
           HttpServletRequest request = ServletActionContext.getRequest();
           String basePath=request.getServletContext().getRealPath("copyInfo.txt");  
           
           pw = new PrintWriter( new FileWriter( basePath ) );
           pw.print(str);
           pw.close();
       } catch (IOException e) {
           logger.error("文件写操作,configInfoAction.writer", e);
       }
       
    }
    
    /**
     * 读取 根目录下的 copyInfo.txt 文件中的 信息
     */
    public String reader(String infoPath) {
       
       HttpServletRequest request = ServletActionContext.getRequest();
       String basePath=request.getServletContext().getRealPath("copyInfo.txt");
       
       FileReader fr;
       String temp="";
       try {
           fr = new FileReader(basePath);
           BufferedReader br=new BufferedReader(fr);
           
           String line=null;
           while((line=br.readLine())!=null){
           temp+=line;
           }
           br.close();
           fr.close();
           
           
       } catch (FileNotFoundException e) {
           logger.error("读版权信息-文件是否存在异常,configInfoAction.reader", e);
       } catch (IOException e) {
           logger.error("读版权信息-IO,configInfoAction.reader", e);
       }
       
       return temp;
    }


/**
     * 向property文件中写入信息
     * @param path
     * @param key
     * @param value
     * @throws IOException
     */
    public void setProperty(String path, String key, String value) throws IOException {
       
           props.setProperty(key,value);//设置属性值,如不属性不存在新建
            //props.setProperty("testProperty","testPropertyValue");
            FileOutputStream out;
           out = new FileOutputStream(path);
           props.store(out,"");//设置属性头,如不想设置,请把后面一个用""替换掉
            out.flush();//清空缓存,写入磁盘
            out.close();//关闭输出流
       
    }
    
    /**
     * 获得配置文件中的  版权信息
     * @return
     */
    public static String getCopyInfo() {
       
       //获得属性文件路径
       HttpServletRequest request = ServletActionContext.getRequest();
       String basePath=request.getServletContext().getRealPath("/WEB-INF/src/setting.properties");    
       
       PropertyUtil pu=new PropertyUtil(basePath);
       System.out.println(pu.getProperty("CopyInfo"));
       return pu.getProperty("CopyInfo");
       
       
    }


private static Properties props=new Properties();
    
    public PropertyUtil(String path){
       
       InputStream is=null;
       try{
           is=new FileInputStream(path);
           props.load(is);
       }catch(Exception e){
           throw new RuntimeException(e);
       }finally{
           if(null!=is){
              try{
                  is.close();
              }catch(IOException e){
                  throw new RuntimeException(e);
              }
           }
       }
    }
    public static String getProperty(String key){ 
       String property=props.getProperty(key);       
       return property;
    }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值