我是通过序列化,把对象写入文件。现在发现一个问题。就是每次开始写的时候,单独写的时候,都会给文件的文件头写入一个标识的4个字节。ac ed 00 05。
然后再把其反序列化的时候,如果读第一个对象的时候,没什么问题,但是读后面几个会因为那个文件头报错。具体代码如下。
有什么办法使得从第二次写的时候,不加入ac ed 00 05,或者说读的时候直接跳过么?
class mainapp。用于做主程序的
public class mainapp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "F:\\java\\myjava\\filetest\\obj.txt";
E1 a = new E1("Ross","china"); //第一次写入ac ed 00 05 + 对象
a.write(path);
E1 b = new E1 ("Peobe","jap"); //第二次写入ac ed 00 05 + 对象
b.write(path);
E1 c = new E1("Joey","french"); //第三次写入ac ed 00 05 + 对象
c.write(path);
E1.read(path);
}}
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class E1 {
person record; public E1(String name,String address){
record = new person(name,address);
}
/*写对象*/
public void write(String path){
ObjectOutputStream objout = null;
FileOutputStream outfile;
try {
outfile = new FileOutputStream(path,true);
objout = new ObjectOutputStream(
new BufferedOutputStream(
outfile));
objout.writeObject(record);
System.out.printf("the obj %1$s has been writen \n",record.toString());
objout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* 读对象*/
static public void read(String path){
ObjectInputStream objin = null;
person readone = null;
int count = 0;
byte[] temp = new byte[4];
try {
FileInputStream infile = new FileInputStream(path);
objin = new ObjectInputStream(infile);
while(true){
readone = (person) objin.readObject();
count++;
System.out.println(readone);
}
}catch (EOFException e){
System.out.printf("%1$d has been read", count);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch blockW
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}person类import java.io.Serializable;public class person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String name;
public String address;
public person(String name,String address){
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString(){
String output = String.format("\"the name is %1$s,the address is %2$s\"",
name,
address);
return output;
}
}
然后再把其反序列化的时候,如果读第一个对象的时候,没什么问题,但是读后面几个会因为那个文件头报错。具体代码如下。
有什么办法使得从第二次写的时候,不加入ac ed 00 05,或者说读的时候直接跳过么?
class mainapp。用于做主程序的
public class mainapp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "F:\\java\\myjava\\filetest\\obj.txt";
E1 a = new E1("Ross","china"); //第一次写入ac ed 00 05 + 对象
a.write(path);
E1 b = new E1 ("Peobe","jap"); //第二次写入ac ed 00 05 + 对象
b.write(path);
E1 c = new E1("Joey","french"); //第三次写入ac ed 00 05 + 对象
c.write(path);
E1.read(path);
}}
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class E1 {
person record; public E1(String name,String address){
record = new person(name,address);
}
/*写对象*/
public void write(String path){
ObjectOutputStream objout = null;
FileOutputStream outfile;
try {
outfile = new FileOutputStream(path,true);
objout = new ObjectOutputStream(
new BufferedOutputStream(
outfile));
objout.writeObject(record);
System.out.printf("the obj %1$s has been writen \n",record.toString());
objout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* 读对象*/
static public void read(String path){
ObjectInputStream objin = null;
person readone = null;
int count = 0;
byte[] temp = new byte[4];
try {
FileInputStream infile = new FileInputStream(path);
objin = new ObjectInputStream(infile);
while(true){
readone = (person) objin.readObject();
count++;
System.out.println(readone);
}
}catch (EOFException e){
System.out.printf("%1$d has been read", count);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch blockW
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}person类import java.io.Serializable;public class person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String name;
public String address;
public person(String name,String address){
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString(){
String output = String.format("\"the name is %1$s,the address is %2$s\"",
name,
address);
return output;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货