日期:2014-05-20 浏览次数:21044 次
import java.io.*; /** * Created by IntelliJ IDEA. * User: gaoyong * Date: 2012-8-9 * Time: 15:31:42 * To change this template use File | Settings | File Templates. */ public class ChangeText { public static void main(String[] args) throws Exception { FileReader reader=new FileReader("asd.txt"); BufferedReader br=new BufferedReader(reader); FileWriter fw=new FileWriter("asd.txt.tmp"); String s=""; while((s=br.readLine())!=null){ System.out.println(""+s); //下面的if语句 写自己的替换逻辑 if(s.equals("02 345")){ s="01 345"; } fw.write(s); fw.write("\r\n"); fw.flush(); } br.close(); fw.close(); File renameFile=new File("asd.txt"); renameFile.delete(); //删掉原来的文件 File file=new File("asd.txt.tmp"); file.renameTo(renameFile); // 重命名文件 } }