Search code examples
javaandroiddocumentfileoutputstreaminternal-storage

Writing to file in Documents folder in internal storage


I've been trying to save text to a file in the documents folder in internal storage to be accessed by file manager so i can read it, I've tried several methods including using a writer, but I can't seem to get it to work, I'm not trying to save to external storage, I don't have external storage, only internal, and that's where my documents folder is, so I'm assuming I don't have to bother with the permissions in manifest, I threw in the setReadable just in case but I still can't find it in the documents folder, this is where I'm currently at.

public void writeToFile(String string){
        
try {
            
            File file = new File(Environment.DIRECTORY_DOCUMENTS, "myFile.txt");
            file.setReadable(true);
            FileOutputStream stream = new FileOutputStream(file);
            stream.write(string.getBytes(string));
            stream.flush();
            stream.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

    }

Solution

  • File file = new File(Environment.DIRECTORY_DOCUMENTS, "myFile.txt");

    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "myFile.txt");