我写了一小段代码来搜索/替换包含序列化数据的文件中的旧路径/新路径。我在加载文件之前对其进行了转换,这样我可以将序列化的类移动到新路径,而无需将此类的副本保留在旧路径中。这就是你使用它的方式:
File baseDirectory = applicationContext.getFilesDir();
File file = new File( baseDirectory, "settings.data" );
if (file.exists()) {
//We have to convert it to newsettings.Data
byte[] convertedBytes = common.utils.SerializeTools.changePathInSerializedFile(file, "old.path.data", "new.path.data");
//Write converted file
File newFile = new File( baseDirectory, "newsettings.data" );
FileOutputStream fos = new FileOutputStream(newFile);
fos.write(convertedBytes);
fos.close();
//Remove old file
file.delete();
}
这是 SerializeTools.java 的代码。我在这篇很棒的博客文章http://www.javaworld.com/community/node/2915中学习了 java 序列化格式。
package common.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class SerializeTools {
static public byte[] changePathInSerializedFile(File f, String fromPath, String toPath) throws IOException {
byte[] buffer = new byte[(int)f.length()];
FileInputStream in = new FileInputStream(f);
in.read(buffer);
in.close();
return SerializeTools.changePathInSerializedData(buffer,fromPath,toPath);
}
static public byte[] changePathInSerializedData(byte[] buffer, String fromPath, String toPath) throws IOException {
byte[] search = fromPath.getBytes("UTF-8");
byte[] replace = toPath.getBytes("UTF-8");
ByteArrayOutputStream f = new ByteArrayOutputStream();
for (int i=0;i<buffer.length;i++) {
//Search 2 bytes ahead to let us modify the 2 bytes length of the class name (see Serialize format http://www.javaworld.com/community/node/2915 )
boolean found=false;
int searchMaxIndex=i+search.length+2;
if (searchMaxIndex<=buffer.length) {
found=true;
for (int j=i+2;j<searchMaxIndex;j++) {
if (search[j-i-2]!=buffer[j]) {
found=false;
break;
}
}
}
if (found) {
int high=((int)(buffer[i])&0xff);
int low=((int)(buffer[i+1])&0xff);
int classNameLength=(high<<8)+low;
classNameLength+=replace.length-search.length;
//Write new length
f.write((classNameLength>>8)&0xff);
f.write((classNameLength)&0xff);
//Write replacement path
f.write(replace);
i=searchMaxIndex-1;
} else {
f.write(buffer[i]);
}
}
f.flush();
f.close();
return f.toByteArray();
}
}