如果ObjectInputStream.objectRead似乎对我不起作用,我不会感到惊讶是因为我错过了一些明显的东西。
下面,您将找到代码和应用程序日志输出。
不会抛出异常。代码执行完毕。
但是,从SDCard检索的文件是空的(由.size()表示为0)虽然当我在SDCard上检查它时它的大小大于0(几KB)并且我能够写入文件(附加模式)没有问题(证明在.writeObject操作文件大小增加之后。
我被困住了。我可能错过了一些明显但我无法找到问题的东西。
我真的很感激这个问题的反馈,建议或解决方案。
谢谢,
Alex Donnini
100315-2上的源代码更新
protected static synchronized void readUserLocationInformation() throws StreamCorruptedException, IOException, ClassNotFoundException
{
ArrayList<userLocationInformation> mUserLocationInformation = new ArrayList<userLocationInformation>();
int mUserLocationInformationSize = mUserLocationInformation.size();
createUserLocationDatabaseOnSdCardStorage();
userLocationInformation userLocationInformationHolder = new userLocationInformation();
userLocationInformationHolder.lat = myAndApsLatlng[0];
userLocationInformationHolder.lon = myAndApsLatlng[1];
System.arraycopy( myAndApsLatlng, 0, userLocationInformationHolder.myAndApsLatlng, 0, myAndApsLatlng.length);
userLocationInformationHolder.estimateTime = System.nanoTime();
mUserLocationInformation.add(userLocationInformationHolder);
mUserLocationInformationSize = mUserLocationInformation.size();
try {
Log.i(TAG, " - readUserLocationInformation - mUserLocationInformationSize - " + mUserLocationInformationSize);
if(mUserLocationInformationSize > 0)
{
for(int f = 0; f < mUserLocationInformationSize; ++f)
{
Log.i(TAG, " - readUserLocationInformation - mUserLocationInformation.get(f).estimateTime - " + mUserLocationInformation.get(f).estimateTime);
Log.i(TAG, " - readUserLocationInformation - mUserLocationInformation.get(f).lat - " + mUserLocationInformation.get(f).lat);
Log.i(TAG, " - readUserLocationInformation - mUserLocationInformation.get(f).lon - " + mUserLocationInformation.get(f).lon);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write object to external storage START $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Object objectToBeStoredHolder;
FileOutputStream objectNameFileOutputStream = null;
OutputStream objectNameBufferedOutputStream = null;
ObjectOutputStream objectNameObjectOutputStream = null;
objectToBeStoredHolder = mUserLocationInformation;
try
{
try
{
Log.i(TAG, " - readUserLocationInformation - object - "+userLocationDatabaseFileNameObject);
Log.i(TAG, " - readUserLocationInformation - setting up objectNameFileOutputStream --- ");
objectNameFileOutputStream = new FileOutputStream(objectStorageLocationDirectory+userLocationDatabaseFileNameObject, true);
} catch (IOException e1)
{
e1.printStackTrace();
}
try
{
Log.i(TAG, " - readUserLocationInformation - object - "+userLocationDatabaseFileNameObject);
Log.i(TAG, " - readUserLocationInformation - setting up objectNameBufferedOutputStream --- ");
objectNameBufferedOutputStream = new BufferedOutputStream(objectNameFileOutputStream);
}
catch (Exception e1)
{
e1.printStackTrace();
}
try
{
Log.i(TAG, " - readUserLocationInformation - object - "+userLocationDatabaseFileNameObject);
Log.i(TAG, " - readUserLocationInformation - setting up objectNameObjectOutputStream --- ");
objectNameObjectOutputStream = new ObjectOutputStream(objectNameBufferedOutputStream);
}
catch (Exception e)
{
Log.i(TAG, " - readUserLocationInformation - exception - object - "+userLocationDatabaseFileNameObject);
e.printStackTrace();
}
try
{
Log.i(TAG, " - readUserLocationInformation - objectNameObjectOutputStream.writeObject - object - "+userLocationDatabaseFileNameObject);
objectNameObjectOutputStream.writeObject(objectToBeStoredHolder);
}
finally
{
Log.i(TAG, " - readUserLocationInformation - finally objectNameObjectOutputStream for object - "+userLocationDatabaseFileNameObject);
objectNameObjectOutputStream.flush();
objectNameObjectOutputStream.close();
}
}
catch (Exception e)
{
Log.i(TAG, " - readUserLocationInformation - object - "+userLocationDatabaseFileNameObject);
Log.i(TAG,
" - readUserLocationInformation - FAILED to write object ObjectToBeStored --- ");
e.printStackTrace();
}
//write object to external storage END $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Log.i(TAG, " - readUserLocationInformation - retrieving user location database stored on SDcard as arraylist --- ");
FileInputStream userLocationObjectFileInputStream = null;
ObjectInputStream userLocationInputStream = null;
Log.i(TAG, " - readUserLocationInformation - setting up FileInputStream --- ");
userLocationObjectFileInputStream = new FileInputStream(objectStorageLocationDirectory+userLocationDatabaseFileNameObject);
Log.i(TAG, " - readUserLocationInformation - setting up ObjectInputStream --- ");
userLocationInputStream = new ObjectInputStream(userLocationObjectFileInputStream);
Log.i(TAG, " - readUserLocationInformation - retrieving user location list stored in ArrayList from SDcard --- ");
mUserLocationInformation = (ArrayList) userLocationInputStream.readObject();
mUserLocationInformationSize = mUserLocationInformation.size();
Log.i(TAG, "readUserLocationInformation - mUserLocationInformationSize - "+mUserLocationInformationSize);
if(mUserLocationInformationSize > 0)
{
for(int f = 0; f < mUserLocationInformationSize; ++f)
{
Log.i(TAG, " - readUserLocationInformation - mUserLocationInformation.get(f).estimateTime - " + mUserLocationInformation.get(f).estimateTime);
Log.i(TAG, " - readUserLocationInformation - mUserLocationInformation.get(f).lat - " + mUserLocationInformation.get(f).lat);
Log.i(TAG, " - readUserLocationInformation - mUserLocationInformation.get(f).lon - " + mUserLocationInformation.get(f).lon);
}
}
}
应用程序日志 - 更新于100315-2
10-05 18:11:51.548: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - mUserLocationInformationSize - 1
10-05 18:11:51.548: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - mUserLocationInformation.get(f).estimateTime - 6768145557155
10-05 18:11:51.548: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - mUserLocationInformation.get(f).lat - 42.35552279549794
10-05 18:11:51.548: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - mUserLocationInformation.get(f).lon - -71.17862967362912
10-05 18:11:51.548: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - object - userLocationDatabaseFileNameObject
10-05 18:11:51.548: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - setting up objectNameFileOutputStream ---
10-05 18:11:51.549: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - object - userLocationDatabaseFileNameObject
10-05 18:11:51.549: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - setting up objectNameBufferedOutputStream ---
10-05 18:11:51.549: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - object - userLocationDatabaseFileNameObject
10-05 18:11:51.549: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - setting up objectNameObjectOutputStream ---
10-05 18:11:51.549: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - objectNameObjectOutputStream.writeObject - object - userLocationDatabaseFileNameObject
10-05 18:11:51.551: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - finally objectNameObjectOutputStream for object - userLocationDatabaseFileNameObject
10-05 18:11:51.552: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - retrieving user location database stored on SDcard as arraylist ---
10-05 18:11:51.552: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - setting up FileInputStream ---
10-05 18:11:51.552: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - setting up ObjectInputStream ---
10-05 18:11:51.552: I/ContinuousLocationTrackingService(10206): - readUserLocationInformation - retrieving user location list stored in ArrayList from SDcard ---
10-05 18:11:51.553: I/ContinuousLocationTrackingService(10206): readUserLocationInformation - mUserLocationInformationSize - 0
createUserLocationDatabaseOnSdCardStorage()的源代码
protected static void createUserLocationDatabaseOnSdCardStorage()
{
boolean userLocationdatabaseCreateActionResult = false;
if(!userLocationDatabaseObject.exists())
{
try {
userLocationdatabaseCreateActionResult = userLocationDatabaseObject.createNewFile();
} catch (IOException e) {
Log.i(TAG, "createUserLocationDatabaseOnSdCardStorage - FAILED to create userLocationDatabaseObject --- ");
e.printStackTrace();
}
if (userLocationdatabaseCreateActionResult) {
Log.i(TAG, "createUserLocationDatabaseOnSdCardStorage - userLocationDatabaseObject created successfully --- ");
} else {
Log.i(TAG, "createUserLocationDatabaseOnSdCardStorage - userLocationDatabaseObject not created --- ");
}
}
else
{
Log.i(TAG, "createUserLocationDatabaseOnSdCardStorage - userLocationDatabaseObject already exists --- ");
}
}
userLocationInformation CLASS的定义
public static class userLocationInformation implements Serializable, Comparator<userLocationInformation>
{
public long estimateTime = 0l;
public double lat;
public double lon;
public double[] myAndApsLatlng = new double[10];
public double alternateLatitude = 0.0;
public double alternateLongitude = 0.0;
public double locationAltitude = 0.0;
public long locationTime = 0;
public float locationSpeed = 0.0f;
public float locationAccuracy = 0.0f;
public float locationBearing = 0.0f;
@Override
public int compare(userLocationInformation one, userLocationInformation another) {
long estimateTimeOne = one.estimateTime;
long estimateTimeAnother = another.estimateTime;
return (one.estimateTime < another.estimateTime ? -1 :
(estimateTimeOne == estimateTimeAnother ? 0 : 1));
}
}
答案 0 :(得分:0)
在writeObject()
电话之后
什么 writeObject()
来电?
您发布的代码中没有writeObject()
来电。
如果这是所有相关代码,那么您所做的就是创建一个空文件。你不能理性地期望从中反序列化对象。
你需要摆脱仅仅调用createNewFile()
的逻辑,并替换以下逻辑:
new FileOutputStream()
new ObjectOutputStream()
writeObject()
ObjectOutputStream
。请参阅单个try / catch查看代码看起来有多好?