Mam aplikację, która zapisuje dane do pliku (pamięć wewnętrzna) danych wprowadzanych przez użytkownika i podczas uruchamiania ładuje ten plik i wyświetla jego zawartość. Chciałbym wiedzieć: gdzie mogę znaleźć mój plik (data.txt)? Oprócz tego, jeśli wpisuję "Hello", a następnie "World", gdy ładuję plik widzę "HelloWorld" w tej samej linii, ale chcę "Hello" i "World" wydrukowane w 2 różnych liniach.Plik zapisu lokalizacji Android FileOutputStream
Do zapisywania pliku:
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());
}
}
Dla pliku ładowania:
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;
}
Dzięki
[UPDATE]
mam wstawiony "\ n" po każdym wejściu:
user = (EditText) v.findViewById(R.id.username);
writeToFile(user.getText().toString() + "\n");
Ale kiedy drukowanie mój plik Są zawsze na tej samej linii
jaki sposób można ustawić nową ścieżkę do zapisania pliku ? – xXJohnRamboXx