首页 > 解决方案 > 如何将导出的 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

我将不胜感激任何帮助。

标签: javaserializationobjectoutputstream

解决方案


您正在使用 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 反序列化时创建它。


推荐阅读