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

时间:2018-03-11 16:06:36

标签: android storage file-handling

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

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

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

}

错误

  

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void java.io.PrintWriter.println(java.lang.String)'

2 个答案:

答案 0 :(得分:0)

你真的不需要这么多代码

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

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");

答案 1 :(得分:0)

解决方案已在StackOverflow上提供;你想查看下面的链接。这正是你想要做的。

将文件写入内部存储

  

Android: how to write a file to internal storage

从内部读取文件

  

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