Android: how to write a file to internal storage

I am developing a simple Android application and I need to write a text file on the internal storage device. I know that there are many questions (and answers) on this question, but I really cannot understand what I am doing wrong.

This is part of the code that I use in my activity to write a file:

public void writeAFile(){
    String fileName = "myFile.txt";
    String textToWrite = "This is some text!";
    FileOutputStream outputStream;

   try {
      outputStream = openFileOutput(fileName , Context.MODE_PRIVATE);
      outputStream.write(textToWrite.getBytes());
      outputStream.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
}

I really can't understand what mistake I am making. In addition, I tried this project on my emulator in Android Studio and on my phone to understand where I was doing something wrong, but even with this project, not a single file was written either on the phone or on the emulator.

EDIT: , , :

public void ReadBtn(View v) {
    //reading text from file
    try {
        FileInputStream fileIn=openFileInput("myFile.txt");
        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();
    }
}

.

+4
4


EDIT: e.printStackTrace();, .

public void wrtieFileOnInternalStorage(Context mcoContext,String sFileName, String sBody){
    File file = new File(mcoContext.getFilesDir(),"mydir");
    if(!file.exists()){
        file.mkdir();
    }

    try{
        File gpxfile = new File(file, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();

    }catch (Exception e){
        e.printStackTrace();

    }
}

:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+8

, .

, . , Android SDK . , " ", , , , ( ).

, ,

+5

Save to internal memory

        data="my Info to save";
        try {
           FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE);
           fOut.write(data.getBytes());
           fOut.close();
           Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
        }
        catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }

Reading from internal storage

      try {
           FileInputStream fin = openFileInput(file);
           int c;
           String temp="";
           while( (c = fin.read()) != -1){
              temp = temp + Character.toString((char)c);
           }
           tv.setText(temp);
           Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
        }
        catch(Exception e){
        }
0
source

Source: https://habr.com/ru/post/1679442/


All Articles