實例講解Java讀取一般文本文件和word文檔的方法
更新時間:2016年06月09日 08:59:48 作者:it_wangxiangpan
讀取一般文本文件很好辦,調(diào)用Java自帶的io包里的類即可,富文本的doc文件我們可以用Apache的poi項目中的WordExtractor,這里我們一起來以實例講解Java讀取一般文本文件和word文檔的方法
一般文本文件
我們以日志文件.log文件為例:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class File_Test {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("D:\\logserrorMsg.log");
if(file.exists()){
System.out.println("此文件存在");
} else {
System.out.println("此文件不存在");
}
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s;
while((s=br.readLine())!=null){
System.out.println(s);
}
System.out.println("文件大小為(MB):"+new FileInputStream(file).available() / 1024 / 1024 +"M");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
.doc文件
這里我們使用WordExtractor讀取Word文檔,WordExtractor來自于Apache的poi類庫項目,官方下載地址:https://poi.apache.org/download.html
import java.io.FileInputStream;
import org.textmining.text.extraction.WordExtractor;
public class WordTest {
public static void main(String args[]) throws Exception {
new WordTest().readByOther();
}
public void readByText() throws Exception {
FileInputStream in = new FileInputStream("C://test.doc ");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
System.out.println(str);
}
}
相關(guān)文章
Spark SQL關(guān)于性能調(diào)優(yōu)選項詳解
這篇文章將為大家詳細(xì)講解有關(guān)Spark SQL性能調(diào)優(yōu)選項,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲2023-02-02
使用dom4j實現(xiàn)xml轉(zhuǎn)map與xml轉(zhuǎn)json字符串
這篇文章主要為大家詳細(xì)介紹了如何使用dom4j實現(xiàn)xml轉(zhuǎn)map與xml轉(zhuǎn)json字符串功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-11-11
springboot接口接收數(shù)組及多個參數(shù)的問題及解決
這篇文章主要介紹了springboot接口接收數(shù)組及多個參數(shù)的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
SpringBoot實現(xiàn)阿里云短信接口對接的示例代碼
這篇文章主要介紹了SpringBoot實現(xiàn)阿里云短信接口對接的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

