87 lines
3.0 KiB
Java
87 lines
3.0 KiB
Java
package com.ruoyi.common.utils;
|
|
|
|
import org.apache.poi.hwpf.extractor.WordExtractor;
|
|
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
|
import java.io.*;
|
|
|
|
/**
|
|
* @author wangqiong
|
|
* @description 读取文件内容
|
|
* @date 2023-12-11 11:45
|
|
*/
|
|
public class ReadFileUtils {
|
|
|
|
|
|
public static String readerTxtFile(String filePath){
|
|
BufferedReader br=null;
|
|
StringBuilder result=new StringBuilder();
|
|
try {
|
|
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)),"GBK"));
|
|
String line=null;
|
|
while ((line=br.readLine())!=null) {
|
|
result.append(line).append("\n");
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (null!=br){
|
|
try {
|
|
br.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
return result.toString();
|
|
}
|
|
public static String readWord(String filePath) throws Exception{
|
|
|
|
File file = new File(filePath);
|
|
if(file.length()==0) return ""; // 需要操作原因是可能会空文件问题,如果不做处理,在下面读取中会报错
|
|
StringBuffer sb = new StringBuffer();
|
|
String buffer = "";
|
|
try {
|
|
if (filePath.endsWith(".doc")) {
|
|
InputStream is = new FileInputStream(file);
|
|
WordExtractor ex = new WordExtractor(is);
|
|
buffer = ex.getText();
|
|
if(buffer.length() > 0){
|
|
//使用回车换行符分割字符串
|
|
String [] arry = buffer.split("r\\n");
|
|
for (String string : arry) {
|
|
sb.append(string.trim());
|
|
}
|
|
}
|
|
} else if (filePath.endsWith(".docx")) {
|
|
FileInputStream fis = new FileInputStream(file);
|
|
XWPFDocument xdoc = new XWPFDocument(fis);
|
|
XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
|
|
buffer = extractor.getText();
|
|
sb.append(buffer!=null?buffer:"");
|
|
|
|
|
|
// OPCPackage opcPackage = POIXMLDocument.openPackage(filePath);
|
|
// XWPFWordExtractor extractor = new XWPFWordExtractor(opcPackage);
|
|
// buffer = extractor.getText();
|
|
// if(buffer.length() > 0){
|
|
// //使用换行符分割字符串
|
|
// String [] arry = buffer.split("\n");
|
|
// for (String string : arry) {
|
|
// sb.append(string.trim());
|
|
// }
|
|
// }
|
|
} else {
|
|
return null;
|
|
}
|
|
return sb.toString();
|
|
} catch (Exception e) {
|
|
System.out.print("error---->"+filePath);
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
}
|