12

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.

Chin
  • 19,717
  • 37
  • 107
  • 164
mortiped
  • 869
  • 1
  • 6
  • 28

7 Answers7

18

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

Vishrant
  • 15,456
  • 11
  • 71
  • 120
Kick
  • 4,823
  • 3
  • 22
  • 29
1

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. 
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Actually you are trying to access directory using FileOutputStream( ) means you are trying to access directory "C:\Users\administrator\Pictures\tuto" using -

public static final String RESULT = "C:\\Users\\administrator\\Pictures\\tuto";
new FileOutputStream(RESULT);

Which is wrong as the valid input which can be provided to FileOutputstream( ) is either file name (like "xyz.txt") or path of file (like "C:\sample\xyz.txt").

Use file name OR file path with FileOutputstream( ) and your problem will solve.

Thanks.

Bhanu
  • 663
  • 5
  • 13
0

I had a similar problem in which I unzipped a jar file which failed due to this error message. This jar was a jar with dependencies and I had recently added a new dependency. After examining the jar contents, it turned out I had a LICENSE file and a folder license in the same root. While this is completely valid on Linux, the Windows file system will barf. The work around in my case to was trap this error in a try/catch. In the catch, check if you're on windows, if so log warning as there's not much that can be done, otherwise throw.

spy
  • 3,199
  • 1
  • 18
  • 26
0

Not the answer from this question

I got the same exception because Windows is not case sensitive.
Trying to create one file named "test" and other named "TEST" will generate the same exception.

0

You can try this:

if(!file.canRead()){
    file.setReadable(true);
 }

 FileOutputStream file  = new FileOutputStream("path/file.txt");
Dino
  • 7,779
  • 12
  • 46
  • 85
0

by this, you can change access to your file or folder dynamically. Note: this will work on Linux machine only.

private void filePermissions(File filePath) throws IOException {

        Path path = Paths.get(filePath.toString());

        Set<PosixFilePermission> perms = java.nio.file.Files.readAttributes(path, PosixFileAttributes.class)
                .permissions();

        perms.add(PosixFilePermission.OWNER_WRITE);
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        perms.add(PosixFilePermission.GROUP_WRITE);
        perms.add(PosixFilePermission.GROUP_READ);
        perms.add(PosixFilePermission.GROUP_EXECUTE);
        perms.add(PosixFilePermission.OTHERS_WRITE);
        perms.add(PosixFilePermission.OTHERS_READ);
        perms.add(PosixFilePermission.OTHERS_EXECUTE);
        java.nio.file.Files.setPosixFilePermissions(path, perms);

    }
David Buck
  • 3,752
  • 35
  • 31
  • 35