2014-02-13 31 views
1

我有這樣的代碼在我的活動:寫作奇怪的問題/讀取的ArrayList到一個文件

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_top_jokes_body); 

     final GlobalsHolder globals = (GlobalsHolder)getApplication(); 

     TextView text = (TextView) findViewById(R.id.txtJoke); 
     text.setText(globals.getList().get(globals.clickedPosition)); 

     ArrayList<String> jokeBodyList = new ArrayList<String>(); 


     jokeBodyList.addAll(readFromFile()); 
     jokeBodyList.add(text.getText().toString()); 

     System.out.println("List content(jokeBodyList): " + jokeBodyList.toString()); 
     writeToFile(jokeBodyList); 
     System.out.println("The file content(new)" + readFromFile().toString()); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.top_jokes_body, menu); 
     return true; 
    } 

    /* Write content to a file */ 
    private void writeToFile(ArrayList<String> list) { 

     FileOutputStream fos; 
     if(list != null){ 
     try { 
       fos = openFileOutput("jokesBody2.bjk",Context.MODE_PRIVATE); 
       ObjectOutputStream out = new ObjectOutputStream(fos); 
       out.writeObject(list.toString()); 
       out.close(); 
     } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 
     }else{ 
      try { 
       fos = openFileOutput("jokesBody2.bjk",Context.MODE_PRIVATE); 
       ObjectOutputStream out = new ObjectOutputStream(fos); 
       out.writeObject(""); 
       out.close(); 
     } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 
     } 
    } 

    /* Read file's content */ 
    private ArrayList<String> readFromFile() { 
     File file = new File("jokesBody.bjk"); 
     String ret = ""; 
     ArrayList<String> list = new ArrayList<String>(); 
     try { 
      InputStream inputStream = openFileInput(file.toString()); 

      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(); 
      } 
     } catch (FileNotFoundException e) { 
      Log.e("log activity", "File not found: " + e.toString()); 
     } catch (IOException e) { 
      Log.e("log activity", "Can not read file: " + e.toString()); 
     } 

     return list; 
    } 

主要思想是通過對文件的內容到ArrayList然後到另一個值添加到同一ArrayList和然後將列表寫回到文件。 我有兩種方法可以讀取和寫入文件,問題在於寫入和讀取有問題。我的意思是這兩張印花

System.out.println("List content(jokeBodyList): " + jokeBodyList.toString()); 
System.out.println("The file content(new)" + readFromFile().toString()); 

返回一些奇怪的值。這是輸出:

02-13 09:10:22.482: I/System.out(2286): List content(jokeBodyList): [����tT[���sr�java.util.ArrayListx����a��I�sizexp���w���, t�����sr�java.util.ArrayListx����a��I�sizexp���w��� t��t��t��t�.joke 13joke 12Joke 8 ,joke 14 ,joke 13 ,Joke 9t� ,Joke 9t� ,Joke 8t�t� ,Ceco's joket� ,joke 13xt�  ,joke 12x, ,joke 12], joke 10] 
02-13 09:10:22.542: I/System.out(2286): The file content(new)[����tT[���sr�java.util.ArrayListx����a��I�sizexp���w���, t�����sr�java.util.ArrayListx����a��I�sizexp���w��� t��t��t��t�.joke 13joke 12Joke 8 ,joke 14 ,joke 13 ,Joke 9t� ,Joke 9t� ,Joke 8t�t� ,Ceco's joket� ,joke 13xt�  ,joke 12x, ,joke 12]] 

即使文本文件爲空,也會發生這種情況。我錯過了什麼?我知道這是一個非常小的東西,但我無法找到它。

回答

3

out.writeObject(list.toString());是完全錯誤! 使用out.writeObject(list);這是ObjectOutputStream的用途。

閱讀是周圍的其他方式:

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); 
yourArrayList = (ArrayList)ois.readObject(); 
ois.close();