-2

I'm trying to create a directory in internal storage and then write my files in that directory. I'm able to create the directory in internal storage but it is not showing.

When I create new file in that directory and write in that file, I get an error of null pointer exception. File.createNewFile() method is ignored and my file is not created. I'm using printwriter to write in file and buffered reader to read. How to read and write files in internal storage?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exercise_monday);

    //Creating directory and file
     File internalPath = Environment.getDataDirectory();
     myDir = new File("/data/", "Directory");
     if(!myDir.exists()) {
         myDir.mkdir();
        Log.d(TAG, "Dir created");
     }

      monExerciseFile = new File(myDir, "Work_Monday.txt");
      if(!monExerciseFile.exists()) {
          try {
              boolean a = monExerciseFile.createNewFile();
        //monExerciseFile.createNewFile(); I have tried this statement also
              Log.d(TAG, "File created");
          } catch (IOException e) {
              e.printStackTrace();
              Log.d(TAG, "File not created");
          }
      }

    //Printwriter
    try {
        printWriter = new PrintWriter(monExerciseFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Setting toolbar for monday activity
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_monday);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        toolbar.setTitle(R.string.exercise_monday);
    }
}

public void addExercise(View view) {


    EditText et = (EditText) findViewById(R.id.m_child_et_1);
    String exerciseToBeWritten = String.valueOf(et.getText());
    printWriter.println(exerciseToBeWritten);

    try{
        BufferedReader br = new BufferedReader(new FileReader(monExerciseFile));
        Log.d("ExerciseMonday.class", br.readLine());

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

}

Error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.PrintWriter.println(java.lang.String)' on a null object reference

James Z
  • 12,209
  • 10
  • 24
  • 44
Sid
  • 29
  • 7

2 Answers2

0

You really don't need this much Code

Just Do this, And your Folder will be Created:

File file = new File(Environment.getExternalStorageDirectory(), "MAIN_FOLDER");
   if (!file.exists()) {
       file.mkdir();
   }
File folder1 = new File(Environment.getExternalStorageDirectory(), "MAIN_FOLDER/FOLDER_1");
   if (!folder1.exists()) {
      folder1.mkdir();
   }
File folder2 = new File(Environment.getExternalStorageDirectory(), "MAIN_FOLDER/FOLDER_2");
   if (!folder2.exists()) {
      folder2.mkdir();
   }

...

And If you Something Happen After the Folders Are Created, You Can Do as Below:

File file = new File(Environment.getExternalStorageDirectory(), "MAIN_FOLDER");
if (!file.exists()) {
  boolean success = file.mkdir();
  if (success) {
    // Folder Created and you can now something Else if Creation was Successful


  }
}

"MAIN_FOLDER" Is The Name of the Main Folder you want to Create

And "Folder1" and "Folder2" are Folders Inside that "MAIN_FOLDER"

NOW, IF you Want to Read a Folder or a File, Just Do Something like This:

    private File ReadFile(String FolderName) throws IOException {
        return new File(Environment.getExternalStorageDirectory(), FolderName);
    }

And just Enter The Name of the Folder you are Looking For:

ReadFile("MAIN_FOLDER");
//--------OR-----------
ReadFile("MAIN_FOLDER/Folder1");
//--------OR-----------
ReadFile("MAIN_FOLDER/Folder2");
//--------OR-----------

If you are Looking for A File. All You need the End type of the File.

For Example For a jpg Image, It would be like this:

ReadFile("MAIN_FOLDER/image.jpg");
Shahin
  • 391
  • 3
  • 9
0

The solution is already available on StackOverflow; you want to check the links below. It is what exactly you want to do.

Write file to internal storage

Android: how to write a file to internal storage

Read file from internal

How do I read the file content from the Internal storage - Android App