我有以下代码与iText库正确集成.
import java.io.*; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfWriter; @org.eclipse.jdt.annotation.NonNullByDefault(true) public class HelloWorld { public static final String RESULT = "C:\\Users\\administrator\\Pictures\\tuto"; @SuppressWarnings("resource") public static void main(String[] args) throws DocumentException,IOException { Document document = new Document(); PdfWriter.getInstance(document,new FileOutputStream(RESULT)); document.open(); document.add(new Paragraph("Hello World!")); document.close(); } }
Exception in thread "main" java.io.FileNotFoundException: C:\Users\valentin.schaefer\Pictures\tuto (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at HelloWorld.main(HelloWorld.java:25)
然而,我是计算机管理员,我通常拥有所有权限帐户.我不明白他为什么要退我访问被拒绝.
解决方法
您正在尝试访问该目录. FileOutputStream的参数应该是指向文件的File / Path对象:
FileOutputStream file = new FileOutputStream("path/file.txt"); File -------------------------------^
有关详细信息,请查看http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html