- publicclassTest3{
- /**
- *
- *@parampath
- *@parampaths
- *@paramsavepaths
- */
- publicstaticvoidchange(Stringpath,Stringpaths,Stringsavepaths){
- Filed=newFile(paths);
- //取得当前文件夹下所有文件和目录的列表
- Filelists[]=d.listFiles();
- Stringpathss=newString("");
- //对当前目录下面所有文件进行检索
- for(inti=0;i<lists.length;i++){
- if(lists[i].isFile()){
- //得到doc文件名
- Stringfilename=lists[i].getName();
- //定义最简文件名
- Stringrealfilename=newString(filename.substring(0,
- (filename.length()-4)));
- //定义文件全名为路径加文件名
- Stringfullsavefilename=paths+realfilename;
- fullsavefilename=fullsavefilename.replace('/','_');///////
- fullsavefilename=fullsavefilename.replace(':','@');
- //fullsavefilename=fullsavefilename.substring(47,
- //fullsavefilename.length());
- //定义文件类型
- Stringfiletype=newString("");
- filetype=filename.substring((filename.length()-3),
- filename.length());
- if(filetype.equals("doc")){
- System.out.println("--------/n当前正在转换......");
- //打印当前目录路径
- System.out.println(paths);
- //打印doc文件名
- System.out.println(realfilename);
- //启动word
- ActiveXComponentapp=newActiveXComponent(
- "Word.Application");
- Stringdocpath=paths+filename;
- Stringtxtpath=savepaths+fullsavefilename+".htm";
- //格式化文件名
- txtpath=formatname(txtpath);
- //将中文文件名改为拼音
- txtpath=pinyin(path,txtpath);
- //要转换的word文件
- StringinFile=docpath;
- //txt文件
- StringtpFile=txtpath;
- booleanflag=false;
- try{
- //设置word不可见
- app.setProperty("Visible",newVariant(false));
- Objectdocs=app.getProperty("Documents").toDispatch();
- //打开word文件
- Objectdoc=Dispatch.invoke(
- (Dispatch)docs,
- "Open",
- Dispatch.Method,
- newObject[]{inFile,newVariant(false),
- newVariant(true)},newint[1])
- .toDispatch();
- //作为txt格式保存到临时文件
- Dispatch.invoke((Dispatch)doc,"SaveAs",
- Dispatch.Method,newObject[]{tpFile,
- newVariant(10)},newint[1]);
- Variantf=newVariant(false);
- Dispatch.call((Dispatch)doc,"Close",f);
- flag=true;
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- app.invoke("Quit",newVariant[]{});
- }
- System.out.println("转化完毕!/n--------");
- }
- }else{
- pathss=paths;
- //进入下一级目录
- pathss=pathss+lists[i].getName()+"//";
- //递归遍历所有目录
- change(path,pathss,savepaths);
- }
- }
- }
- publicstaticStringformatname(Stringname){
- name=name.replace('、','_');
- name=name.replace(')',')');
- name=name.replace('(','(');
- returnname;
- }
- publicstaticStringpinyin(Stringpath,Stringhanzi){
- Filef=newFile(path);
- Stringpinyin=newString("");
- pinyin=hanzi;
- try{
- FileReaderfr=newFileReader(f);
- BufferedReaderbr=newBufferedReader(fr);
- Stringline=newString("");
- line=br.readLine();
- intcount=0;
- intlocation=0;
- while(line!=null){
- while(pinyin.indexOf(line.charAt(0))!=-1){
- location=pinyin.indexOf(line.charAt(0));
- pinyin=pinyin.substring(0,location)
- +line.substring(1,2)
- +pinyin.substring(location+1,pinyin.length());
- }
- line=br.readLine();
- System.out.print("/b/b/b/b/b/b"+count++);
- }
- fr.close();
- br.close();
- }catch(Exceptione){
- //System.err.println(e);
- }
- returnpinyin;
- }
- publicstaticvoidmain(String[]args){
- Stringpaths=newString("e://360Downloads//");
- Stringsavepaths=newString("e://360Downloads//Video//");
- Stringpath=newString("e://360Downloads//pinyin.doc");
- change(path,paths,savepaths);
- }
- }
下载jacob.rar,解压出jacob.dll和jacob.jar
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/833580