2011-12-13 42 views
1

如何使用相同的名稱使用POI API如何使用POI API保存Word文檔?

public class readDoc { 
    public static void main(String[] args) { 
     String filesname = "Hello.doc"; 
     POIFSFileSystem fs = null; 
     try { 
      fs = new POIFSFileSystem(new FileInputStream(filesname; 
      //Couldn't close the braces at the end as 
      my site did not allow it to close 
      HWPFDocument doc = new HWPFDocument(fs); 
      WordExtractor we = new WordExtractor(doc); 
      String[] paragraphs = we.getParagraphText(); 
      System.out.println("Word Document has " + 
      paragraphs.length + " paragraphs"); 
      for(int i=0; i<paragraphs .length; i++) { 
       System.out.println("Length:"+paragraphs[i].length()); 
      } 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+1

,我以爲他是問「如何拯救世界」:d – FUD

回答

2
FileOutputStream fos = new FileOutputStream(filesname); 
doc.write(fos); 
fos.close(); 

你可以嘗試寫POIFsFileSystem以及保存文檔文件。

FileOutputStream fos = new FileOutputStream(filesname); 
fs.write(fos); 
fos.close(); 
+0

請你可以加我上面的代碼在我的代碼,並張貼在同一... – user1053424

+0

把它放在後for循環。 –