我有一個應用程序保存到用戶輸入的文件(內部存儲)數據,並在啓動時加載此文件並顯示內容。我會知道:我在哪裏可以找到我的文件(data.txt)? 除了,如果我輸入「你好」,然後「世界」當我加載文件我看到「HelloWorld」在同一行,但我希望「你好」和「世界」打印在2不同的行。Android FileOutputStream位置保存文件
爲了節省文件:
public void writeToFile(String data) {
try {
FileOutputStream fou = openFileOutput("data.txt", MODE_APPEND);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fou);
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
對於加載文件:
public String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput("data.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
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 ret;
}
感謝
[更新]
我每次輸入後插入 「\ n」:
user = (EditText) v.findViewById(R.id.username);
writeToFile(user.getText().toString() + "\n");
但是,當我打印我的文件,他們總是在同一行
如何設置新的路徑保存文件? – xXJohnRamboXx