无法使用 ByteAarrayOutputStream 保存和检索

问题描述 投票:0回答:1

我正在制作这个 J2ME 应用程序,但当我尝试保存时遇到一些问题,我认为它保存正确,但我不确定......但是当我检索它时,它给出了 null

这就是我存储它们的方式

        PAR par = new PAR(oldMonPay, newMonPay, oldInterest);
        par.setOldMPay(oldMonPay);
        par.setNewMPay(newMonPay);
        par.setOldInt(oldInterest);

这就是我保存和检索的方式

public static byte[] parseObjPAR(PAR p) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out;

    try {
        out = new DataOutputStream(baos);
        out.writeUTF(p.getNewMPay());
        out.writeUTF(p.getOldInt());
        out.writeUTF(p.getOldMPay());



    } catch (IOException e) {
    }
    return baos.toByteArray();
}

public static PAR parseByteArrPAR(byte[] b) {
    PAR p = null;
    ByteArrayInputStream bais;
    DataInputStream in;

    if (b != null) {
        try {
            bais = new ByteArrayInputStream(b);
            in = new DataInputStream(bais);
            p = new PAR(
                    in.readUTF(),
                    in.readUTF(),
                    in.readUTF());    
        } catch (IOException e) {
        }
    }
    return p;
}

这就是我显示检索到的信息的方式,还有另一个问题,这个东西没有显示所有数据,而是只显示 3 条记录。我认为前3个。

 public void populatePAResult(PAR[] p) {
    try {
        for (int i = 0; i < p.length; i++) {
            String oldMP = p[i].getOldMPay();
            String newMP = p[i].getNewMPay();
            String oldI = p[i].getOldInt();



            result1.append("Day : " + oldMP, null);
            result1.append("Time : " + oldI, null);
            result1.append("Technology : " + newMP, null);

        }
    } catch (Exception e) {
    }
}
java java-me rms
1个回答
0
投票

parseObjPAR
方法中写入数据的顺序是:

out.writeUTF(p.getNewMPay());
out.writeUTF(p.getOldInt());
out.writeUTF(p.getOldMPay());

然而,当您读回它并传递构造函数期望的顺序时,是不同的:

PAR par = new PAR(oldMonPay, newMonPay, oldInterest);

所以即使不是

null
,加载的数据也是无效的。

© www.soinside.com 2019 - 2024. All rights reserved.