How to create a file in Android? Code used:

String FILENAME = "hello_file"; String mystring = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(mystring.getBytes()); fos.close(); 

Throws FileNotFoundException. If thrust in try / catch, then IOException. What to do? On the Internet, found solutions that cause the same Exception. Thank you in advance...

    1 answer 1

     String FILENAME = "hello_file", mystring = "hello world!"; new File(Environment.getExternalStorageDirectory(), FILENAME).createNewFile(); FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(mystring.getBytes()); fos.flush(); fos.close(); 

    Here you can. That is, the file creation code is:

     new File(путь к файлу).createNewFile(); 
    • An IOException crashes. And the question arises: how to spell the file path? In my case, I write /res/raw/file.txt. Somewhere I found what I needed through asset. What to do? - Styopkin Stepan
    • @StyopkinStepan, the res folder is read only. Here stackoverflow.com/questions/11628058/… this issue was discussed - nick