首页 > 解决方案 > CSV 文件无法在 Mac 计算机上正确打开

问题描述

我正在使用 jackson-dataformat-csv 库生成 csv 文件,但我无法在 Mac 计算机上正确打开它。这是生成csv的java代码,我不知道我在这里做错了什么:

import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CsvDownload {
    public static void main(String[] args) throws  IOException {

        User user1 = new User("Cow",25, "xxxxxx@gmail.com","1234567890",
                "Street","Heights","7100",
                "Male","Town");

        User user2 = new User("John",26, "xxxxxxx@gmail.com","+1234567890",
                "Street","Heights","7100",
                "Male","Town");


        List<User> users = new ArrayList<User>();
        users.add(user1);
        users.add(user2);

        // create mapper and schema
        CsvMapper mapper = new CsvMapper();
        CsvSchema schema = mapper.schemaFor(User.class).withHeader();
        schema = schema.withColumnSeparator(',');

        // output writer
        ObjectWriter myObjectWriter = mapper.writer(schema);
        File tempFile = new File("users.csv");
        FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);
        BufferedOutputStream bufferedOutputStream = new 
        BufferedOutputStream(tempFileOutputStream, 1024);
        OutputStreamWriter writerOutputStream = new 
        OutputStreamWriter(bufferedOutputStream, "UTF-8");
        myObjectWriter.writeValue(writerOutputStream, users);
    }
}

型号类:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder(value = {"name", "age", "email", "phone_number",
    "physical_address", "postal_address", "postal_code",
    "gender", "city"})
public class User {
    @JsonProperty("Name")
    private String name;
    @JsonProperty("Age")
    private int age;
    @JsonProperty("Email Address")
    private String email;
    @JsonProperty("Phone Number")
    private String phone_number;
    @JsonProperty("Physical Address")
    private String physical_address;
    @JsonProperty("Postal Address")
    private String postal_address;
    @JsonProperty("Postal Code")
    private String postal_code;
    @JsonProperty("Gender")
    private String gender;
    @JsonProperty("City")
    private String city;

    public User(String name, int age, String email, String phone_number, String 
    physical_address, String postal_address, String postal_code, String gender, 
    String city) {
        this.name = name;
        this.age = age;
        this.email = email;
        this.phone_number = phone_number;
        this.physical_address = physical_address;
        this.postal_address = postal_address;
        this.postal_code = postal_code;
        this.gender = gender;
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getPhysical_address() {
        return physical_address;
    }

    public void setPhysical_address(String physical_address) {
        this.physical_address = physical_address;
    }

    public String getPostal_address() {
        return postal_address;
    }

    public void setPostal_address(String postal_address) {
        this.postal_address = postal_address;
    }

    public String getPostal_code() {
        return postal_code;
    }

    public void setPostal_code(String postal_code) {
        this.postal_code = postal_code;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

这是我用来生成 csv 文件但在 Mac 计算机上无法正确打开的代码,我在 Windows 上没有问题。 屏幕图像

标签: javacsvjackson

解决方案


推荐阅读