2016-04-27 45 views
0

我有一個static ArrayList<String> items這是通過listview顯示後,它被填充元素。最簡單的方法來編寫一個ArrayList到Java文本文件

什麼是最簡單的方法來保存arraylist和/或listview到文本文件?

+1

我想,你應該檢查[this](http://stackoverflow.com/questions/16111496/java-how-can-i-write-my-arraylist-to-a-file-and-read-load - 這個文件到)的答案。 – 306

+0

嗯我試過它tbh但無濟於事。也許它是因爲我使用了一個arrayadapter。 https://gyazo.com/ace5e232e9cebe53d3d8f236941d1842 –

回答

0

我已經這樣做了。但是這裏的身體是JSON字符串。

public void saveTextFile(Context context, String sFileName, String sBody) { 
    try { 
     String root = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     File filePath = new File(root + "/Personal Encyclopedia/Data" + File.separator); 
     if (!filePath.exists()) { 
      filePath.mkdirs(); 
     } 
     File writeFile = new File(filePath, sFileName); 
     FileWriter writer = new FileWriter(writeFile, true); 
     writer.append(sBody); 
     writer.flush(); 
     writer.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

讓你ArrayList一個字符串(通過重寫toString()方法可能),並用文件名YourFileName.txt調用。可能是有幫助的

+0

thx爲你的答案傢伙..我有點做了,但輸出是一個字符串,而不是一個列表視圖(我想這是很難做到)。使用不同的代碼: https://gyazo.com/bfd329d0ba9d43040935d964a226d42c –

相關問題