public class Recorder {
private static Vector<EnemyTank> enemyTanks = null;
public static String recordFilePath = "e:\\mytemp\\recordenemy.dat";
private static int allEnemyNum = 0;
private static ObjectOutputStream oos = null;
public static String recordFile = "e:\\mytemp\\record.txt";
private static BufferedWriter bw = null;
private static ObjectInputStream ois = null;
private static BufferedReader br = null;
//得到击毁敌人坦克成绩数
public static void getEnemyNum(){
try {
br = new BufferedReader(new FileReader(recordFile));
allEnemyNum = Integer.parseInt(br.readLine());
// Recorder.setAllEnemyNum(allEnemyNum);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//得到记录了坐标及方向的敌人坦克
public static Vector<EnemyTank> readEnemy(){
Vector<EnemyTank> enemyTanks = new Vector<>();
try {
FileInputStream fis = new FileInputStream(recordFilePath);
ois = new ObjectInputStream(fis);
while(fis.available() > 0){
// System.out.println((EnemyTank) ois.readObject());
EnemyTank enemyTank = (EnemyTank) ois.readObject();
enemyTanks.add(enemyTank);
System.out.println(enemyTank);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(ois != null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return enemyTanks;
}
//记录击毁敌人坦克成绩数
public static void saveRecord(){
try {
bw = new BufferedWriter(new FileWriter(recordFile));
bw.write(getAllEnemyNum() + "\r\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void addAllEnemyNum(){
allEnemyNum++;
}
public static int getAllEnemyNum() {
return allEnemyNum;
}
public static void setAllEnemyNum(int allEnemyNum) {
Recorder.allEnemyNum = allEnemyNum;
}
//记录关闭时剩余敌人坦克的坐标及方向
public static void saveEnemy() {
try {
oos = new ObjectOutputStream(new FileOutputStream(recordFilePath));
// EnemyTank enemyTank = new EnemyTank(200,500,0);
// oos.writeObject(enemyTank);
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
if(enemyTank.isIslive()){
EnemyTank saveEnemy = new EnemyTank(enemyTank.getX(),enemyTank.getY(),
enemyTank.getDirection());
oos.writeObject(saveEnemy);
System.out.println(saveEnemy);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
Recorder.enemyTanks = enemyTanks;
}
}
以上通过结束时敌人坦克的x,y坐标和方向 new的对线传给dat文件,但是反序列化读取的时候得到的对象x,y,和方向三个int类型的值都变成了0,导致读取的敌人坦克都是出现在原点,是哪里出了问题呀 求帮忙看下