使用BufferedWriter将arrayList写入txt文件中。

huangapple go评论67阅读模式
英文:

Write arrayList< model class> into txt file using BufferedWriter

问题

以下是您提供的代码的翻译部分:

// 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();
}

@Entity
public class BundlePack {
    // ... (省略其他属性和方法)
    
    @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 + '\'' +
                '}';
    }
}

public class BundlePackRepo {
    // ... (省略其他属性和方法)
    
    public static class bundlepacks {
        // ... (省略其他属性和方法)
    }
}

请注意,由于您要求只返回翻译的部分,我已经省略了注释和额外的解释性内容。如果您需要进一步的帮助或解释,请随时提问。

英文:

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&lt;BundlePack&gt; arrayBundlePack;

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

                            BundlePack bundlePack;
                            for (int a = 0; a &lt; 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 = &quot;bundlepack.txt&quot;;
                                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 = &quot;promotion_kit&quot;)
private String promotion_kit;

@ColumnInfo(name = &quot;stock_code&quot;)
private String stock_code;

@ColumnInfo(name = &quot;issue_ref&quot;)
private String issue_ref;

@ColumnInfo(name = &quot;quantity&quot;)
private Integer quantity;

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

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

@ColumnInfo(name = &quot;status&quot;)
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 &quot;BundlePack{&quot; +
            &quot;id=&quot; + id +
            &quot;, promotion_kit=&#39;&quot; + promotion_kit + &#39;\&#39;&#39; +
            &quot;, stock_code=&#39;&quot; + stock_code + &#39;\&#39;&#39; +
            &quot;, issue_ref=&#39;&quot; + issue_ref + &#39;\&#39;&#39; +
            &quot;, quantity=&quot; + quantity +
            &quot;, sys_mod=&quot; + sys_mod +
            &quot;, modify_dt=&quot; + modify_dt +
            &quot;, status=&#39;&quot; + status + &#39;\&#39;&#39; +
            &#39;}&#39;;
}
}

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(&quot;promotionkit&quot;)
    private String promotion_kit;
    @SerializedName(&quot;stock_code&quot;)
    private String stock_code;
    @SerializedName(&quot;iss_ref&quot;)
    private String issue_ref;
    @SerializedName(&quot;qty&quot;)
    private int quantity;
    @SerializedName(&quot;sys_mod&quot;)
    private String sys_mod;
    @SerializedName(&quot;modify_dt&quot;)
    private String modify_dt;
    @SerializedName(&quot;status&quot;)
    private String status;
    @SerializedName(&quot;id&quot;)
    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;
    }
}
}

答案1

得分: 0

你正在调用toString()方法将每个bundlepack实例转换为字符串以进行输出。因此,你需要为bundlepack类添加一个toString的重写方法。它需要将实例格式化为你需要的形式。

由于你目前还没有重写该方法,你的代码当前正在调用Object.toString()…它只会输出对象的类名和哈希码。

你的IDE可能会有一个辅助工具,根据类的字段生成一个toString方法。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月18日 10:39:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63948581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定