问题
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