FileOutputStream access is denied : JAVA

mortiped picture mortiped · Feb 25, 2014 · Viewed 43.5k times · Source

I have the following code with the iText library properly integrated.

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(); 
    }
}

This code returns me an error message, which is as follows.

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)

Yet I am the computer administrator and I normally have all permissions account. I don't understand why he retourn me Access is denied.

Answer

Kick picture Kick · Feb 25, 2014

You are trying to access the directory. The parameter of the FileOutputStream should be a File/ Path object pointing to a file:

 FileOutputStream file  = new FileOutputStream("path/file.txt");
                   File -------------------------------^

For more detail take a look on http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html