Android Java - 保存对象列表以归档

时间:2016-10-19 18:30:00

标签: java android

我是编程的新手,我试图将对象列表保存到保存文件,但似乎没有任何效果。我没有得到任何错误或其他任何错误,但价值只是赢得了保存(例如:我点击按钮" Money"赚钱,需要保存,但是当我重新启动应用时,&# 34;钱"再次是0)在Java中我的版本正在运行,现在我在android中转换我的代码并且它无法正常工作

我的SaveFile类:

  public void SaveFile(){
        try{
            File SaveFile = new File("SaveFile.sav");
            if(!SaveFile.exists()) {
                SaveFile.createNewFile();
            }
            FileOutputStream saveFileSub = new FileOutputStream(SaveFile);
            ObjectOutputStream save = new ObjectOutputStream(saveFileSub);

            save.writeObject(CarMain.main);
            save.writeObject(Box.boxes);
            save.writeObject(CarFrame.frames);
            save.writeObject(Part.parts);
            save.writeObject(CarsLv1.cars);
            save.writeObject(CarsLv2.cars);
            save.writeObject(CarsLv3.cars);
            save.writeObject(CarsLv4.cars);
            save.writeObject(CarsLv5.cars);
            save.writeObject(CarsLv6.cars);
            save.writeObject(CarsLv7.cars);
            save.writeObject(CarsLv8.cars);
            save.writeObject(CarsLv9.cars);
            save.writeObject(CarsLv10.cars);
            save.writeObject(Statistic.statistic);

            save.close();
        }
        catch(Exception exc){
            exc.printStackTrace();
        }
    }

我的LoadFile类:

  public void LoadFile(){
        try{
            File SaveFile = new File("SaveFile.sav");
            FileInputStream SaveFileSub = new FileInputStream(SaveFile);
            ObjectInputStream load = new ObjectInputStream(SaveFileSub);

            CarMain.main = (Integer[]) load.readObject();
            Box.boxes = (Integer[]) load.readObject();
            CarFrame.frames = (Integer[]) load.readObject();
            Part.parts = (Integer[]) load.readObject();
            CarsLv1.cars = (Integer[]) load.readObject();
            CarsLv2.cars = (Integer[]) load.readObject();
            CarsLv3.cars = (Integer[]) load.readObject();
            CarsLv4.cars = (Integer[]) load.readObject();
            CarsLv5.cars = (Integer[]) load.readObject();
            CarsLv6.cars = (Integer[]) load.readObject();
            CarsLv7.cars = (Integer[]) load.readObject();
            CarsLv8.cars = (Integer[]) load.readObject();
            CarsLv9.cars = (Integer[]) load.readObject();
            CarsLv10.cars = (Integer[]) load.readObject();
            Statistic.statistic = (Integer[]) load.readObject();
        load.close();
    }
    catch(Exception exc){
        exc.printStackTrace();
    }
}

我的游戏循环在" OnCreate" class:

  LoadFile();

GameLoop= new Thread() {
    @Override
    public void run() {
        try {
            int waited = 1;

            while (waited > 0) {
                sleep(100);
                waited += 1;
                SaveFile();
            }
            SaveFile();
        } catch (InterruptedException e) {
            // do nothing
        } finally {
            SaveFile();
        }

    }
};
GameLoop.start();

1 个答案:

答案 0 :(得分:0)

我注意到你正在保存整数数组,如果你没有很多数据,请尝试使用sharedPreference来保存和获取数据

public void saveIntegerArray(Integer[] arr){
            if (mSharedPreferences!=null){
                mSharedPreferences.edit().putString(yourKey,Arrays.toString(arr)).apply();
            }
        }

        public Integer[] getIntegerArray(){
                if (mSharedPreferences!=null){
                    String s = mSharedPreferences.getString(yourKey, null);
                    return getIntegers(s);
                } else return null;
            }
    public static int[] getIntegers(String s) {
        int[] result;
        ArrayList<Integer> helper = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ' ^ i == s.length() - 1) {
                if (i == s.length() - 1) { sb.append(s.charAt(i)); }
                if (sb.toString().length() > 0) {
                    try { helper.add(Integer.parseInt(sb.toString()));
                    } catch(NumberFormatException nfe) {
                            // Ignore non-integers
                    }
                    sb.setLength(0);
                    continue;
                }
            }
            sb.append(s.charAt(i));
        }

        result = new int[helper.size()];
        int i = 0;

        for (Integer n : helper) {
            result[i++] = n;
        }
        return result;
    }