Android内部存储指定多级目录下保存文件的实现方法咨询
Great question! To save your file to files/Somefolder/otherfolderchild/filename.txt, you do need to create the parent directories first—Android's built-in openFileOutput() method can't automatically create nested subdirectories for you. If you try to use it with a path containing subdirectories directly, you'll get an IOException because the directories don't exist yet.
Here's how to modify your existing MainActivity.java code to achieve this:
Modified Write Button Method
This code first creates the nested directory structure, then writes your file to the target path:
// write text to file public void WriteBtn(View v) { // Define your nested directory path and target filename String subDirPath = "Somefolder/otherfolderchild"; String fileName = "filename.txt"; // Get the root "files" directory of your app's internal storage File rootFilesDir = getFilesDir(); // Create a File object for the nested directory structure File targetDir = new File(rootFilesDir, subDirPath); // Create all missing parent directories (if they don't exist) if (!targetDir.exists()) { boolean dirsCreated = targetDir.mkdirs(); if (!dirsCreated) { Toast.makeText(getBaseContext(), "Failed to create subdirectories!", Toast.LENGTH_SHORT).show(); return; } } // Create a File object for the target file File targetFile = new File(targetDir, fileName); // Write content to the file try { FileOutputStream fileout = new FileOutputStream(targetFile); OutputStreamWriter outputWriter = new OutputStreamWriter(fileout); outputWriter.write(textmsg.getText().toString()); outputWriter.close(); // Display success message Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getBaseContext(), "Error saving file!", Toast.LENGTH_SHORT).show(); } }
Modified Read Button Method
Update the read logic to match the new file path:
// Read text from file public void ReadBtn(View v) { String subDirPath = "Somefolder/otherfolderchild"; String fileName = "filename.txt"; File rootFilesDir = getFilesDir(); // Point to the target file in the nested directory File targetFile = new File(new File(rootFilesDir, subDirPath), fileName); // Reading text from file try { FileInputStream fileIn = new FileInputStream(targetFile); InputStreamReader InputRead = new InputStreamReader(fileIn); char[] inputBuffer = new char[READ_BLOCK_SIZE]; String s = ""; int charRead; while ((charRead = InputRead.read(inputBuffer)) > 0) { String readstring = String.copyValueOf(inputBuffer, 0, charRead); s += readstring; } InputRead.close(); textmsg.setText(s); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getBaseContext(), "Error reading file!", Toast.LENGTH_SHORT).show(); } }
Key Notes
getFilesDir(): Returns the absolute path to your app's private internal storage directory (/data/data/com.bla.smthing/files), which is the "files" folder you view in Android Studio's Device File Explorer.mkdirs(): Unlikemkdir()(which only creates a single directory level),mkdirs()creates all missing parent directories in the path—critical for nested folders likeSomefolder/otherfolderchild.- No extra permissions needed: Since we're using your app's private internal storage, Android automatically grants access without needing
WRITE_EXTERNAL_STORAGEor similar permissions.
After making these changes, you'll find your file at exactly the path you wanted: files/Somefolder/otherfolderchild/filename.txt in Android Studio's file manager.
内容的提问来源于stack exchange,提问作者Rupesh Kumar

