Java 编写arrayList<;模型类>;使用BufferedWriter将其转换为txt文件

Java 编写arrayList<;模型类>;使用BufferedWriter将其转换为txt文件,java,android,android-studio,arraylist,bufferedwriter,Java,Android,Android Studio,Arraylist,Bufferedwriter,我正在尝试从arrayBundlePack获取数据从enqueue调用获取数据后,我正在尝试使用BufferedWriter将arrayBundlePack中保存的数据写入txt文件,但它无法获得我想要的输出 我不确定这是否是输出arrayList模型类的方法 这是密码 private ArrayList<BundlePack> arrayBundlePack; Call<BundlePackRepo> call = .............. //

我正在尝试从arrayBundlePack获取数据从enqueue调用获取数据后,我正在尝试使用BufferedWriter将arrayBundlePack中保存的数据写入txt文件,但它无法获得我想要的输出

我不确定这是否是输出arrayList模型类的方法

这是密码

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();
                                }
                            }
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
类。它需要按照您的目的所需的格式格式化实例

由于您当前尚未重写该方法,因此您的代码当前正在调用
Object.toString()
。。。它只输出对象的类名和哈希代码


您的IDE可能有一个助手,可以根据类的字段为类生成
toString

Hi,我尝试在bundlepack上重写toString(),但它只输出一行而不是所有数据。我不知道为什么,因为数据应该超过1000+,所以你的重写方法做了什么?我会很快把它放到BundlePack中,这样你就可以看到它没有
toString()
方法。@override public String toString(){return“BundlePack{+“id=“+id+”,promotion\u kit=”+促销工具+'\'''+'、股票代码=''+股票代码+'\'''+'、发行参考='+发行参考+'\'''+'、数量='+数量+“、系统模块=+系统模块+”、修改模块=“+修改模块+”、状态='+状态+'\''+'}}}我覆盖到字符串()在BundlePack上,alreadyDont重写toString()。只需使用不同的函数名。
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;
    }
}
}