實(shí)例講解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來(lái)自于Apache的poi類庫(kù)項(xiàng)目,官方下載地址: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)文章
Mybatis?如何開(kāi)啟控制臺(tái)打印sql語(yǔ)句
這篇文章主要介紹了Mybatis?如何開(kāi)啟控制臺(tái)打印sql語(yǔ)句問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
SpringMVC KindEditor在線編輯器之文件上傳代碼實(shí)例
這篇文章主要介紹了SpringMVC KindEditor在線編輯器之文件上傳代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Java并發(fā)底層實(shí)現(xiàn)原理學(xué)習(xí)心得
本片文章是學(xué)習(xí)Java并發(fā)底層實(shí)現(xiàn)原理的一篇知識(shí)心得,對(duì)大家學(xué)習(xí)這個(gè)方便的知識(shí)很有幫助,一起參考下。2018-01-01
Maven中pom.xml文件報(bào)錯(cuò)的原因解決
創(chuàng)建Maven項(xiàng)目的時(shí)候,如果你選擇的Packaging為war,那么就會(huì)報(bào)錯(cuò),本文主要介紹了Maven中pom.xml文件報(bào)錯(cuò)的原因解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Resty極簡(jiǎn)restful框架快速接入Spring
這篇文章主要為大家介紹了Resty極簡(jiǎn)的restful框架快速接入Spring詳細(xì)說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
詳解Springboot如何通過(guò)注解實(shí)現(xiàn)接口防刷
本文主要為大家介紹一種極簡(jiǎn)潔、靈活通用接口防刷實(shí)現(xiàn)方式、通過(guò)在需要防刷的方法加上@Prevent?注解即可實(shí)現(xiàn)短信防刷,感興趣的可以了解一下2022-09-09
SpringBoot實(shí)現(xiàn)動(dòng)態(tài)加載外部Jar流程詳解
這篇文章主要介紹了SpringBoot動(dòng)態(tài)加載外部Jar的流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-05-05
springboot+nginx+https+linux實(shí)現(xiàn)負(fù)載均衡加域名訪問(wèn)簡(jiǎn)單測(cè)試
這篇文章主要介紹了springboot+nginx+https+linux實(shí)現(xiàn)負(fù)載均衡加域名訪問(wèn)簡(jiǎn)單測(cè)試,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05

