不管你用的是ObjectOutputStream还是FileOutputStream都是OutputStream只是实现不同而已....建议再回炉看一下IO这节吧.你说的场景当然可以,把对象序列化后其实就是一堆字节码了,那么你的问题就是如何把一堆字节码写入txt文本文件了.public static byte[] objectToByte(Object obj) throws IOException { ByteArrayOutputStream buff = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(buff); out.writeObject(obj); try { return buff.toByteArray(); } finally { out.close(); } }public static Object byteToObject(byte[] b) throws IOException, ClassNotFoundException { ByteArrayInputStream buff = new ByteArrayInputStream(b); ObjectInputStream in = new ObjectInputStream(buff); Object obj = in.readObject(); try { return obj; } finally { in.close(); } }我提供一个这样的方法,对象经过这个方法就得到了你要的对象序列化后的字节数组了.然就是就是把字节码转换成字符串,以方便写入文本文件. 这里我提供一个工具类,这是我自己写用以平时项目中使用的一个将字节码可视化的. public class PrivacyUtil { /** * hex table */ private static char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * do not allow instantiation. */ private PrivacyUtil() { } /** * bytes to string. * @param b src bytes。 * @return hex string。 */ public static String toHexString(byte[] b) { StringBuilder sb = new StringBuilder(b.length * 2); for (int i = 0; i < b.length; i++) { sb.append(hexChars[(b[i] & 0xf0) >>> 4]); sb.append(hexChars[b[i] & 0x0f]); } return sb.toString(); } /** * hex string to bytes * @param hexString hex string. * @return src bytes. */ public static byte[] toOriginalByte(String hexString) { if (hexString == null || hexString.isEmpty()) { return null; } byte h;//hight index byte l;//low index byte[] buff = new byte[hexString.length() / 2]; int buffIndex = 0; byte nowByte = 0; for (int count = 0; count < hexString.length(); count += 2) { h = (byte) (((byte) checkCharIndex(hexString.charAt(count))) << 4); l = (byte) checkCharIndex(hexString.charAt(count + 1)); nowByte = (byte) (nowByte | h); nowByte = (byte) (nowByte | l); buff[buffIndex++] = nowByte; nowByte = 0; } return buff; } /** * find hex table index. * return 0 if not found. * @param c need find char. * @return less than 0 means not found. */ private static int checkCharIndex(char c) { int index = -1; for (int count = 0; count < hexChars.length; count++) { if (c == hexChars[count]) { index = count; break; } } return index; } }调用这个工具的 PrivacyUtil.toHexString方法你就得到了一个字符串.把这个字符串写方便的写入文本文件进行持久了. 需要反序列化时只需要 调用toOriginalByte方法得到对应的字节码再调用上面的"byteToObject"方法得到对象.
我觉得这是一个类似协议的问题,如下一个User类: public class User{ String name = 'a'; int age = 20; }使用ObjectOutputStream存入文件,数据格式可能是 {class:'User',pro:[{name:'name',value:'a',type:'String'},{name:'age',value:'20',type:'Int'}]} 然后使用ObjectOutputStream读取的时候按一定的协议再把这些数据还原(可能会用到反射) 也就是说ObjectOutputStream其内部可能封装了对象数据的生成和解析协议。。 以上全是我猜的呵呵
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buff);
out.writeObject(obj);
try {
return buff.toByteArray();
} finally {
out.close();
}
}public static Object byteToObject(byte[] b)
throws IOException, ClassNotFoundException {
ByteArrayInputStream buff = new ByteArrayInputStream(b);
ObjectInputStream in = new ObjectInputStream(buff);
Object obj = in.readObject();
try {
return obj;
} finally {
in.close();
}
}我提供一个这样的方法,对象经过这个方法就得到了你要的对象序列化后的字节数组了.然就是就是把字节码转换成字符串,以方便写入文本文件.
这里我提供一个工具类,这是我自己写用以平时项目中使用的一个将字节码可视化的.
public class PrivacyUtil { /**
* hex table
*/
private static char[] hexChars = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
}; /**
* do not allow instantiation.
*/
private PrivacyUtil() {
} /**
* bytes to string.
* @param b src bytes。
* @return hex string。
*/
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(hexChars[(b[i] & 0xf0) >>> 4]);
sb.append(hexChars[b[i] & 0x0f]);
}
return sb.toString();
} /**
* hex string to bytes
* @param hexString hex string.
* @return src bytes.
*/
public static byte[] toOriginalByte(String hexString) {
if (hexString == null || hexString.isEmpty()) {
return null;
}
byte h;//hight index
byte l;//low index
byte[] buff = new byte[hexString.length() / 2];
int buffIndex = 0;
byte nowByte = 0;
for (int count = 0; count < hexString.length(); count += 2) {
h = (byte) (((byte) checkCharIndex(hexString.charAt(count))) << 4);
l = (byte) checkCharIndex(hexString.charAt(count + 1));
nowByte = (byte) (nowByte | h);
nowByte = (byte) (nowByte | l);
buff[buffIndex++] = nowByte;
nowByte = 0;
}
return buff;
} /**
* find hex table index.
* return 0 if not found.
* @param c need find char.
* @return less than 0 means not found.
*/
private static int checkCharIndex(char c) {
int index = -1;
for (int count = 0; count < hexChars.length; count++) {
if (c == hexChars[count]) {
index = count;
break;
}
} return index;
}
}调用这个工具的 PrivacyUtil.toHexString方法你就得到了一个字符串.把这个字符串写方便的写入文本文件进行持久了.
需要反序列化时只需要 调用toOriginalByte方法得到对应的字节码再调用上面的"byteToObject"方法得到对象.
public class User{
String name = 'a';
int age = 20;
}使用ObjectOutputStream存入文件,数据格式可能是
{class:'User',pro:[{name:'name',value:'a',type:'String'},{name:'age',value:'20',type:'Int'}]}
然后使用ObjectOutputStream读取的时候按一定的协议再把这些数据还原(可能会用到反射)
也就是说ObjectOutputStream其内部可能封装了对象数据的生成和解析协议。。
以上全是我猜的呵呵
还可以采用将对象进行json序列化为字符串,然后与一一追加到txt文件里面,每个字符串用一个特殊的分隔符或者空行都行,读取的时候按照相应的规则读取并采用JSON进行反序列化