2013-05-28 30 views
0

我正在處理一個應用程序,我想在文件中有一個ArrayList。我寫了兩個方法來保存和獲取:序列化控制文件

public static boolean saveMetars(Context context, ArrayList<Metar> metars) { 
      try { 
       FileOutputStream fos = context.openFileOutput("metars_array", Context.MODE_WORLD_READABLE); 
       ObjectOutputStream oos = new ObjectOutputStream(fos); 
       oos.writeObject(metars); 
       oos.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return false; 
      } 

      return true; 
     } 



     public static ArrayList<Metar> getArrayMetars(Context context) { 
      try { 
       Log.d("here", "asas"); 
       FileInputStream fis = context.openFileInput("metars_array"); 
          ObjectInputStream is = new ObjectInputStream(fis); 
       Object readObject = is.readObject(); 
       is.close(); 

       if(readObject != null && readObject instanceof ArrayList) { 
        Log.d("here2", "asas"); 
        return (ArrayList<Metar>) readObject; 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

我的問題是:當用戶安裝該應用程序,並使用它的第一次,我有一個XML文件,我用一個方法(在創建了APPI)讀取並創建METAR對象添加到我的ArrayList ..問題是,我想有一個if語句或類似的東西,控制如果文件

context.openFileOutput(「metars_array」 Context.MODE_WORLD_READABLE) 存在。

我製成

if(getApplicationContext().getDir("metars-array", MODE_WORLD_READABLE).exists() || getBaseContext().getDir("metars-array", MODE_WORLD_READABLE).exists()){ 
     ArrayList<Metar> mets = getArrayMetars(getBaseContext()); 
     isMetarsInDb=true; 
} 
else 
    isMetarsInDb=false; 

該錯誤是,它總是在第一條目是否,當我做

ArrayList的METS = getArrayMetars(getBaseContext());

它返回一個錯誤說

文件 「/data/data/android.altimeter.com/app_metars_array」(文件未找到 )

我想這樣做,因爲如果文件不存在(用戶第一次使用該應用程序,他從未序列化ArrayList),我必須使用從XML讀取的方法,然後序列化爲metars_array文件。

回答

0

其解決:

檢查的TimerTask:

t.schedule(new TimerTask() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      try { 
        FileInputStream fis = getApplicationContext().openFileInput("metars_array"); 
        ObjectInputStream is = new ObjectInputStream(fis); 
        Object readObject = is.readObject(); 
        is.close(); 

        if(readObject != null && readObject instanceof ArrayList) { 

         isInDb = true; 
         metars=(ArrayList<Metar>) readObject; 
         isMetToArrayFin=true; 
         t.cancel(); 
        } 

        else{ 
         isInDb = false; 
         xmlToStruct(); 
         saveMetars(getApplicationContext(), metars); 
         t.cancel(); 
        } 
      } 
      catch (Exception e) { 
         // TODO: handle exceptionrunOnUiThread(new Runnable() 
       isInDb = false; 
       xmlToStruct(); 
       saveMetars(getApplicationContext(), metars); 
       t.cancel(); 
      } 

     } 
    },0,12000); 
1

從設計角度看,建議將數據存儲爲數據(以XML或數據庫或某種結構化格式)。然而,在具體回答這個問題的興趣...

「metars-array」與目錄「app_metars_array」(不相同的URI)不匹配... 其次,對目錄和文件的引用似乎是混合的。 仔細檢查引用文件與目錄(我沒有看到通過文件的目錄遍歷,但即使目錄被選中,但文件引用)...

因此文件未找到錯誤。非常直接。 如果相對路徑可能會更改,請確保使用規範名稱(完整路徑)。 也就是說,「/數據」意味着「/ data」是根源,但它是作爲相對的?

+0

我已經解決了這個問題;) – cpfp

+0

大。如果這個評論是答案,請隨時標記這個答案。謝謝! –