Dependent Packages:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
Source:
1 Packageauto.script.interfaceupdate.base;2 3 Importorg.apache.commons.io.FileUtils;4 5 ImportJava.io.File;6 Importjava.io.IOException;7 Importjava.util.List;8 9 /**Ten * @Auther: Gongxingrui One * @Date: 2018-05-10 A * @Description: Modify file Contents: String -by-line substitution - **/ - Public classMyfileutils { the /** - * Modify file content: String -by-line substitution - * - * @paramfile: Files to be processed + * @paramoldstr: Old string to replace - * @paramnewstr: New string for substitution + */ A Public Static Booleanmodifyfilecontent (file file, string oldstr, String newstr) { atlist<string> list =NULL; - Try { -List = Fileutils.readlines (file, "UTF-8"); - for(inti = 0; I < list.size (); i++) { -String temp =List.get (i). ReplaceAll (Oldstr, newstr); - List.remove (i); in List.add (i, temp); - } toFileutils.writelines (file, "UTF-8", List,false); +}Catch(IOException e) { - e.printstacktrace (); the } * $ return true;Panax Notoginseng } - the Public Static Booleanmodifyfilecontent (String FilePath, String oldstr, String newstr) { + returnModifyfilecontent (NewFile (FilePath), Oldstr, newstr); A } the +}
Java Modified file content: string-by-line substitution