package com.supcon;
import java.io.*;
/**
 * @author: zhaoxu
 * @description:
 */
public class test {
    public static void main(String[] args) throws IOException {
        StringBuffer fileString = readFile(args[0]);
        String newString = fileString.toString().replaceAll(args[1], args[2]);
        writeToFile(args[0],newString);
    }
    /**
     * 按行读取全部文件数据
     *
     * @param strFile
     */
    public static StringBuffer readFile(String strFile) throws IOException {
        StringBuffer strSb = new StringBuffer();
        InputStreamReader inStrR = new InputStreamReader(new FileInputStream(strFile), "UTF-8");
        // character streams
        BufferedReader br = new BufferedReader(inStrR);
        String line = br.readLine();
        while (line != null) {
            strSb.append(line).append("\r\n");
            line = br.readLine();
        }
        return strSb;
    }a
    /**
     * 写入文件
     * @param fileName
     * @param s
     * @throws IOException
     */
    public static void writeToFile(String fileName,String s) throws IOException {
        File f1 = new File(fileName);
        OutputStream out = null;
        BufferedWriter bw = null;
        if (f1.exists()) {
            out = new FileOutputStream(f1);
            bw = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
            bw.write(s);
            bw.flush();
            bw.close();
        } else {
            System.out.println("文件不存在");
        }
    }
}
java通过正砸表达式修改某个文件内容
最新推荐文章于 2024-09-13 11:22:58 发布
          
          
       
          
       
       
                   
                   
                   
                   
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   1439
					1439
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            