数据存取,没想象中的那么复杂,java中已经有很好的方法。转自《java游戏高级编程》 清华大学出版社
package tools;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
*
* @author ttwings
* @version 0.1
* @since 2015-10-19 上午9:04:50
*
*/
public class SerializableLib {
public static byte[] compress(Serializable serializable) throws IOException{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
save(serializable ,byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
/**
* 保存数据
* @param serializable
* @param outputStream
* @throws IOException
*/
public static void save(Serializable serializable,OutputStream outputStream) throws IOException{
ObjectOutputStream objectOutputStream = null;
try{
objectOutputStream = new ObjectOutputStream(
new GZIPOutputStream(
new BufferedOutputStream(outputStream)));
}
finally{
if(objectOutputStream != null){
objectOutputStream.close();
}
else{
outputStream.close();
}
}
}
/**
* 读取数据
* @param inputStream 输入数据流
* @return
* @throws ClassNotFoundException
* @throws IOException
*/
public static Serializable load(InputStream inputStream) throws ClassNotFoundException,IOException{
ObjectInputStream objectInputStream = null;
try{
objectInputStream
= new ObjectInputStream(
new GZIPInputStream(
new BufferedInputStream(inputStream)));
return (Serializable) objectInputStream.readObject();
}
finally{
if(objectInputStream != null){
objectInputStream.close();
}else{
inputStream.close();
}
}
}
}
ps:做了初步测试,完全可以用。200 M 多的地图数据,直接压缩成 2M 大小的文件。。 = = 。