Android 文件读写

Android 文件读写


      学过Java的同学都知道,我们新建文件,然后就可以写入数据了,但是Android却不一样,因为Android是基于Linux的,我们在读写文件的时候,还需加上文件的操作模式,Android中的操作模式如下:


1、文件操作模式

2、文件常用方法

3、文件工具类

public class FileHelper {

    private Context mContext;

    public FileHelper() {
    }

    public FileHelper(Context mContext) {
        super();
        this.mContext = mContext;
    }

    /*
    * 这里定义的是一个文件保存的方法,写入到文件中,所以是输出流
    * */
    public void save(String filename, String filecontent) throws Exception {
        //这里我们使用私有模式,创建出来的文件只能被本应用访问,还会覆盖原文件哦
        FileOutputStream output = mContext.openFileOutput(filename, Context.MODE_PRIVATE);
        output.write(filecontent.getBytes());  //将String字符串以字节流的形式写入到输出流中
        output.close();         //关闭输出流
    }


    /*
    * 这里定义的是文件读取的方法
    * */
    public String read(String filename) throws IOException {
        //打开文件输入流
        FileInputStream input = mContext.openFileInput(filename);
        byte[] temp = new byte[1024];
        StringBuilder sb = new StringBuilder("");
        int len = 0;
        //读取文件内容:
        while ((len = input.read(temp)) > 0) {
            sb.append(new String(temp, 0, len));
        }
        //关闭输入流
        input.close();
        return sb.toString();
    }

}

4.读取SD卡上的文件


public class SDFileHelper {

    private Context context;

    public SDFileHelper() {
    }

    public SDFileHelper(Context context) {
        super();
        this.context = context;
    }

    //往SD卡写入文件的方法
    public void savaFileToSD(String filename, String filecontent) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
            FileOutputStream output = new FileOutputStream(filename);
            output.write(filecontent.getBytes());
            //将String字符串以字节流的形式写入到输出流中
            output.close();
            //关闭输出流
        } else Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
    }

    //读取SD卡中文件的方法
    //定义读取文件的方法:
    public String readFromSD(String filename) throws IOException {
        StringBuilder sb = new StringBuilder("");
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
            //打开文件输入流
            FileInputStream input = new FileInputStream(filename);
            byte[] temp = new byte[1024];
            int len = 0;
            //读取文件内容:
            while ((len = input.read(temp)) > 0) {
                sb.append(new String(temp, 0, len));
            }
            //关闭输入流
            input.close();
        }
        return sb.toString();
    }
}

最后别忘记在AndroidManifest.xml写上读写SD卡的权限哦!

<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,文件操作是非常常见的,比如读取或写入文本、图片或视频等文件。本文将简要介绍在Android中实现文件读写的一些基本知识和技巧。 1. 读取文件Android中,读取文件通常有两种常用的方法:使用FileInputStream和BufferedReader。如下所示: String filePath = "your/file/path"; // 使用FileInputStream读取 FileInputStream inputStream = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int length = 0; while ((length = inputStream.read(buffer)) > 0) { String content = new String(buffer, 0, length); Log.i("File", "Content: " + content); } inputStream.close(); // 使用BufferedReader读取 FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { Log.i("File", "Line: " + line); } bufferedReader.close(); 2. 写入文件 如果需要向文件中写入内容,我们可以使用FileOutputStream和BufferedWriter来实现。 String filePath = "your/file/path"; // 使用FileOutputStream写入 FileOutputStream outputStream = new FileOutputStream(filePath); String content = "Hello World!"; outputStream.write(content.getBytes()); outputStream.close(); // 使用BufferedWriter写入 FileWriter fileWriter = new FileWriter(filePath); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); String line = "Hello World!"; bufferedWriter.write(line); bufferedWriter.flush(); bufferedWriter.close(); 需要注意的是,在执行文件写入操作时,需要先授予应用程序写入文件的权限。可以在AndroidManifest.xml中添加WRITE_EXTERNAL_STORAGE权限。 总结 以上是Android文件读写的基本内容。在实际开发中,还需要注意文件路径的获取、文件不存在的判断等问题。另外,需要注意写入的权限授权和向用户说明应用程序写入的具体目的和内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值