On an Android watch, you need to write information to a *.txt file *.txt To do this, I use the following code:

 public boolean write(){ boolean result = true; try { FileOutputStream fOut = context.openFileOutput("text.txt",context.MODE_WORLD_READABLE); fOut.write(createJson().getBytes()); fOut.close(); }catch (FileNotFoundException e) { e.printStackTrace(); result = false; }catch (IOException e) { e.printStackTrace(); result = false; } return result; } 

And for reading:

 public boolean read(){ boolean result = true; try { FileInputStream fin = context.openFileInput("text.txt"); int c; String temp=""; while( (c = fin.read()) != -1){ temp = temp + Character.toString((char)c); } fin.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); result = false; } catch (IOException e1) { e1.printStackTrace(); result = false; } return result; } 

The problem is that the MODE_WORLD_READABLE mode MODE_WORLD_READABLE already Deprecated in connection with which the question is, how can I read / write in another way?

    1 answer 1

    1. android fileoutputstream example

    2. http://developer.android.com/intl/ru/reference/java/io/FileOutputStream.html

        File file = ... OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); ... finally { if (out != null) { out.close(); } } } 

    Another option :

     String filePath = Environment.getExternalStorageDirectory() + "/text.txt"; try { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filePath )); ... out.close(); } catch (Exception e) { e.printStackTrace(); } 
    • I tried it, it works on the phone, and on the clock java.io.FileNotFoundException: test.txt: open failed: EROFS (Read-only file system) - Kirill Stoianov
    • @KirillStoianov, look at another option from the update of the answer - YurySPb
    • I also tried java.io.FileNotFoundException: /storage/emulated/0/text.txt: open failed: EACCES (Permission denied), there is permission to write! - Kirill Stoianov
    • @KirillStoianov, you have been sealed and you ask me if you have permission? .. Maybe you need to request it from the user in runtime. - Yuriy SPb
    • then a typo, permission spelled out. - Kirill Stoianov