• java 读取类内容给指定的方法追加内容


    思路:首先读取文件内容,找到方法名后,然后追加内容。针对的是小文件处理方式。

     String beanPath = System.getProperty("user.dir") + "/src/main/java/com/test.java";
    		 try {
    			BufferedReader bufReader = new BufferedReader(
    					new InputStreamReader(new FileInputStream(new File(beanPath))));//数据流读取文件
    			StringBuffer strBuffer = new StringBuffer();
    			for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {
    				strBuffer.append(temp);
    				strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割
    				if(temp.indexOf("test") != -1){//找到指定的方法
    					strBuffer.append("添加内容");//插入内容
    					strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割
    				}
    			}
    			bufReader.close();
    			PrintWriter printWriter = new PrintWriter(beanPath);//替换后输出的文件位置
    			printWriter.write(strBuffer.toString().toCharArray());
    			printWriter.flush();
    			printWriter.close();
    
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    

      

  • 相关阅读:
    CH6301 疫情控制
    [BeiJing2010组队]次小生成树 Tree
    CH6303 天天爱跑步
    CH6302 雨天的尾巴
    POJ3417 Network
    Problem 1999. -- [Noip2007]Core树网的核
    [Apio2010]patrol 巡逻
    「TJOI2018」str
    NOI2018 你的名字
    BZOJ5137 [Usaco2017 Dec]Standing Out from the Herd
  • 原文地址:https://www.cnblogs.com/zt528/p/5354455.html
Copyright © 2020-2023  润新知