gpt4 book ai didi

java - 如何将导出的 java 对象的格式更改为文件?

转载 作者:行者123 更新时间:2023-12-01 19:34:51 28 4
gpt4 key购买 nike

我正在尝试将 java 对象导出到 txt 文件中,但该文件中的输出格式不正确并且包含不必要的数据。如果有人能告诉我我做错了什么。这是代码的简单示例:

import java.io.Serializable;

public class Test implements Serializable{

private static final long serialVersionUID = 1L;
private String shortName;
private String fullName;


public Test(String shortName, String fullName) {
this.shortName=shortName;
this.fullName=fullName;
}

public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}

@Override
public String toString() {
return "Name:" + shortName + "\nFullName: " + fullName;
}

}

这是该方法的一部分:

FileOutputStream outputStream = new FileOutputStream(fullPath);
ObjectOutputStream o = new ObjectOutputStream(outputStream);
Test test = new Test("Short name of test","Full name of test");
o.writeObject(test);
o.close();
outputStream.close();

这就是我在文件中得到的内容:

’ sr &com.testing.project.Evaluation.model.Test        L fullNamet Ljava/lang/String;L     shortNameq ~ xpt Full name of testt Short name of test

我将不胜感激任何形式的帮助。

最佳答案

您正在使用 Java 序列化,它将对象写入其自己的二进制格式,而不是文本。如果您需要文本格式,我建议您使用 JSON,以及诸如 jackson-databind 之类的库。 .

为了方便起见,这里是一个工作示例(既写入文本文件又从文本文件读回对象):

主类

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, FileNotFoundException, IOException {
ObjectMapper om = new ObjectMapper();
Test test = new Test("Short name of test","Full name of test");
om.writeValue(new FileOutputStream("test.json"), test);

Test readValue = om.readValue(new FileInputStream("test.json"), Test.class);
System.out.println(readValue.getShortName());
System.out.println(readValue.getFullName());
}
}

测试类

import java.io.Serializable;

public class Test implements Serializable{

private static final long serialVersionUID = 1L;
private String shortName;
private String fullName;

public Test() {

}

public Test(String shortName, String fullName) {
this.shortName=shortName;
this.fullName=fullName;
}

public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}

@Override
public String toString() {
return "Name:" + shortName + "\nFullName: " + fullName;
}
}

请注意,我已向 Test.class 添加了一个默认构造函数,以便 Jackson 可以在从 json 反序列化时创建它。

关于java - 如何将导出的 java 对象的格式更改为文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214004/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com