Reputation: 90
So, I am trying to implement serialization on a more andvanced program but it didnt work, so i tried a simple test and this still gives a Access Is Denied error when i try to write to my .ser file
MAIN CLASS
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class App implements Serializable {
public static void main(String[] args) throws Exception {
person p = new person("John", 30);
FileOutputStream fos = new FileOutputStream("person.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p);
oos.close();
fos.close();
}
}
CLASS I WANT TO SERIALIZE
import java.io.Serializable;
public class person implements Serializable {
private String name;
private int age;
person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "helloworld";
}
}
ERROR MESSAGE
Exception in thread "main" java.io.IOException: Access is denied
at java.base/java.io.FileOutputStream.writeBytes(Native Method)
at java.base/java.io.FileOutputStream.write(FileOutputStream.java:347)
at java.base/java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1891)
at java.base/java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1800)
at java.base/java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:251)
at App.main(App.java:10)
This is probably a very simple fix but im at a total loss. Thanks!
UPDATE
I am running Windows 11 and the file is in the workspace directory (Test\person.ser
), the java files are in Test\src\
directory and it starts in the Test\bin\
directory per Visual Studio Code default. read only is unchecked and I've tried running as administrator.
Upvotes: 2
Views: 112
Reputation: 90
I just factory wiped my pc and now the previous code works... idk if someone else ever stumbles upon this it could be your JDK config or some other security setting.
Upvotes: 0
Reputation: 718936
There is nothing fundamentally wrong with your Java code. When I run it on my (Linux) machine, it just works. No errors. The "person.ser" is created in the current directory as expected. It should behave the same on Windows.
So your problem is something to do with your computer and the way that you are running the App
code. The operating system is telling you that you are not allowed to write that file.
But it is not simply file permissions. When I (deliberately) make the "person.ser" file not writable, I get a different stacktrace to yours:
$ java App
Exception in thread "main" java.io.FileNotFoundException: person.ser (Permission denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at App.main(App.java:9)
Note that the exception is thrown while opening the file. This is what I would expect to happen.
But in your stacktrace, the file open (for writing) has succeeded and the "Permission Denied" happens when you are trying to write the serialization header to the file.
Strange. Very strange.
I suspect there is something strange about the current directory and / or the file system where it resides. Is is it on a remote mounted share? Is it on a removable device? It the file system itself set to read-only? Is it an unusual file system type (e.g. not NTFS)? Are you running this on a system with some kind of "mandatory access control" hardening that would block some writes? An overly aggressive anti-virus application perhaps?
When you ran the code did an empty "person.ser" file get created in the expected location? Can you delete it and try again? Is this repeatable?
Can you write a simple program that writes (say) "hello world" to a text file called "person.ser" in the current directory? Does that work?
Upvotes: 1