【问题标题】:How to read and write file in internal storage?如何在内部存储中读写文件?
【发布时间】:2018-03-11 16:06:35
【问题描述】:

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

当我在该目录中创建新文件并写入该文件时,我收到空指针异常错误。 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)”

【问题讨论】:

  • 请发布您的代码和确切的错误信息。

标签: android storage file-handling


【解决方案1】:

你真的不需要这么多代码

只需这样做,您的文件夹就会被创建:

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

【讨论】:

    【解决方案2】:

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

    将文件写入内部存储

    Android: how to write a file to internal storage

    从内部读取文件

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

    【讨论】:

      猜你喜欢
      • 2012-04-01
      • 1970-01-01
      • 2018-05-24
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 2013-05-17
      相关资源
      最近更新 更多