如何读写内部存储中的文件?

How to read and write file in internal storage?

我正在尝试在内部存储中创建一个目录,然后将我的文件写入该目录。 我可以在内部存储中创建目录,但它没有显示。

当我在该目录中创建新文件并写入该文件时,出现空指针异常错误。 File.createNewFile() 方法被忽略,我的文件没有创建。 我正在使用 printwriter 写入文件并缓冲 reader 进行读取。 如何读写内部存储中的文件?

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();
    }

}

错误

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

你真的不需要这么多代码

只需执行此操作,即可创建您的文件夹:

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();
   }

...

如果文件夹创建后出现问题,可按如下操作:

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" 是您要创建的主文件夹的名称

并且"Folder1"和"Folder2"是"MAIN_FOLDER"

中的文件夹

现在,如果您想阅读文件夹或文件,只需执行以下操作:

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

然后只需输入您要查找的文件夹的名称:

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

如果您正在寻找文件。您只需要文件的结束类型。

例如对于一张jpg图片,应该是这样的:

ReadFile("MAIN_FOLDER/image.jpg");

该解决方案已在 Whosebug 上可用;你想检查下面的链接。这正是您想要做的。

将文件写入内部存储器

从内部读取文件

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