Java之修改檔案內容:字串逐行替換

來源:互聯網
上載者:User

標籤:cti   news   content   ack   util   des   字元   修改   end   

依賴包:

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

源碼:

 1 package auto.script.interfaceupdate.base; 2  3 import org.apache.commons.io.FileUtils; 4  5 import java.io.File; 6 import java.io.IOException; 7 import java.util.List; 8  9 /**10  * @Auther:gongxingrui11  * @Date:2018-05-1012  * @Description: 修改檔案內容:字串逐行替換13  **/14 public class MyFileUtils {15     /**16      * 修改檔案內容:字串逐行替換17      *18      * @param file:待處理的檔案19      * @param oldstr:需要替換的舊字串20      * @param newStr:用於替換的新字串21      */22     public static boolean modifyFileContent(File file, String oldstr, String newStr) {23         List<String> list = null;24         try {25             list = FileUtils.readLines(file, "UTF-8");26             for (int i = 0; i < list.size(); i++) {27                 String temp = list.get(i).replaceAll(oldstr, newStr);28                 list.remove(i);29                 list.add(i, temp);30             }31             FileUtils.writeLines(file, "UTF-8", list, false);32         } catch (IOException e) {33             e.printStackTrace();34         }35 36         return true;37     }38 39     public static boolean modifyFileContent(String filePath, String oldstr, String newStr) {40         return modifyFileContent(new File(filePath), oldstr, newStr);41     }42 43 }

 

Java之修改檔案內容:字串逐行替換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.