写和读多个对象文件多个、对象、文件

2023-09-06 06:19:32 作者:放不了你丶梦

我desigining handrwriting aplication为Android。

我想将信息写入(LOGINFO类)日志文件,每次用户preSS回车键。

之后,我想读的信息。

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

 公共类LOGINFO实现Serializable {

私有静态最后长的serialVersionUID = -5777674941129067422L;

公共静态列表<点[] []>笔触;
公共静态列表< byte []的> codeS;

显示//只写和读的方法

私人无效的writeObject(ObjectOutputStream的流)抛出IOException异常{
    stream.defaultWriteObject();
    stream.writeInt(strokes.size());
    点[] [] pointsArray = NULL;
    的for(int i = 0; I< strokes.size();我++)
    {
        pointsArray =((点[] [])strokes.get(ⅰ));
        stream.writeInt(pointsArray.length);
        对于(INT J = 0; J< pointsArray.length; J ++)
        {
            stream.writeInt(pointsArray [J] .length);
            对于(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大小= codes.size();
    stream.writeInt(大小);
    的for(int i = 0; I<大小;我++)
    {
        stream.write(codes.get(一));
    }
}
 

下面是只读方式:

 私人无效的readObject(java.io.ObjectInputStream中流)
    {
        stream.defaultReadObject();
        INT strokesSize = stream.readInt();
        的for(int i = 0; I< strokesSize;我++)
        {
            INT arrayXSize = stream.readInt();
            点[] []点=新点[arrayXSize] [];
            对于(INT J = 0; J< arrayXSize; J ++)
            {
                INT arrayYSize = stream.readInt();
                点[J] =新点[arrayYSize]
                对于(INT K = 0; K< arrayYSize; k ++)
                    点[J] [K] =新的点(stream.readInt(),stream.readInt());
            }
            strokes.add(分);
        }

        INT codesSize = stream.readInt();
        的for(int i = 0; I< codesSize;我++)
        {
            byte []的缓冲区=新的字节[3];
            stream.read(缓冲液,0,3);
            codes.add(缓冲区);
        }
    }
 
Word文档快速选中多个对象 形状 的方法

它工作得很好,当我只保存一个对象文件。当我试图挽救更多的,阅读是不工作(这将引发StreamCorruptedException)。阅读来自while循环,只有一个对象。任何想法?

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

  //写入文件
logInfo.writeLog();

//从文件中读取
ArrayList的< LOGINFO> logInfoArrayList = logInfo.readLog();
 

定义为:

 公共无效WRITELOG()
{
    档案文件=新的文件(Environment.getExternalStorageDirectory()getAbsolutePath(),data.log。);
    FileOutputStream中FOS;
    尝试 {
        FOS =新的FileOutputStream(文件,真正的);
        // fos基因= openFileOutput(Environment.getExternalStorageDirectory()getAbsolutePath()+/data.log,Context.MODE_APPEND。);
        ObjectOutputStream的OS =新的ObjectOutputStream(FOS);
        os.writeObject(本);
        os.close();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
}

公众的ArrayList< LOGINFO> readLog()
{
    ArrayList的< LOGINFO> logInfoArray =新的ArrayList< LOGINFO>();

    尝试{
        档案文件=新的文件(Environment.getExternalStorageDirectory()getAbsolutePath(),data.log。);
        的FileInputStream FIS =新的FileInputStream(文件);
        ObjectInputStream的读者=新的ObjectInputStream(FIS);

        LOGINFO tempLogInfo =新LOGINFO();
        而((tempLogInfo =(LOGINFO)reader.readObject())!= NULL)
            logInfoArray.add(tempLogInfo);
        reader.close();
    }赶上(例外五){
     // TODO自动生成的catch块
     e.printStackTrace();
    }

    返回logInfoArray;
}
 

请求的更新:

  //我们使用这个类来不写在已经存在的文件的头
类MyObjectOutputStream扩展的ObjectOutputStream {

    公共MyObjectOutputStream(OutputStream的OS)抛出IOException异常{
        超(OS);
      }

    @覆盖
    保护无效writeStreamHeader(){}
}
 

解决方案

您无法追加到与创建的现有文件中的的ObjectOutputStream ,至少不是没有努力。还有一招地方有关扩展的ObjectOutputStream 并重写 writeStreamHeader()的方法,以免写入流头的第二次,但我并不赞成。你真的应该重写整个文件,也许作为一个列表。

您并不需要所有这code。只要笔画 codeS 非静态和非瞬态和摆脱的readObject()的writeObject()方法完全。

I am desigining handrwriting aplication for android.

I would like to write information (LogInfo class) to log file, every time user press Enter button.

After, I would like to read the information.

This is part of my class with custom write method in LogInfo class:

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 shown

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

Here is read method:

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

It works well when I save only one object in file. When I try to save more, reading is not working (It throws StreamCorruptedException). It read only one object from while loop. Any ideas?

In main class, I just use two simple methods:

// WRITE TO FILE
logInfo.writeLog();

// READ FROM FILE
ArrayList<LogInfo> logInfoArrayList = logInfo.readLog();

defined as:

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

requested UPDATE:

//We use this class to not write a header in a file that already exist
class MyObjectOutputStream extends ObjectOutputStream {

    public MyObjectOutputStream(OutputStream os) throws IOException {
        super(os);
      }

    @Override
    protected void writeStreamHeader() {}
}

解决方案

You can't append to an existing file created with an ObjectOutputStream, at least not without effort. There is a trick somewhere about extending ObjectOutputStream and overriding the writeStreamHeader() method so as not to write the stream header the second time, but I'm not in favour of it. You should really rewrite the whole file, maybe as a List.

You don't need all this code. Just make strokes and codes non-static and non-transient, and get rid of the readObject() and writeObject() methods altogether.