1. 程式人生 > >POI 輸出word列表的內容(Doc2String)

POI 輸出word列表的內容(Doc2String)

對於word列表,如何從列表中將資料輸出,這是在查詢的過程中首先需要做的:

package org.algorithm;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;


public class Doc2String {

    public static String doc2String
(File file){ String result = " "; try{ FileInputStream fis = new FileInputStream(file); HWPFDocument doc = new HWPFDocument(fis); Range rang = doc.getRange(); result += rang.text(); fis.close(); }catch(Exception e){ e.printStackTrace(); } return
result; } public static void main(String[] args) { String path = "e:\\luceneDoc"; List<File> fileList = getFileList(path);//獲取路徑下的檔案列表 for(File file: fileList){ String filename = file.getName();//獲取檔名稱 String result = doc2String(file);//將檔案內容轉換成字串型別
System.out.println(filename+":"+"\r\n"+result); } } public static List<File> getFileList(String path){ File[] files = new File(path).listFiles();//輸入檔案列表 List<File> fileList = new ArrayList<File>();//建立一個集合陣列物件,用於儲存檔案資訊 for(File file: files){ fileList.add(file); } return fileList; } }

Output:

1.doc:
 There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.

10.doc:
 you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.
  May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human

2.doc:
 May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.

3.doc:
 The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people

4.doc:
 who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can’t go on well in lifeuntil you let go of your past failures and heartaches.

5.doc:
 When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you're the one who is smiling and everyone around you is crying.

6.doc:
 Please send this message to those people who mean something to you,to those who have touched your life in one way or another,to those who make you smile when you really need it,to

7.doc:
 those that make you see the brighter side of things when you are really down,to those who you want to let them know that you appreciate their friendship.And if you don’t

8.doc:
 don’t worry,nothing bad will happen to you,you will just miss out on the opportunity to brighten someone’s day with this message.

9.doc:
 who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people
  who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will

原始路徑下檔案列表:

這裡寫圖片描述