Apache POI XWPF Docx损坏的文件

时间:2012-06-18 13:02:32

标签: java apache apache-poi docx

我尝试打开现有的文本文件,阅读它,然后使用Apache POI XWPF将文本写入2007 Word Doc(DOCX)。该文件已成功创建,但是,在尝试打开它时,Word会建议该文件已损坏。

我是Apache POI的新手。下面的函数中是否有明显的错误?

private static void writeTextToDocument(String readFileName, String writeFileName){
    BufferedReader br = null;
    XWPFDocument document = new XWPFDocument();
    try {
        String sCurrentLine;
        XWPFParagraph paragraphOne = document.createParagraph();
        XWPFRun paragraphOneRunOne = paragraphOne.createRun();
        br = new BufferedReader(new FileReader(readFileName));

        while ((sCurrentLine = br.readLine()) != null) {
            paragraphOneRunOne.setText((translateText(accessToken, sCurrentLine))); 
            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(writeFileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                document.write(outStream);
                outStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }   
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

0 个答案:

没有答案