FileOutputStream access is denied : JAVA
Asked Answered
P

7

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.

Pooch answered 25/2, 2014 at 7:55 Comment(5)
Does the directory exist?Lepidopteran
Did you try accessing file from somewhere outside user space? or different drive?Eloiseloisa
yes, the directory exist and the folder have fool permissionsPooch
it looks like you may be pointing to a directory. Try ensuring that the RESULT is pointed at a non existent file or one that can be edited and saved.Heartwood
offtopic: your logs contain your personal data. I suggest posting the logs anonymized.Lunna
E
20

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

Ergener answered 25/2, 2014 at 8:1 Comment(0)
A
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. 
Arsenault answered 25/2, 2014 at 7:58 Comment(0)
Z
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.

Zeitgeist answered 25/2, 2014 at 8:39 Comment(0)
C
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.

Catch answered 21/2, 2018 at 14:27 Comment(0)
N
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.

Naught answered 19/10, 2018 at 4:48 Comment(0)
D
0

You can try this:

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

 FileOutputStream file  = new FileOutputStream("path/file.txt");
Dearing answered 8/11, 2019 at 6:23 Comment(1)
Please avoid posting code only answer and provide some explanation to clarify your answer.Waly
D
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);

    }
Dearing answered 13/11, 2019 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.