There is the following method for writing to the file:
void serializeEntity(Class<?> entityClass, HashMap<String, Object> entityStore) { String fileName = "entity_"+entityClass.getSimpleName()+".entity"; FileOutputStream fileOut = null; try { fileOut = MyApplication.getContext().openFileOutput(fileName, 0); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(entityStore); out.close(); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } This code writes data to a file which is located in the root folder, as far as I understand. I want to write the same file and then read it but not from the root folder, but add my own and write to my folder. For this, I use the following code:
public boolean isDirExist(String dirName) { File newdir = MyApplication.getContext().getDir(dirName, Context.MODE_PRIVATE); return newdir.exists(); } public void createDir(String dirName) { File newdir = MyApplication.getContext().getDir(dirName, Context.MODE_PRIVATE); if (!isDirExist(dirName)) newdir.mkdirs(); } Here is the problem, the method isDirExist() - returns true what name of the folder I would not pass there. And the createDir() method does not create a folder, or creates it, but not where I expect to see it later.
I list the files in this way:
public void printFilesList() { for (String file : MyApplication.getContext().getFilesDir().getAbsoluteFile().list()) { Log.wtf(TAG, "FILE: " + file); } } This method shows me all the files that I wrote using the first method (which is in the root), but I do not see the folders I created there.
The question consists of several:
- how to create a folder
- how then in this folder to write the file
- then how to get a list of folders and files in the root and their subfolders and files
- How to read a file from a subfolder.
PS Read / write is interested in internal storage , so that the user would not have access to them.