I have a static ArrayList<String> items
which is shown via listview after it gets populated with elements.
What is the easiest way to to save the arraylist and/or listview to a textfile?
I have a static ArrayList<String> items
which is shown via listview after it gets populated with elements.
What is the easiest way to to save the arraylist and/or listview to a textfile?
I've done it this way. But the body here is JSON String.
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();
}
}
Make your ArrayList
a string (possible by overriding toString()
method) and call with file name YourFileName.txt
. May be it is helpful