Android:如何将文件写入内部存储 [英] Android: how to write a file to internal storage

查看:1076
本文介绍了Android:如何将文件写入内部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的android应用程序,我需要在内部存储设备中编写一个文本文件.我知道对此事有很多疑问(和答案),但我确实无法理解我以错误的方式正在做的事情.

I am developing a simple android application and I need to write a text file in internal storage device. I know there are a lot of questions (and answers) about this matter but I really cannot understand what I am doing in the wrong way.

这是我在活动中为了编写文件而使用的一段代码:

This is the piece of code I use in my activity in order to write the 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();
   }
}

我真的不明白我在犯什么错误.此外,我在Android模拟器上尝试了项目Studio和我的手机,以便了解我在哪里做错了什么,但是即使使用该项目,手机或仿真器上也不会写入文件.

I really cannot understand which mistake I am doing. In addition, I have tried this project on my emulator in Android Studio and my phone in order to understand where I am doing something wrong but even with that project no file is written neither on the phone or on the emulator.

我知道没有文件写入内部存储器,因为在写入文件后,我尝试使用以下代码读取文件的内容:

I know that no file is written to my internal storage because I try to read the content of the file, after I have written to it, with this code:

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

什么也没显示.

推荐答案

使用以下代码将文件写入内部存储

Use below code to write a file to internal storage

:添加了e.printStackTrace();来捕获阻止,这是一种很好的做法,并提供了权限提醒

added e.printStackTrace(); to catch block as a good practice and permissions reminders

public void writeFileOnInternalStorage(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();

    }
}

别忘了在清单中添加外部存储的读写权限:

And don't forget to add external storage writing AND reading permissions in the manifest:

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

在Android 6.0之后,必须请求WRITE_EXTERNAL_STORAGE权限 运行时间

After Android 6.0 WRITE_EXTERNAL_STORAGE permission must request in run time

requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 1);

然后可以在调用它的活动内的onRequestPermissionsResult()方法中处理许可请求的结果

and then can handle the result of permission request in onRequestPermissionsResult() method inside activity called from it

这篇关于Android:如何将文件写入内部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆