zoukankan      html  css  js  c++  java
  • java修改文件内容

    项目使用到了需要将配置文件中指定内容替换为想要更新的内容,特此记下,已被后用

    /**
    	 * 
    	 * @param fileName 要修改的文件名
    	 * @param oldstr 要修改的字段
    	 * @param newStr 替换的字段
    	 */
    	public static Boolean updateStartBat(String fileName, String oldstr, String newStr){
    		RandomAccessFile raf = null;
    		try {
    			raf = new RandomAccessFile(PathUtil.appPath +File.separator +"nginx-1.14.2"
    					+ File.separator + File.separator+fileName, "rw");
    			String line = null;
                long lastPoint = 0; //记住上一次的偏移量
                while ((line = raf.readLine()) != null) {
                    final long ponit = raf.getFilePointer();
                    if(line.contains(oldstr)){
                    	String str=line.replace(oldstr, newStr);
                    	raf.seek(lastPoint);
                    	raf.writeBytes(str);
                    }
                    lastPoint = ponit; 
                }
    		} catch (FileNotFoundException e) {
    			
    			e.printStackTrace();
    		} catch (IOException e) {
    			
    			e.printStackTrace();
    		} finally {
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    		return true;
    	}
    

      

  • 相关阅读:
    3.13作业 制作网页布局
    3.11 框架和样式表
    表单
    3.8学习记录
    第一次作业
    数据库增删改查
    数据库三大范式
    数据库中的时间戳
    数据库的主键与外键
    登录页面
  • 原文地址:https://www.cnblogs.com/wangjinyu/p/10803596.html
Copyright © 2011-2022 走看看