java 判断word损坏 java判断word版本
摘要:java怎么判断一个文件是否存在File file = new File("文件路径");if (file exists()) 这里可以判定是否存在if(file isFile()) 判断是...
发布日期:2020-11-23java怎么判断一个文件是否存在
File file = new File("文件路径");if (file.exists()) // 这里可以判定是否存在if(file.isFile())// 判断是否是一个标准文件if(file.isDirectory())// 判断是否是文件夹if(file.isHidden())// 判断是否是隐藏文件
java判断word是否是03版本
请贴出代码,谢谢。
请关闭输入流,释放资源,谢谢。
调用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 如何判断文件路径是否存在
existspublic boolean exists()测试此抽象路径名表示的文件或目录是否存在。
返回:当且仅当此抽象路径名表示的文件或目录存在时,返回 true;否则返回 false isFilepublic boolean isFile()测试此抽象路径名表示的文件是否是一个标准文件。
如果该文件不是一个目录,并且满足其他与系统有关的标准,那么该文件是标准 文件。
由 Java 应用程序创建的所有非目录文件一定是标准文件。
返回:当且仅当此抽象路径名表示的文件存在且 是一个标准文件时,返回 true;否则返回 false
求教用java 生成word!!!
1-apache的POI,此方法对Excel的导出做的很好,目前对Word的导出方面的功能尚未完全。
2-纯JavaScript脚本实现。
主要通过客户端调用本机Office组件来实现。
3-在JSP页面引入头文件实现。
纯JavaScript脚本实现细节方面大体是创建一个word组件ActiveXObject("Word.Application"),用js通过表ID取得表内容然后保存到word,要注意的是js实现有很多不好的地方,例如Internet选项需要把ActiveX空间全部启用,安全级别设置为中。
这样的话岂不是每台机器都要配置一下。
其次每次生成word文档以后弹出对话框(无法保存此文件,因为它已在别处打开(C:\...\STARTUP\Powerword.dot)),出现此问题就需要把C:\Documents and Settings\当前用户名\Application Data\Microsoft\Word\STARTUP下的Powerword.dot文件删除,每次遇到此问题就需要删除文件来解决,十分不方便。
JSP页面引入来实现Word保存就方便多了,但是也有不足的地方,首先如果需要引入如果需要下载的话就引入其实如果大家用框架做就方便多了,比如Struts2。
在Action里直接写如下代码:if(out!=null){String fileName="";fileName+="评价报告.doc";try {HttpServletResponse response = ServletActionContext.getResponse();response.setHeader("Content-disposition","attachment; filename="+new String(fileName.getBytes("GB2312"), "8859_1"));} catch (UnsupportedEncodingException e) {e.printStackTrace();}out是jsp页面表单元素,一个button,用于提交表单到相应Action进行Word下载。
Action设置jsp页面头文件。
这样每次点击button就可以把相应jsp页面的内容保存到Word中并且支持下载,Word中内容并且是可编辑状态。
不足的地方在于由于表内容是动态生成,有的需要先查看在下载Word,就需要另外建立一个新JSP页面进行Word下载,当然首先要在struts.xml里配置好页面转向。
新建立的页面传值同查看页面要保持一样。
用Java写一个程序,判断txt文档中,一些词语出现的次数,我输入什...
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.Scanner;public class Test {public static void main(String[] args) throws Exception {String filePath = "C:/test.txt";Test test = new Test();try(Scanner scan = new Scanner(System.in)){System.out.println("Please input the source word.");String source = scan.nextLine();int times = test.appearedTimes(source, filePath, false);System.out.println(times);}}/*** * @param source The specific word you will input* @param filePath The TXT file"s full path* @param ignoreCase Whether ignore the word"s case* @return* @throws Exception*/public int appearedTimes(String source, String filePath, boolean ignoreCase) throws Exception {int times = 0;File file = new File(filePath);if(!file.exists() || !file.isFile()){throw new Exception("File not found or isn"t existing!");}try(BufferedReader reader = new BufferedReader(new FileReader(file));){String content = null;while((content = reader.readLine()) != null){times += timesPerLine(ignoreCase ? content.toLowerCase() : content, ignoreCase ? source.toLowerCase() : source);}}return times;}private int timesPerLine(String line, String source){int times = 0;if(line.indexOf(source) return 0;}if(line == null || line.length() == 0){return 0;}times ++;String restString = line.substring(line.indexOf(source) + source.length());times += timesPerLine(restString, source);return times;}}
java 怎么判断文件是否存在
经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式java读取word文档时,虽然网上介绍了很多插件poi,各种格式都可以设定、java2Word,不需要借助任何插件,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好法,操作方法完全一致,有的机器不报错,有的报错,比较诡异,我曾经在不同的机器上试过,做项目不太敢用);java2Word、jacob容易报错找不到注册,poi无法读取格式(新的API估计行好像还在处于研发阶段,不太稳定、itext等等、jacob,只需基本IO操作外加编码转换即可。
rtf格式文件表面看来和doc没啥区别,都可以用word打开。
具体代码可以参考:/...