简体   繁体   中英

Write arrayList< model class> into txt file using BufferedWriter

I'm trying to get the data from the arrayBundlePack after getting data from enqueue call, I'm trying to use BufferedWriter to the saved data in the arrayBundlePack & write in the txt file, but it just won't get the output I wanted.

I'm not sure if that's the way to output an arrayList model class.

Here's the code

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;
    }
}
}

You are calling toString() to convert each bundlepack instance to a string for output. So you need to an override for toString to the bundlepack class. It needs to format the instance in the form that you need for your purposes.

Since you currently haven't overridden that method, your code is currently calling Object.toString() ... which only outputs the object's class name and hashcode.

Your IDE may well have a helper to generate a toString for a class, based on the fields of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM