java中.rtf文件变成文本文件
来源:互联网 发布:js时间相减 编辑:程序博客网 时间:2024/06/05 09:38
公司业务需求需要把.rtf文件转化成文本文件。
package chuyuanxiaojie;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.StringReader;import java.sql.Blob;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.zip.ZipInputStream;import javax.swing.text.DefaultStyledDocument;import javax.swing.text.rtf.RTFEditorKit;public class Indexer { public static void main(String[] args) { for (int i = 0; i < 5; i++) new Thread(new tRunnable(i)).start(); } public static Connection getConnection() { Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection( "jdbc:oracle:thin:@IP:1521:ORCL", "账号", "密码"); System.out.println("success"); } catch (Exception e) { e.printStackTrace(); } return conn; } public static Connection getConnectionnyns() { Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection( "jdbc:oracle:thin:@IP:1521:ORCL", "账号", "密码"); System.out.println("success"); } catch (Exception e) { e.printStackTrace(); } return conn; } public static String hex2str(byte[] b1) { String str = null; try { byte[] b2 = null; ZipInputStream zis = new ZipInputStream( new ByteArrayInputStream(b1)); while (zis.getNextEntry() != null) { byte[] tmp = new byte[1024]; int num = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((num = zis.read(tmp, 0, tmp.length)) != -1) baos.write(tmp, 0, num); b2 = baos.toByteArray(); baos.flush(); baos.close(); } zis.close(); DefaultStyledDocument dsd = new DefaultStyledDocument(); new RTFEditorKit().read(new ByteArrayInputStream(b2), dsd, 0); str = new String(dsd.getText(0, dsd.getLength()).getBytes( "ISO8859_1"), "gbk"); str = str.replaceAll("[A-Z]{2}\\(\\d{8},\\d,\\d\\)", ""); } catch (Exception e) { e.printStackTrace(); } return str; }}class tRunnable implements Runnable { public int num; public tRunnable(int num) { this.num = num; } @Override public void run() { try { Connection conn = new Indexer().getConnection(); Connection connnyns = new Indexer().getConnectionnyns(); int startCount = num * 900000; int endCount = startCount + 900000; conn.setAutoCommit(false); PreparedStatement ps = connnyns .prepareStatement("insert into medical_record values(?,?,?)"); System.out.println("线程" + num + "开始 " + startCount + "--" + endCount); for (int i = startCount, j = 0; i < endCount; i += 3000) { ResultSet rs = conn .createStatement() .executeQuery( "select * from (select a.文件ID,a.内容,b.创建时间,rownum rn from 电子病历格式 a,电子病历记录 b where a.文件ID=b.ID and b.文件ID='29' and rownum<=" + (i + 3000) + ") where rn>" + i); while (rs.next()) { Blob blob = rs.getBlob(2); byte[] b1 = blob.getBytes(1, (int) blob.length()); String s = Indexer.hex2str(b1); ps.setLong(1, rs.getLong(1)); ps.setCharacterStream(2, new StringReader(s), s.length()); ps.setDate(3, rs.getDate(3)); ps.executeUpdate(); } conn.commit(); rs.close(); j += 3000; System.out.println("线程" + num + "完成" + j + "条记录"); } } catch (Exception e) { e.printStackTrace(); } }}
阅读全文
0 0
- java中.rtf文件变成文本文件
- Java中存取Rtf文件
- Java中存取Rtf文件的程序
- Java中存取Rtf文件的程序
- JAVA解析RTF 文件
- c#中读写rtf文件
- Java学习中文本文件转为XMl文件
- RTF文件
- Java 写rtf(word) excel文件
- java文件读取文本文件
- java IO体系中读取文件文本文件中内容
- linux将文本文件变成可以执行的脚本文件
- java中读写文本文件
- java中复制文本文件
- 批量处理txt文本文件到Excel文件中去----java
- java中关于判断文本文件读到文件尾
- 在存储过程服务器中输出PDF,CSV,RTF文件
- [原创]从RTF格式 的文件中提取文本。
- H264码流结构分析和rtp打包结构详解
- android平台下的数据存储与解析-sharedprefrences和文件存储
- 1.3 Getting Started
- 微信小程序开发入门教程 含视频 有源码
- 本地打包传maven库
- java中.rtf文件变成文本文件
- SpringBoot的actuator(执行器)常用的访问地址
- ADS1.2卸载不了和重装不上的解决办法
- Storm调优(optimiaze)
- Hibernate4之getCurrentSession和openSession
- 909422229_汉字转拼音工具:JPinyinUtils
- 解决 linux 的 bash:vi:command not find
- Flask: Web表单
- button按钮失效五秒实现