java 修改文件某行内容 - Go语言中文社区

java 修改文件某行内容


1、原始文件见下图,insert into 后没有表名,sql无法执行。如需执行,就得替换空串,加入表名

 2、处理程序

public class ProcessData {       
    public String readFileContent(String filePath) {  
        BufferedReader br = null;  
        String line = null;  
        StringBuffer bufAll = new StringBuffer();  //保存修改过后的所有内容,不断增加         
        try {            
            br = new BufferedReader(new FileReader(filePath));              
            while ((line = br.readLine()) != null) {  
                StringBuffer buf = new StringBuffer();  
              //修改内容核心代码
                if (line.startsWith("INSERT INTO")) {  
                   buf.append(line);
                   buf.replace(12, 14, "lf");
                   buf.append(System.getProperty("line.separator"));
                   bufAll.append(buf);
                }  
          
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (br != null) {  
                try {  
                    br.close();  
                } catch (IOException e) {  
                    br = null;  
                }  
            }  
        }  
        return bufAll.toString();  
    }        
   //写回文件
    public void writeFile(String filePath, String content) {  
        BufferedWriter bw = null;  
          
        try {           
            bw = new BufferedWriter(new FileWriter(filePath));     
            bw.write(content);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bw != null) {  
                try {  
                    bw.close();  
                } catch (IOException e) {  
                    bw = null;  
                }  
            }  
        }  
    }     

    public static void main(String[] args) {  
        String filePath = "D:/data_lf.sql";
        ProcessData processData = new ProcessData();  
        processData.writeFile(filePath, processData.readFileContent(filePath));        
    }    
}

3、处理后的文件

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_33643690/article/details/81737957
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-28 02:08:30
  • 阅读 ( 1049 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢