java– 将多个对象写入和读取到文件

java– 将多个对象写入和读取到文件,第1张

概述我正在为android设计一个手写应用程序.每次用户按下回车键时,我都想将信息(类LogInfo)写入日志文件.之后,我想阅读存储的信息.这是我的类的一部分,具有自定义写入方法:publicclassLogInfoimplementsSerializable{privatestaticfinallongserialVersionUID=-5777674

我正在为android设计一个手写应用程序.

每次用户按下回车键时,我都想将信息(类LogInfo)写入日志文件.

之后,我想阅读存储的信息.

这是我的类的一部分,具有自定义写入方法:

public class LogInfo implements Serializable {private static final long serialVersionUID = -5777674941129067422L;public static List<Point[][]> strokes;public static List<byte[]> codes;// Only write and read methods shownprivate voID writeObject(ObjectOutputStream stream) throws IOException{    stream.defaultWriteObject();    stream.writeInt(strokes.size());    Point[][] pointsArray = null;    for (int i = 0; i < strokes.size(); i++)    {        pointsArray = ((Point[][])strokes.get(i));        stream.writeInt(pointsArray.length);        for (int j = 0; j < pointsArray.length; j++)        {            stream.writeInt(pointsArray[j].length);            for (int k = 0; k < pointsArray[j].length; k++)            {                stream.writeInt(pointsArray[j][k].x);                stream.writeInt(pointsArray[j][k].y);                //stream.writeObject(elementData[i]);            }        }    }    int size = codes.size();    stream.writeInt(size);    for (int i = 0; i < size; i++)    {        stream.write(codes.get(i));    }}

这是读取方法:

private voID readobject(java.io.ObjectinputStream stream)    {        stream.defaultReadobject();        int strokesSize = stream.readInt();        for (int i = 0; i < strokesSize; i++)        {            int arrayXSize = stream.readInt();            Point[][] points = new Point[arrayXSize][];            for (int j = 0; j < arrayXSize; j++)            {                int arrayYSize = stream.readInt();                points[j] = new Point[arrayYSize];                for (int k = 0; k < arrayYSize; k++)                    points[j][k] = new Point(stream.readInt(), stream.readInt());            }            strokes.add(points);        }        int codesSize = stream.readInt();        for (int i = 0; i < codesSize; i++)        {            byte[] buffer = new byte[3];            stream.read(buffer, 0, 3);            codes.add(buffer);        }    }

当我在文件中只保存一个对象时,它运行良好.当我尝试保存更多时,读取不起作用(它会抛出StreamCorruptedException).它只读取while循环中的一个对象!

在主类中,我只使用两个简单的方法:

// WRITE TO filelogInfo.writeLog();// READ FROM fileArrayList<LogInfo> logInfoArrayList = logInfo.readLog();

定义为:

public voID writeLog(){    file file = new file (Environment.getExternalStorageDirectory().getabsolutePath(), "data.log");    fileOutputStream fos;    try {        fos = new fileOutputStream(file, true);        //fos = openfileOutput(Environment.getExternalStorageDirectory().getabsolutePath() + "/data.log", Context.MODE_APPEND);        ObjectOutputStream os = new ObjectOutputStream(fos);        os.writeObject(this);        os.close();     } catch (IOException e) {        // Todo auto-generated catch block        e.printstacktrace();    }}public ArrayList<LogInfo> readLog(){    ArrayList<LogInfo> logInfoArray = new ArrayList<LogInfo>();    try{        file file = new file (Environment.getExternalStorageDirectory().getabsolutePath(), "data.log");        fileinputStream fis  = new fileinputStream(file);        ObjectinputStream reader = new ObjectinputStream(fis);          LogInfo tempLogInfo = new LogInfo();        while((tempLogInfo = (LogInfo)reader.readobject()) != null)            logInfoArray.add(tempLogInfo);        reader.close();    } catch (Exception e) {     //Todo auto-generated catch block     e.printstacktrace();    }    return logInfoArray;}

请求更新:

//We use this class to not write a @R_404_5548@ in a file that already existclass MyObjectOutputStream extends ObjectOutputStream {    public MyObjectOutputStream(OutputStream os) throws IOException {        super(os);      }    @OverrIDe    protected voID writeStream@R_404_5548@() {}}

解决方法:

>您无法附加到使用ObjectOutputStream创建的现有文件,至少不会毫不费力.有一个关于扩展ObjectOutputStream和覆盖writeStream@R_404_5548@()方法的技巧,以便不第二次写入流头,但我并不赞成它.你应该真的重写整个文件,也许作为一个列表.
>您不需要所有这些代码.只需使笔画和代码非静态和非瞬态,并完全摆脱readobject()和writeObject()方法.

总结

以上是内存溢出为你收集整理的java – 将多个对象写入和读取到文件全部内容,希望文章能够帮你解决java – 将多个对象写入和读取到文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1106602.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存