对文档内容处理后再写入另一个文档
来源:互联网 发布:网络配置异常怎么修复 编辑:程序博客网 时间:2024/05/16 05:27
首先将处理定义成一个接口
public interface DealLine { public String deal(String lineString, int line, int maxLine); //需要处理的字符串行,当前是第几行及总共多少行}
实现处理接口
public class AddSelectDealLine implements DealLine { public String deal(String lineString, int line, int maxLine){ int countNum = 3; if (line % countNum == 0 || line == 1) { lineString = "insert into tmp_clear_user(province,msisdn,service_id)\r\n" + "select " + "'" + lineString.replaceAll("\\s", "','") + "'" + " from dual union"; //每3行数据组成一个oracle sql语句,最后不足3行的数据也组成一个sql语句 }else if((line + 1) % countNum == 0 || line == (maxLine - 1)){ lineString = "select " + "'" + lineString.replaceAll("\\s", "','") + "'" + " from dual;"; }else{ lineString = "select " + "'" + lineString.replaceAll("\\s", "','") + "'" + " from dual union"; } return lineString; }}
读写中引入处理接口
public class FileReadAndFileWrite { public static void readFileByLinesAndWriteFileByLines(String openFileName, String saveFileName, DealLine dl) { BufferedReader reader = null; BufferedWriter writer = null; String tempString = null; int line = 1; int maxLine = 1; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(openFileName), "utf-8")); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveFileName), "utf-8")); //get maxLine while ((tempString = reader.readLine()) != null) { maxLine++; } reader.close(); reader = new BufferedReader(new InputStreamReader(new FileInputStream(openFileName), "utf-8")); //dealLine while ((tempString = reader.readLine()) != null) { writer.write(dl.deal(tempString, line, maxLine)); writer.newLine(); line++; } reader.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } if (writer != null) { try { writer.close(); } catch (IOException e2) { } } } } public static void createFile(String fileName) { File file = new File(fileName); try { if (!file.exists()) { file.createNewFile(); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { DealLine dl = new AddSelectDealLine(); //DealLine dl = new QueryUrlDealLine(); readFileByLinesAndWriteFileByLines("d:/新建文本文档.txt", "d:/测试.txt", dl); //每次有新的处理时只要实现接口,再在main重新定义一个接口类型指向新实现接口即可 }}
0 0
- 对文档内容处理后再写入另一个文档
- Python 对文档内容TFIDF处理
- 网页内容写入word文档
- 将内容写入xml文档
- 用户使用DocConvert处理文档后对DocConvert的评价
- 将html内容写入word文档
- ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- 创建一个TXT文档,并在文档后添加内容
- 如何把数据库的内容写入到Word文档中
- 如何把数据库的内容写入到Word文档中?
- textarea写入内容,复制到指定的文本文档。
- 实现Freemarker生成word文档,内容可二次写入
- vs2010 操作ini文档,写入和读出内容
- 运用 Range 对象处理 Word 文档内容
- 文档内容相似项处理-Shingling
- 运用Range 对象处理 Word 文档内容
- Java 对txt文档的读取和写入
- Java里面对XML文档的处理
- 报表查询中count用法
- Sublime安装,包管理
- php:细说PHP之文章发布操作实例
- android常用的各大开放平台网址
- 浅谈weblogic与tomcat的区别
- 对文档内容处理后再写入另一个文档
- 程序猿需掌握的35个jQuery小技巧
- 【java基础 8】垃圾收集算法及内存分配策略
- 列表
- pymongo的一个注意点
- 1000句最常用英语口语 (二)
- iOS 将状态栏设置成白色
- Android menu
- C# DataSet和DataTable详解