2014-02-12 38 views
2

我有這種方法,應該在一arrayList寫入文件:不能我的ArrayList中寫入一個文件如預期

private ArrayList<String> readFromFile() { 
    String ret = ""; 
    ArrayList<String> list = new ArrayList<String>(); 
try { 
    InputStream inputStream = openFileInput("jokesBody.bjk"); 

    if (inputStream != null) { 
     InputStreamReader inputStreamReader = new InputStreamReader(
       inputStream); 
     BufferedReader bufferedReader = new BufferedReader(
       inputStreamReader); 
     String receiveString = ""; 
     StringBuilder stringBuilder = new StringBuilder(); 

     while ((receiveString = bufferedReader.readLine()) != null) { 
      list.add(receiveString); 
     } 

     inputStream.close(); 
     ret = stringBuilder.toString(); 
     System.out.println("DA CRAZY FILE: " + ret); 
    } 
} catch (FileNotFoundException e) { 
    Log.e("login activity", "File not found: " + e.toString()); 
} catch (IOException e) { 
    Log.e("login activity", "Can not read file: " + e.toString()); 
} 

return list; 
} 

它的問題是,它寫得像[item1, item2, item3]後來當值我需要將值加載回listArray,它將整個行加載到索引0處。現在我已經找到了編寫和讀取arrayList的corerct方法,但是我遇到了訪問teh文件的麻煩。

這裏是我試過的代碼:

 private void writeToFile(ArrayList<String> list) { 
       try { 

        FileOutputStream fos = new FileOutputStream("jokesBody.bjk"); 
         ObjectOutputStream oos = new ObjectOutputStream(fos); 
         oos.writeObject(list); // write MenuArray to ObjectOutputStream 
         oos.close(); 

       } catch (IOException e) { 
        Log.e("Exception", "File write failed: " + e.toString()); 

         } 
} 

但它拋出以下異常:

02-12 09:21:10.227: E/Exception(2445): File write failed: java.io.FileNotFoundException: /jokesBody.bjk: open failed: EROFS (Read-only file system) 

哪裏是錯,這裏是默認的應用程序文件的位置?我知道我錯過了一些小東西,但作爲一名Android初學者,我無法發現它。

+1

你沒有寫權限 – Triode

回答

3

是不是這樣的:

java.io.FileNotFoundException: /jokesBody.bjk: open failed: EROFS (Read-only file system) 

的問題?您正在寫入一個不可寫區域。改變你正在寫的地方(也許creating a temporary file would be a simple first step-我不熟悉Android,但我認爲這是可能的)

+0

我明白了,但是我明白了對我來說很奇怪的是,當我使用'InputStream'時一切正常,但是當我改變到'FileOutputStream'時,這個區域被禁止了......臨時文件在我的情況下是不會做的。 :( – Slim

1

你的文件似乎是隻讀的。你不能寫入只讀文件!

1

我不認爲你實際上保存了你認爲你的文件。在向外部存儲器寫入文件時查看this tutorial。幾件事情:

(1)您需要在清單中請求許可以寫入外部存儲。如果不是,您最終只能看到一個只讀的情況。 (2)在寫入代碼之前,需要在代碼中獲取外部存儲目錄。這應該與一般支票preceeded爲你的文件目錄是否是擺在首位寫:

public boolean isExternalStorageWritable() {  
    String state = Environment.getExternalStorageState();  
    if (Environment.MEDIA_MOUNTED.equals(state)) {   
     return true;  
    }  
    return false; 
} 

然後,您可以創建你要存儲和它們在該位置存儲,所以你的文件的特定目錄稍後可以找到它們。例如:

public File getAlbumStorageDir(String albumName) { 
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);  
    if (!file.mkdirs()) {   
     Log.e(LOG_TAG, "Directory not created");  
    }  
    return file; 
} 

然後,您可以寫的內容是返回

+0

我明白了,但對我來說奇怪的是,當我使用InputStream時,一切正常,但是當我更改爲FileOutputStream時,該區域被禁止... – Slim

+0

這並不會讓我感到驚訝。僅僅因爲我可以從'InputStream'創建一些東西並不意味着我正在寫入的文件或區域是可寫目錄。這就是爲什麼你需要首先檢查你寫的是什麼,獲取適當的文件位置,然後寫在那裏。 – Rarw

1

當您正在爲Android開發的文件,你必須從Context OutputStream的:

fos = context.openFileOutput("jokesBody.bjk", Context.MODE_PRIVATE); 

的解釋關於如何使用Android上的文件在這裏:Saving Files