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.