java - 如何将导出的 java 对象的格式更改为文件?
问题描述
我正在尝试将 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 反序列化时创建它。
推荐阅读
- unity3d - 管理 Unity 3D 生成的 Assembly-C-Sharp.dll 上的版本
- python - 使用 pandas.ExcelWriter 进行条件格式设置:无法手动更改格式并使用 openpyxl 读取格式
- python - 如何修复握手失败和套接字通信中没有共享密码错误
- sql-server - 如何在 hbernate 中获取 nVarchar 的实际值
- xaml - 如何在 MasterPage 中选择的项目上显示选择器?
- java - 有没有一种功能性的方法来遍历基于最后处理的项目的流累积和过滤?
- c# - 如何自动更改新 .zip 文件的名称?
- activerecord - 如何将数据数组保存到数据库
- swift - 如何引用作为关联类型的协议的属性?
- react-native - 有没有办法更改顶部选项卡导航器的字体颜色和下划线颜色?