使用 BufferedWriter 将 arrayList< model class> 写入 txt 文件

Write arrayList< model class> into txt file using BufferedWriter

我正在尝试从 enqueue 调用获取数据后从 arrayBundlePack 获取数据,我正在尝试使用 BufferedWriter 来保存 [=24= 中的数据]arrayBundlePack & 在txt文件中写入,但它不会得到我想要的输出。

我不确定这是否是输出 arrayList 模型的方式 class。

这是代码

private ArrayList<BundlePack> arrayBundlePack;

Call<BundlePackRepo> call =  ..............         // api content
    call.enqueue(new Callback<BundlePackRepo>() {
                    @Override
                    public void onResponse(Call<BundlePackRepo> call, retrofit2.Response<BundlePackRepo> response) {
                        if (response.isSuccessful()) {
                            BundlePackRepo repos = response.body();
                            BundlePackRepo.bundlepacks[] bundlePacksarray;
                            bundlePacksarray = repos.getBundlePacks();

                            BundlePack bundlePack;
                            for (int a = 0; a < bundlePacksarray.length; a++) {
                                bundlePack = new BundlePack();
                                bundlePack.setPromotion_kit(bundlePacksarray[a].getPromotion_kit());
                                bundlePack.setStock_code(bundlePacksarray[a].getStock_code());
                                bundlePack.setIssue_ref(bundlePacksarray[a].getIssue_ref());
                                bundlePack.setQuantity(bundlePacksarray[a].getQuantity());
                                String sysMod = bundlePacksarray[a].getSys_mod();
                                String modifyDate = bundlePacksarray[a].getModify_dt();
                                try {
                                    bundlePack.setSys_mod(df.parse(sysMod));
                                    bundlePack.setModify_dt(df.parse(modifyDate));
                                } catch (java.text.ParseException e) {
                                    e.printStackTrace();
                                }
                                bundlePack.setStatus(bundlePacksarray[a].getStatus());

                                arrayBundlePack.add(bundlePack);

                                                           //Trying to put out the data to text file

                                String filename = "bundlepack.txt";
                                File txtData = new File(getApplicationContext().getFilesDir(), filename);
                                FileWriter txtWriter = null;
                                try {
                                    txtWriter = new FileWriter(txtData);
                                    BufferedWriter fileWriter = new BufferedWriter(txtWriter);
                                    for (BundlePack bdpack : arrayBundlePack) {
                                        fileWriter.write(bdpack.toString());
                                        fileWriter.newLine();
                                        fileWriter.flush();
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

BundlePack

@Entity
public class BundlePack{
@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo(name = "promotion_kit")
private String promotion_kit;

@ColumnInfo(name = "stock_code")
private String stock_code;

@ColumnInfo(name = "issue_ref")
private String issue_ref;

@ColumnInfo(name = "quantity")
private Integer quantity;

@TypeConverters(DateConverter.class)
@ColumnInfo(name = "sys_mod")
private Date sys_mod;

@TypeConverters(DateConverter.class)
@ColumnInfo(name = "modify_dt")
private Date modify_dt;

@ColumnInfo(name = "status")
private String status;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getPromotion_kit() {
    return promotion_kit;
}

public void setPromotion_kit(String promotion_kit) {
    this.promotion_kit = promotion_kit;
}

public String getStock_code() {
    return stock_code;
}

public void setStock_code(String stock_code) {
    this.stock_code = stock_code;
}

public String getIssue_ref() {
    return issue_ref;
}

public void setIssue_ref(String issue_ref) {
    this.issue_ref = issue_ref;
}

public Integer getQuantity() {
    return quantity;
}

public void setQuantity(Integer quantity) {
    this.quantity = quantity;
}

public Date getSys_mod() {
    return sys_mod;
}

public void setSys_mod(Date sys_mod) {
    this.sys_mod = sys_mod;
}

public Date getModify_dt() {
    return modify_dt;
}

public void setModify_dt(Date modify_dt) {
    this.modify_dt = modify_dt;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}
    @Override
public String toString() {
    return "BundlePack{" +
            "id=" + id +
            ", promotion_kit='" + promotion_kit + '\'' +
            ", stock_code='" + stock_code + '\'' +
            ", issue_ref='" + issue_ref + '\'' +
            ", quantity=" + quantity +
            ", sys_mod=" + sys_mod +
            ", modify_dt=" + modify_dt +
            ", status='" + status + '\'' +
            '}';
}
}

BundlePackRepo

public class BundlePackRepo {

private String count;

private bundlepacks[] bundlepacks;

public String getCount() {
    return count;
}

public void setCount(String count) {
    this.count = count;
}

public bundlepacks[] getBundlePacks() {
    return bundlepacks;
}

public void setBundlePacks(bundlepacks[] bundlePacks) {
    this.bundlepacks = bundlePacks;
}

public static class bundlepacks {

    @SerializedName("promotionkit")
    private String promotion_kit;
    @SerializedName("stock_code")
    private String stock_code;
    @SerializedName("iss_ref")
    private String issue_ref;
    @SerializedName("qty")
    private int quantity;
    @SerializedName("sys_mod")
    private String sys_mod;
    @SerializedName("modify_dt")
    private String modify_dt;
    @SerializedName("status")
    private String status;
    @SerializedName("id")
    private String id;
    public String getPromotion_kit() {
        return promotion_kit;
    }

    public void setPromotion_kit(String promotion_kit) {
        this.promotion_kit = promotion_kit;
    }

    public String getStock_code() {
        return stock_code;
    }

    public void setStock_code(String stock_code) {
        this.stock_code = stock_code;
    }

    public String getIssue_ref() {
        return issue_ref;
    }

    public void setIssue_ref(String issue_ref) {
        this.issue_ref = issue_ref;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getSys_mod() {
        return sys_mod;
    }

    public void setSys_mod(String sys_mod) {
        this.sys_mod = sys_mod;
    }

    public String getModify_dt() {
        return modify_dt;
    }

    public void setModify_dt(String modify_dt) {
        this.modify_dt = modify_dt;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
}

您正在调用 toString() 将每个 bundlepack 实例转换为字符串以供输出。因此,您需要将 toString 覆盖为 bundlepack class。它需要以您需要的形式格式化实例。

由于您目前没有覆盖该方法,您的代码目前正在调用 Object.toString() ...它只输出对象的 class 名称和哈希码。

您的 IDE 可能有一个助手根据 class.

的字段为 class 生成 toString