【demo】java file修改

来源:互联网 发布:php音乐网站设计 编辑:程序博客网 时间:2024/06/08 09:05

第一篇csdn博客,希望大家多多关照,相互学习

java file操作,就是通过java程序来对各种文件的操作。通过写程序可以快速的去掉一些手工的工作,达到一劳永逸的效果

因为刚好用到,而且感觉用到的会比较频繁,我觉得还是有必要总结一下

应用场景:

对于这样的sql,要求不能利用数据库操作软件,将其中的一个字段的值全部去掉,变成第二幅图的样子

这可怎么办?

虽然这个例子不太恰当,因为日常用的时候通过数据库软件就可以完成,但是其他场景应用还是会比较多的。对于其他类型的修改,如果数据量不大的话还是可以手动修改的,但是如果超过20条以上,并且用的还是比较多的话,还是老老实实自己写工具吧。当然,本篇文章只是起一个总结的作用,所以并不打算深入,只是做一个简单的案例,如果有更难的需求,就不在这里描述了

talk is cheap,show me the code

package tools.util;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.OutputStreamWriter;public class UpdateFile {public static void main(String[] args) {updateFile("d:\\1.sql", "d:\\2.sql");}/** * 利用程序将文件某个地方修改 * ***********************************修改前后对照*********************************** * INSERT INTO `tb_item` VALUES (536563, '阿尔卡特 (OT-927) 炭黑 联通3G手机 双卡双待',  * INSERT INTO `tb_item` VALUES ( '阿尔卡特 (OT-927) 炭黑 联通3G手机 双卡双待',  *  * 可以看到,程序可以用来修改sql的某个字段 * 用途:自动修改sql的某个字段 * @param oldFile * @param newFile */private static void updateFile(String oldFile,String newFile){try {BufferedReader br = new BufferedReader(new FileReader(oldFile));File f = new File(newFile);FileOutputStream fos = new FileOutputStream(f);OutputStreamWriter osw = new OutputStreamWriter(fos);String str = null;while ((str = br.readLine()) != null) {int begin = str.indexOf("(");int end = str.indexOf(",");str = str.substring(0, begin+1)+str.substring(end+1);osw.write(str);osw.write("\r\n");}br.close();osw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {System.out.println("文件修改完成,修改好的文件路径是:"+newFile);}}}




原创粉丝点击