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

原始路径下文件列表:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值