输入流输出流写入手机的文件

本文介绍了一种从资源文件夹复制文件到设备指定路径的方法,并详细展示了如何通过HTTP请求从服务器下载文件到本地存储的过程。

输入流输出流的根本作用是为了永久性的数据存储,这里利用输入流将raw文件夹下的.js文件读到buffer缓冲区中,再写到手机指定路径下(即一个文件)。(File更应该叫做一个路径)


     例如EngineNew项目中:


 copyBinary(R.raw.widget6510, path + "widget6510.js");

public void copyBinary(int id, String path) {
//        log("copy -> " + path);
        try {
            InputStream ins = getResources().openRawResource(id);
            int size = ins.available();

            // Read the entire resource into a local byte buffer.
            byte[] buffer = new byte[size];
            ins.read(buffer);   // read()一次读取一个字节
            ins.close();

            FileOutputStream fos = new FileOutputStream(path);
            fos.write(buffer);
            fos.close();
        } catch (Exception e) {
            Log.d("public void createBinary() error! : " , e.getMessage().toString());
        }
    }
    
    
    在AutoMgrNew 项目中: 原理上都是控制输入流和输出流,理论一样,细节略有不同下面使用的是文件输入输出流
    
       public int getFileFromServer(String downLoadUrl, String pathUrl, String name) {
        this.isSotp = false;
        if(Environment.getExternalStorageState().equals("mounted")) {
            try {
                try {
                    URL e = new URL(downLoadUrl);
                    HttpURLConnection conn = (HttpURLConnection)e.openConnection();
                    conn.setConnectTimeout(5000);
                    long allSize = (long)conn.getContentLength();
                    this.downLoadListener.start(allSize, name);
                    InputStream is = conn.getInputStream();
                    File file = new File(pathUrl);
                    FileOutputStream fos = new FileOutputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(is);
                    byte[] buffer = new byte[1024];
                    int total = 0;

                    byte var15;
                    do {
                        int len;
                        if((len = bis.read(buffer)) == -1) {
                            fos.close();
                            bis.close();
                            is.close();
                            this.downLoadListener.stop();
                            var15 = 1;
                            return var15;
                        }

                        fos.write(buffer, 0, len);
                        total += len;
                        this.downLoadListener.progressSize((long)total, allSize);
                    } while(!this.isSotp);

                    this.downLoadListener.stop();
                    var15 = 0;
                    return var15;
                } catch (MalformedURLException var21) {
                    var21.printStackTrace();
                } catch (FileNotFoundException var22) {
                    var22.printStackTrace();
                } catch (IOException var23) {
                    var23.printStackTrace();
                }

                return 0;
            } finally {
                ;
            }
        } else {
            this.downLoadListener.stop();
            return 0;
        }
    }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值