java解析word文档有哪些方法
展开全部 java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好办法。
经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。
rtf格式文件表面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。
----- 实现的功能:读取rtf模板内容(格式和文本内容),替换变化部分,形成新的rtf文档。
----- 实现思路:模板中固定部分手动输入,变化的部分用$info$表示,只需替换$info$即可。
1、采用字节的形式读取rtf模板内容2、将可变的内容字符串转为rtf编码3、替换原文中的可变部分,形成新的rtf文档主要程序如下:public String bin2hex(String bin) {char[] digital = "0123456789ABCDEF".toCharArray();StringBuffer sb = new StringBuffer("");byte[] bs = bin.getBytes();int bit;for (int i = 0; i > 4;sb.append("\\'");sb.append(digital[bit]);bit = bs[i] & 0x0f;sb.append(digital[bit]);}return sb.toString(); }public String readByteRtf(InputStream ins, String path){ String sourcecontent =""; try{ ins = newFileInputStream(path); byte[] b= new byte[1024];if (ins == null) {System.out.println("源模板文件不存在");}int bytesRead = 0;while (true) {bytesRead = ins.read(b, 0, 1024); // return final read bytescountsif(bytesRead == -1) {// end of InputStreamSystem.out.println("读取模板文件结束");break;}sourcecontent += new String(b, 0, bytesRead); // convert to stringusing bytes} }catch(Exception e){ e.printStackTrace(); } return sourcecontent ;}以上为核心代码,剩余部分就是替换,从新组装java中的String.replace(oldstr,newstr);方法可以实现,在这就不贴了。
源代码部分详见附件。
运行源代码前提:c盘创建YQ目录,将附件中"模板.rtf"复制到YQ目录之下,运行OpreatorRTF.java文件即可,就会在YQ目录下生成文件名如:21时15分19秒_cheney_记录.rtf的文件。
package com;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;public class OperatorRTF {public String strToRtf(String content){char[] digital = "0123456789ABCDEF".toCharArray();StringBuffer sb = new StringBuffer("");byte[] bs = content.getBytes();int bit;for (int i = 0; i > 4;sb.append("\\'");sb.append(digital[bit]);bit = bs[i] & 0x0f;sb.append(digital[bit]);}return sb.toString();}public String replaceRTF(String content,String replacecontent,intflag){String rc = strToRtf(replacecontent);String target = "";if(flag==0){target = content.replace("$timetop$",rc);}if(flag==1){target = content.replace("$info$",rc);}if(flag==2){target = content.replace("$idea$",rc);}if(flag==3){target = content.replace("$advice$",rc);}if(flag==4){target = content.replace("$infosend$",rc);}return target;}public String getSavePath() {String path = "C:\\YQ";File fDirecotry = new File(path);if (!fDirecotry.exists()) {fDirecotry.mkdirs();}return path;}public String ToSBC(String input){char[] c =input.toCharArray();for (int i =0; i < c.length; i++){if (c[i] == 32){c[i] = (char) 12288;continue;}if (c[i] < 127){c[i] = (char) (c[i] + 65248);}}return newString(c);}public void rgModel(String username, String content) {// TODO Auto-generated method stubDate current=new Date();SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-ddHH:mm:ss");String targetname = sdf.format(current).substring(11,13) + "时";targetname += sdf.format(current).substring(14,16) + "分";targetname += sdf.format(current).substring(17,19) + "秒";targetname += "_" + username +"_记录.rtf";String strpath = getSavePath();String sourname = strpath+"\\"+"模板.rtf";String sourcecontent = "";InputStream ins = null;try{ins = new FileInputStream(sourname);byte[] b = new byte[1024];if (ins == null) {System.out.println("源模板文件不存在");}int bytesRead = 0;while (true) {bytesRead = ins.read(b, 0, 1024); // return final read bytescountsif(bytesRead == -1) {// end of InputStreamSystem.out.println("读取模板文件结束");break;}sourcecontent += ...
java读取word文件的问题
请贴出代码,谢谢。
请关闭输入流,释放资源,谢谢。
调用close()方法。
其他貌似没有发现什么问题。
public static String run(String filename){WordExtractor extractor=null;String text=null;try{FileInputStream in = new FileInputStream (filename);extractor = new WordExtractor();text=extractor.extractText(in);}catch(Exception ex){//logreturn null;}return text;}public static void main(String[] args){try{FileOutputStream out=new FileOutputStream("result.txt");out.write(WordProcess.run(args[0]).getBytes());out.flush();out.close();}catch(Exception ex){System.out.println(ex.toString());}}看看这个。
模范这样写,试试看。
这个代码我试过,没问题,如果这样写还是有问题,那就不是代码的问题了。
Java 解析Word文档简历
import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;// 在当前路径(默认)创建3个非空.doc(当然也可以是.txt,.java…)文件public class FileTest{//遇到异常抛给Java虚拟机处理public static void main(String[] args)throws IOException{//i>-1,恒为true,创建无限文件,就成病毒了for (int i = 0; i < 3; i++){//指定要要输入内容的文件名nameString name = "a" + i + ".doc";//定义一个节点输出流FilOutputStream//通俗点:定义一个水管,水管通向name文件FileOutputStream out = new FileOutputStream(name);//使用PrintStream包装该节点流,使用PrintStream来输出字符串//通俗点:给水管加个水龙头(PrintStream),这个水龙头具有放水功能(ps.print())PrintStream ps = new PrintStream(out);ps.print("我我我窝窝窝窝窝窝窝窝哦我");ps.append("你");}}}一切尽在代码里!
JAVA中如何把WORD文档直接转换成html?
jacob是java和windows下的com桥,通过它我们可以在java程序中调用COM组件。
如果你的JDK是1.4,那你需要下载jacob1.9的jni库才能正常运行,早期版本在JDK1.4下有些问题。
package com;/** * Title:Word文档转html类 * Description: * Copyright:() 2002 * @author 舵手 * @version 1.0 */import com.jacob.com.*;import com.jacob.activeX.*; public class WordtoHtml { /** *文档转换函数 *@param docfile word文档的绝对路径加文件名(包含扩展名) *@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名) */ public static void change(String docfile, String htmlfile) { ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word try { app.setProperty("Visible", new Variant(false)); //设置word不可见 Object docs = app.getProperty("Documents").toDispatch(); Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); // 打开word文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(8) }, new int[1]); // 作为html格式保存到临时文件 Variant f = new Variant(false); Dispatch.call(doc, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[]{}); } } public static void main(String[] strs){ WordtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t"); }}
请各位帮忙为word文件解密!!
java读取txt文件内容。
可以作如下理解:展开全部首先获得一个文件句柄。
File file = new File(); file即为文件句柄。
两人之间连通电话网络了。
接下来可以开始打电话了。
通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。
接下来需要解读成乙方可以理解的东西既然你使用了FileInputStream()。
那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据解读完成后要输出呀。
那当然要转换成IO可以识别的数据呀。
那就需要调用字节码读取的方法BufferedReader()。
同时使用bufferedReader()的readline()方法读取txt文件中的每一行数据哈。
package com.campu;import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.Reader; public class H20121012 {/*** 功能:Java读取txt文件的内容* 步骤:1:先获得文件句柄* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取* 3:读取到输入流后,需要读取生成字节流* 4:一行一行的输出。
readline()。
* 备注:需要考虑的是异常情况* @param filePath*/public static void readTxtFile(String filePath){try {String encoding="GBK";File file=new File(filePath);if(file.isFile() && file.exists()){ //判断文件是否存在InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式BufferedReader bufferedReader = new BufferedReader(read);String lineTxt = null;while((lineTxt = bufferedReader.readLine()) != null){System.out.println(lineTxt);}read.close();}else{System.out.println("找不到指定的文件");}} catch (Exception e) {System.out.println("读取文件内容出错");e.printStackTrace();}}public static void main(String argv[]){String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt"; // "res/";readTxtFile(filePath);} }
转载请注明出处51数据库 » word文件解析java