FileNotFoundException (Access Denied) when trying to write to a new file

My goal

I am trying to write simple objects ( SimpleType

) to files so that files can be loaded later and objects recreated.

My setup

I am currently working in NetBeans IDE (JDK8) on a Windows 7 machine. I don't think this should make a difference.

This is the type I would like to write to the file:

public class SimpleType implements Serializable {
    boolean[] a;
    boolean[] b;
}

      

This is the code I'm trying to run:

public class Test {
    public static void main(String[] args)
        throws IOException, ClassNotFoundException {
        String fileName = "test.txt";
        SimpleType foo = new SimpleType;
        try (ObjectOutputStream out = new ObjectOutputStream(new
             BufferedOutputStream(new FileOutputStream(fileName)))) {
            out.writeObject(foo);
            out.close();
        }
    }
}

      

My problem

The code compiles and runs, but it always produces FileNotFoundException

:

Exception in thread "main" java.io.FileNotFoundException: test.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
    at Test.main(Test.java:33)

      

My attempts to fix it

  • According to the documentation, I expect the file to be created if it doesn't already exist. I have carefully read the Javadoc for the method I am trying to use, an excerpt from which I am citing here (emphasis mine):

public FileOutputStream(String name) throws FileNotFoundException

[...]

Parameters:
name

- file system name
Throws:
FileNotFoundException

- if the file exists, but is rather a directory than a regular file, does not exist but cannot be created , or cannot be opened for any other reason SecurityException

- if a security manager exists and its checkWrite method denies access to write to the file.

  • I'm pretty sure I have read / write permissions on the directory; there is no existing file named test.txt, so it cannot be blocked by another program.

  • Changing fileName

    to an absolute path that I'm sure I can write to doesn't make any difference.

+3


source to share


2 answers


It plays if the file is in read-only mode. Can you try this?

public static void main(String[] args) {
      String fileName = "sampleObjectFile.txt";
      SampleObject sampleObject = new SampleObject();
      File file = new File(fileName);
      file.setWritable(true); //make it writable.
      try(ObjectOutputStream outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))){
           outputStream.writeObject(sampleObject);
           outputStream.close();
      } catch (IOException e) {
           e.printStackTrace();
      }
 }

      



If you are writing a file on an OS disk, you need administrator rights. so avoid writing to the OS disk.

+2


source


This usually happens because you are trying to write in a location that is not allowed by your filesystem (for example, on Windows7, you cannot write a new file in c :). Try investigating where the program is trying to write using procmon from Microsoft SysInternals . Add a new filter (path contains test.txt) and see what happens.



0


source







All Articles