【Android】从raw中读取txt,写入手机存储

实现效果:
在download文件夹下创建printf.txt,并且写入指定内容
这里写图片描述

文字内容:
这里写图片描述

上代码,如果你现在打开着Android Studio,可以新建一个工程跟我一起操作
1、右键main/res,新建一个raw文件夹
这里写图片描述
选择raw
这里写图片描述
2、右键raw文件夹,新建一个文件printf.txt(或者什么名,随便),然后再其中随便输入点什么
这里写图片描述
3、接下来打开AndroidManifest.xml,添加读写文件的权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bm.readtxt0130">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4、然后再MainActivity中输入如下内容,具体解释看注释

package com.bm.readtxt0130;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/*
*实现从RAW中读取数据,放入手机存储中
* */

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

		//文件操作需要在线程中进行
        new Thread(new Runnable() {
            @Override
            public void run() {
                RawToSDCard();
            }
        }).start();

    }

    private void RawToSDCard(){

        //操作1:读取raw中的内容到输入流中,
        //可以Ctrl键+F键查看inputStream是怎么被转换成输出流的
        InputStream inputStream=getResources().openRawResource(R.raw.printf);

        try {
            //File的构造方法可以放路径名,或者路径名+文件名,
            //不同android系统的存储系统路径不一样,
            //使用getAbsolutePath获取绝对路径
            File file=new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath()
                            +"/Download/");
            //判断文件夹是否存在,否则创建文件夹
            if (!file.exists()) {
                file.mkdirs();

            }
            file=new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath()
                            +"/Download/printf.txt");

            //判断文件是否存在,否则创建文件
            if (!file.exists()){
                file.createNewFile();
            }

            //使用文件输出流绑定文件,
            //后续对文件输出流写入,就是对文件写入。
            //关注file是怎么被赋值的。
            FileOutputStream fileOutputStream=new FileOutputStream(file);

            //定义一个字节数组输出流,用于将从raw中读取到的输出流数据放入该数组
            byte[] buffer=new byte[10];
            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
            int len=0;

            //操作2:转换。
            //在这里将inputStream(也就是raw中的数据)放入数组中,
            //Ctrl键+F关注byteArrayOutputStream的数据流到了哪
            while ((len=inputStream.read(buffer))!=-1){
                byteArrayOutputStream.write(buffer,0,len);
            }
            //转换成字节数组
            byte[] bs=byteArrayOutputStream.toByteArray();

            //操作3:写入。数据流入了文件输出流,
            //可以Ctrl键+F关注fileOutputStream何时绑定了文件
            fileOutputStream.write(bs);

            //关闭几个流
            byteArrayOutputStream.close();
            inputStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
        }catch (Exception e){
            Log.e("RawToSDCard",e.toString());
        }


    }

}

工程下载

猜你喜欢

转载自blog.csdn.net/bfz_50/article/details/79214056
今日推荐