我試圖將文本數據添加到「fav.txt」文件。我只能添加一個項目到文件並列出。如何將一個或多個項目添加到文件和列表中? 這是我將代碼添加到文件的代碼。openFileOutput獲取數據,但不列出它們
private void writeToFile(String data,Context context) {
try {
FileOutputStream outputStream;
outputStream = openFileOutput("fav.txt", Context.MODE_PRIVATE);
outputStream.write(data.getBytes());
outputStream.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
這個代碼是從文件
public List<String> listele(Context context) throws FileNotFoundException {
InputStream inputstream=context.openFileInput("fav.txt");
List<String> listem=new ArrayList<>();
try{
InputStreamReader readeer=new InputStreamReader(inputstream);
BufferedReader reader=new BufferedReader(readeer);
String line;
while((line=reader.readLine())!=null)
{
listem.add(line);
}
reader.close();
return listem;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
讀它的工作原理,但只返回一個項目?我怎樣才能解決這個問題?
你想要在你的文件中完全保存什麼? –
@DimaKozhevin只有edittext文本項,當用戶點擊按鈕 – omer1596
你想寫多個字符串嗎?讀取文件中的所有數據? –