Java Swing Apache POI make Word Document read only

五迷三道 提交于 2021-02-19 23:15:56

问题


Hello I am creating a Swing application to create a word document. I have made use of the Apache POI Java API for this purpose.

But the problem is --> How do I set the word document to be read only after creation?

I have heard of java.io.File.setReadOnly() method, but I don't know how to use it in this context.

Here is the code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       try{  
           FileOutputStream outStream=new FileOutputStream("New.docx");
           XWPFDocument doc =new XWPFDocument();

           XWPFParagraph para1=doc.createParagraph();
           para1.setAlignment(ParagraphAlignment.CENTER);
           XWPFRun pararun1= para1.createRun();
           pararun1.setBold(true);
           pararun1.setFontSize(16);
           pararun1.setText(jLabel1.getText());
           pararun1.addBreak();

           XWPFParagraph para2=doc.createParagraph();
           para2.setAlignment(ParagraphAlignment.RIGHT);
           XWPFRun pararun2= para2.createRun();
           pararun2.setFontSize(12);
           pararun2.setText("Date : " +jTextField2.getText());
           pararun2.addBreak();

           doc.write(outStream);
           outStream.close();

       } catch(Exception e){
           e.printStackTrace();
       }
}           

Since I am using the FileOutputStream instead of File here, how do you suggest I make the word file as read only after it is created?

来源:https://stackoverflow.com/questions/47767524/java-swing-apache-poi-make-word-document-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!