Android 将asserts文件夹内文件写入SD卡中

更详细的内容: 

Android 文件读写以及assets操作


----------------------------------------------------------------------------------------------------------------------------------------


参考:

http://www.cnblogs.com/wainiwann/p/3274386.html


#######################################################################


    private void copyBigDataToSD(String strOutFileName)
    {
        try {
            String path = PATH + "/" + strOutFileName;
            InputStream myInput;
            OutputStream myOutput = new FileOutputStream(path);
            myInput = this.getAssets().open(strOutFileName);
            byte[] buffer = new byte[1024];
            int length = myInput.read(buffer);
            while (length > 0) {
                myOutput.write(buffer, 0, length);
                length = myInput.read(buffer);
            }

            myOutput.flush();
            myInput.close();
            myOutput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //从assets 文件夹中获取文件并读取数据
    public void getFromAssets(String fileName){
        try {
            File file = new File(PATH, fileName);
            if (file.exists()) { //if the file is exist, return
                file.delete();
            }
            file.createNewFile();

            InputStream in = getResources().getAssets().open(fileName);
            //获取文件的字节数
            int lenght = in.available();
            //创建byte数组
            byte[]  buffer = new byte[lenght];
            //将文件中的数据读到byte数组中
            in.read(buffer);

            FileOutputStream out = new FileOutputStream(file);
            out.write(buffer);
            out.flush();
            out.close();
            Log.i(TAG, "------------------------------------load ok " + fileName);
            //result = EncodingUtils.getString(buffer, ENCODING);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "--------------------------"+e.getMessage());
        }
    }

    public void getFromAssets(String fileName){
        try {
            File file = new File(PATH, fileName);
            if (file.exists()) { //if the file is exist, return
                file.delete();
            }
            file.createNewFile();
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));

            InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line="";
            String Result="";
            while((line = bufReader.readLine()) != null) {
                Result = line;
                Result += "\n";
                bw.write(Result);
                bw.flush();
            }

            bw.close();
            inputReader.close();
            bufReader.close();
            Log.i(TAG, "------------------------------------load ok "+fileName);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "--------------------------"+e.getMessage());
        }

        //     return "can not access assets";
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值