我想念序列化​​演示简单的东西吗

发布于 2025-01-21 19:14:48 字数 1874 浏览 4 评论 0原文

因此,我正在尝试在更加速度的程序上实现序列化,但是它没有起作用,因此我尝试了一个简单的测试,当我尝试写入我的.ser文件

main类

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();
    }
}

类我想序列化

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";
    }
}

错误消息

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)

这可能是一个非常简单的修复程序,但我完全损失了。谢谢!

Update

我正在运行Windows 11,并且该文件位于Workspace目录中(test \ Person.ser.ser.ser),Java文件在test \ src \ 目录及其从test \ bin \每个Visual Studio代码默认值开始。仅阅读是未选中的,我尝试过以管理员的身份运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾挽 2025-01-28 19:14:48

您的Java代码从根本上没有任何错误。当我在(Linux)机器上运行它时,它就可以工作。没有错误。按预期,在当前目录中创建“ person.ser”。 应该在窗口上表现相同。

因此,您的问题与您的计算机以及您运行App代码的方式有关。操作系统告诉您您不允许编写该文件。

但这不仅仅是文件权限。当我(故意)制作“ person.ser”文件不写时,我会得到与您的不同的叠加:

$ 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)

请注意,打开文件时会抛出异常。 这是我期望发生的事情。

但是在您的stacktrace中,文件打开(用于写作)已经成功,并且当您试图将序列化标题写入文件时,就会发生“许可拒绝”。

奇怪的。很奇怪。

我怀疑当前目录和 /或其居住的文件系统有些奇怪。是在远程安装的共享上吗?是在可移动设备上吗?它是文件系统本身设置为仅读数吗?它是不寻常的文件系统类型(例如NTFS)吗?您是否在具有某种“强制性访问控制”硬化的系统上运行它会阻止一些写入?也许是过于侵略性的反病毒应用程序?

当您运行代码时,在预期位置创建了一个空的“ person.ser”文件?您可以删除它然后重试吗?这可以重复吗?

您能为当前目录中的“ person.ser.ser”写入(说)“ hello world”的简单程序? 有效吗?

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?

逆夏时光 2025-01-28 19:14:48

我刚刚出厂擦拭了我的PC,现在以前的代码工作... IDK如果其他人偶然发现了这一点,则可能是您的JDK配置或其他某些安全设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文