Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileOutputStream access is denied : JAVA

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.

like image 853
mortiped Avatar asked Feb 25 '14 07:02

mortiped


2 Answers

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

like image 158
Kick Avatar answered Sep 29 '22 02:09

Kick


You need to have permission to access that file location. There are two possible solutions.

1. use deferent file location to store your file (eg: D:\\somewhere)  
2. make sure that you have permission to access current location by granting 
   read write permissions. 
like image 35
Ruchira Gayan Ranaweera Avatar answered Sep 29 '22 00:09

Ruchira Gayan Ranaweera