我想写然后从文件中读取一个对象。下面我附上代码示例向您展示我的想法(这只是示例,我的对象更复杂,但问题是一样的)。
我的问题是:
如果我不将 TestObject 字段设为静态,则 testObject.points 在 readObject 方法开始时变为 null。我不明白为什么。谁能给我很好的解释?
我想写一个对象,然后再写 2 个对象,然后将它们全部读取(多个对象写入/读取 - 我想为 Android 创建日志文件)。
你可以帮帮我吗?
// ... 进口
public class TestObject implements Serializable{
transient public ArrayList<Point[]> points;
public TestObject()
{
points = new ArrayList<Point[]>();
Point[] p1 = new Point[1];
p1[0] = new Point(1,1);
Point[] p2 = new Point[2];
p2[0] = new Point(2,2);
p2[1] = new Point(2,2);
points.add(p1);
points.add(p2);
}
private void writeObject(ObjectOutputStream stream) throws IOException
{
stream.defaultWriteObject();
stream.writeInt(points.size());
Point[] pointsArray = null;
for (int i = 0; i < points.size(); i++)
{
pointsArray = ((Point[])points.get(i));
stream.writeInt(pointsArray.length);
for (int j = 0; j < pointsArray.length; j++)
{
stream.writeInt(pointsArray[j].x);
stream.writeInt(pointsArray[j].y);
}
}
}
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
points = new ArrayList<Point[]>();
int pointsSize = stream.readInt();
for (int i = 0; i < pointsSize; i++)
{
int arraySize = stream.readInt();
Point[] pointsArray = new Point[arraySize];
for (int j = 0; j < arraySize; j++)
{
pointsArray[j] = new Point(stream.readInt(), stream.readInt());
}
points.add(pointsArray);
}
}
public void writeLog()
{
File file = new File ("C:\\!\\", "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 TestObject readLog()
{
TestObject testObject = new TestObject();
testObject.points = new ArrayList<Point[]>();
try{
File file = new File ("C:\\!\\", "data.log");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream reader = new ObjectInputStream(fis);
testObject = (TestObject) reader.readObject();
reader.close();
} catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return testObject;
}
}
还有我的主课
// ... 进口
public class Main {
public static void main(String[] args) throws IOException {
TestObject testObject = new TestObject();
testObject.writeLog();
testObject.readLog();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
同样,这只是一个例子。真正的应用程序是为Android。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import android.graphics.Point;
import android.os.Environment;
public class LogInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 2281758309050283667L;
transient public ArrayList<Point[][]> strokes;
transient public LinkedList<byte[]> codes;
public LogInfo()
{
strokes = new ArrayList<Point[][]>();
codes = new LinkedList<byte[]>();
}
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));
}
}
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException
{
strokes = new ArrayList<Point[][]>();
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();
codes = new LinkedList<byte[]>();
for (int i = 0; i < codesSize; i++)
{
byte[] buffer = new byte[3];
stream.read(buffer, 0, 3);
codes.add(buffer);
}
}
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 LogInfo readLog()
{
try{
File file = new File (Environment.getExternalStorageDirectory().getAbsolutePath(), "data.log");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream reader = new ObjectInputStream(fis);
reader.readObject();
reader.close();
} catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return this;
}
}
我从不同的类调用 writeLog 和 readLog,其中我有一个对象实例。
// WRITE TO FILE
logInfo.writeLog();
// CLEAR LOG VARIABLE (NOT FILE)
delAllLogInfo();
// READ FROM FILE
LogInfo newLogInfo = logInfo.readLog();