2019独角兽企业重金招聘Python工程师标准>>>
package com.test;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.ParseException;public class Test {public static void main(String[] args) throws ParseException, IOException {//讀取文件FileInputStream fileInputStream = new FileInputStream(Test.class.getClassLoader().getResource("").getPath()+"/coupons_share.html");InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String str = "";StringBuilder sb = new StringBuilder();while((str=bufferedReader.readLine())!=null){sb.append(str);}//替換文件中的特定字符串String newStr = sb.toString().replace("{bg}", "{11111111111}");//寫入某個文件夾中File file = new File("d://a.html");FileOutputStream fileOutputStream = new FileOutputStream(file);OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);bufferedWriter.write(newStr);//注意flushbufferedWriter.flush();}
}