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:

  1. how to create a folder
  2. how then in this folder to write the file
  3. then how to get a list of folders and files in the root and their subfolders and files
  4. 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.

    1 answer 1

     /* Π—Π°ΠΏΠΈΡΡŒ Π² Ρ„Π°ΠΉΠ» ΠΏΠΎ ΠΏΡƒΡ‚ΠΈ /data/data/имя вашСго ΠΏΠ°ΠΊΠ΅Ρ‚Π°/имя ΠΏΠ°ΠΏΠΊΠΈ/имя Ρ„Π°ΠΉΠ»Π° */ public void writeToFileInYourFolder(byte[] someData, String internalFolderName, String internalFileName) { final FileOutputStream fileOutputStream; // ΠΏΡƒΡ‚ΡŒ ΠΊ ΠΏΠ°ΠΏΠΊΠ΅ final File internalPath = new File(getBaseContext().getDir(internalFolderName, Context.MODE_PRIVATE).getAbsolutePath()); // созданиС ΠΏΠ°ΠΏΠΊΠΈ, Ссли Π½Π΅ сущСствуСт if (!internalPath.exists()) internalPath.mkdirs(); // Ρ„Π°ΠΉΠ» для записи final File internalFile = new File(internalPath, internalFileName); try { fileOutputStream = new FileOutputStream(internalFile); fileOutputStream.write(someData); fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException exception) { Log.e("LOGTAG", exception.toString()); } } 
    • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky ♦