日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

用java遍历所有文件夹,将word文件转换为txt格式

發(fā)布時(shí)間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用java遍历所有文件夹,将word文件转换为txt格式 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

用Java代碼,遍歷文件夾及子文件夾,將其中的doc和docx文件批量轉(zhuǎn)換為txt格式

文件夾結(jié)構(gòu)

bbb文件夾

?ccc文件夾

加幾個(gè)除word外的干擾項(xiàng)

?ddd文件夾

?依賴

<dependencies><dependency><groupId>com.jacob</groupId><artifactId>jacob</artifactId><version>1.19</version><scope>system</scope><systemPath>${basedir}/src/main/resources/lib/jacob.jar</systemPath></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.10-FINAL</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.10.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.10.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.10.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.9</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>xdocreport</artifactId><version>2.0.1</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document</artifactId><version>2.0.1</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.core</artifactId><version>1.0.6</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.pdf</artifactId><version>1.0.6</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId><version>1.0.6</version></dependency></dependencies>

代碼

主方法

這里需要說(shuō)明的是,同一文件夾中可能會(huì)存在A.doc和A.docx這樣的情況,當(dāng)轉(zhuǎn)換為A.txt時(shí)后轉(zhuǎn)換的會(huì)覆蓋掉先前轉(zhuǎn)換的,所以定義了兩個(gè)路徑輸出到不同文件夾中來(lái)解決(當(dāng)然也可以在文件名后加隨機(jī)數(shù)等方法),不過(guò)這里就先不考慮這個(gè)問(wèn)題,暫時(shí)寫(xiě)一個(gè)路徑,可以根據(jù)自己需求修改

public static void main(String[] args) throws Exception {try { // 定義word文件所在路徑String path="F:\\bbb"; // 定義輸出txt文件所在路徑String outdocPath = "F:\\zzz";String outdocxPath = "F:\\zzz";path = URLDecoder.decode(path, "UTF-8"); // 調(diào)用方法,遍歷文件夾LinkedList<File> files = EveryFile.GetDirectory(path);for (int i = 0; i < files.size(); i++) { // word文件所在路徑String filesName = String.valueOf(files.get(i)); // word文件名String fileName=files.get(i).getName(); // 調(diào)用方法,進(jìn)行轉(zhuǎn)換WordToTxt.word2txt(filesName,fileName,outdocPath,outdocxPath);}System.out.println("轉(zhuǎn)換完畢");} catch (UnsupportedEncodingException e) {e.printStackTrace();}}

遍歷文件夾代碼

/*** 遍歷文件夾* @param path* @return*/public static LinkedList<File> GetDirectory(String path) {File file = new File(path);LinkedList<File> Dirlist = new LinkedList<File>(); // 保存待遍歷文件夾的列表LinkedList<File> fileList = new LinkedList<File>();GetOneDir(file, Dirlist, fileList);// 調(diào)用遍歷文件夾根目錄文件的方法File tmp;while (!Dirlist.isEmpty()) {tmp = (File) Dirlist.removeFirst();// 從文件夾列表中刪除第一個(gè)文件夾,并返回該文件夾賦給tmp變量// 遍歷這個(gè)文件夾下的所有文件,并把GetOneDir(tmp, Dirlist, fileList);}return fileList;}// 遍歷指定文件夾根目錄下的文件private static void GetOneDir(File file, LinkedList<File> Dirlist,LinkedList<File> fileList) {// 每個(gè)文件夾遍歷都會(huì)調(diào)用該方法File[] files = file.listFiles();if (files == null || files.length == 0) {return;}for (File f : files) {if (f.isDirectory()) {Dirlist.add(f);} else {// 這里列出當(dāng)前文件夾根目錄下的所有文件,并添加到fileList列表中fileList.add(f);// System.out.println("file==>" + f);}}}

word轉(zhuǎn)txt代碼

/*** 將word轉(zhuǎn)換為txt** @param filesName word文件絕對(duì)路徑 F:\bbb\ddd\周五.docx* @param fileName word文件名 周五.docx* @param outdocPath doc文件轉(zhuǎn)txt輸出路徑* @param outdocxPath docx文件轉(zhuǎn)txt輸出路徑* @throws Exception*/public static void word2txt(String filesName, String fileName, String outdocPath, String outdocxPath) throws Exception {String fileType = new String("");fileType = filesName.substring(filesName.length() - 4, filesName.length());if (fileType.equals("docx")) { // 要轉(zhuǎn)換的文檔全路徑String docxPath = filesName; // 轉(zhuǎn)換后的文檔全路徑String docxtotxtPath = outdocxPath + "/" + fileName.substring(0, fileName.length() - 5) + ".txt";//得到.docx文件提取器XWPFWordExtractor docx = new XWPFWordExtractor(POIXMLDocument.openPackage(docxPath));//提取.docx正文文本String text = docx.getText();FileWriter writer = new FileWriter(docxtotxtPath);writer.write(text);writer.close();} else if (fileType.equals(".doc")) { // 要轉(zhuǎn)換的文檔全路徑String docPath = filesName; // 轉(zhuǎn)換后的文檔全路徑String doctotxtPath = outdocPath + "/" + fileName.substring(0, fileName.length() - 4) + ".txt";InputStream is = new FileInputStream(docPath);HWPFDocument wordDocument = new HWPFDocument(is);WordToTextConverter converter = new WordToTextConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());//對(duì)HWPFDocument進(jìn)行轉(zhuǎn)換converter.processDocument(wordDocument);Writer writer = new FileWriter(new File(doctotxtPath));Transformer transformer = TransformerFactory.newInstance().newTransformer();transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");//是否添加空格transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty(OutputKeys.METHOD, "text");transformer.transform(new DOMSource(converter.getDocument()),new StreamResult(writer));}}

運(yùn)行結(jié)果

?后記

word文件中的圖片轉(zhuǎn)換到txt后將不會(huì)保留。

代碼一定存在可以優(yōu)化的地方,一是水品有限,二是手頭還有其他的事要忙,目前這些代碼滿足需要,所以暫時(shí)沒(méi)有修改。各位大佬使用的時(shí)候根據(jù)自己需要修改,不足之處歡迎批評(píng)指正。

總結(jié)

以上是生活随笔為你收集整理的用java遍历所有文件夹,将word文件转换为txt格式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。