per scrivere in un file:
try {
File myFile = new File(Environment.getExternalStorageDirectory().getPath()+"/textfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.write("replace this with your string");
myOutWriter.close();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
Per leggere dal file:
String pathoffile;
String contents="";
File myFile = new File(Environment.getExternalStorageDirectory().getPath()+"/textfile.txt");
if(!myFile.exists())
return "";
try {
BufferedReader br = new BufferedReader(new FileReader(myFile));
int c;
while ((c = br.read()) != -1) {
contents=contents+(char)c;
}
}
catch (IOException e) {
//You'll need to add proper error handling here
return "";
}
Così verranno restituiti il contenuto del file nella stringa di "contenuti"
Nota: è necessario fornire permessi di lettura e scrittura nel file manifest
fonte
2014-12-03 07:08:14
Vuoi che il tuo array venga salvato anche quando la tua attività è chiusa? – Darpan
Sì. Qualcosa come il download da Internet, quindi salvarlo la prossima volta caricarlo dalla memoria interna –