I'm developing Android app, and I need save and load functions.
I want to save important data in private app storage with this function:
public static void save(Object file, String fileName, Context context)
throws IOException, FileNotFoundException {
FileOutputStream fos = context.openFileOutput(fileName,
context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(file);
oos.flush();
oos.close();
fos.close();
System.out.println(TAG + ": File saved successful!");
}
I've also written method for loading data. This works because I know the exact name of file.
I need now to do dynamic saving of files, with dynamic generation of names. How can I list files saved in pricate app storage? So I can load exact one?
Here you go!
File dir = getFilesDir();
File[] subFiles = dir.listFiles();
if (subFiles != null)
{
for (File file : subFiles)
{
// Here is each file
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With