• 复制res下文件进sd卡


    一、FileUtils

    package com.ct.save;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import android.os.Environment;
    
    public class FileUtils {
    
        private String SDPATH;
    
        public String getSDPATH() {
            return SDPATH;
        }
        public FileUtils() {
            //得到当前外部存储设备的目录
            // /SDCARD
            SDPATH = Environment.getExternalStorageDirectory() + "/";
        }
        /**
         * 在SD卡上创建文件
         * 
         * @throws IOException
         */
        public File creatSDFile(String fileName) throws IOException {
            File file = new File(SDPATH + fileName);
            file.createNewFile();
            return file;
        }
        
        /**
         * 在SD卡上创建目录
         * 
         * @param dirName
         */
        public File creatSDDir(String dirName) {
            File dir = new File(SDPATH + dirName);
            dir.mkdirs();
            return dir;
        }
    
        /**
         * 判断SD卡上的文件夹是否存在
         */
        public boolean isFileExist(String fileName){
            File file = new File(SDPATH + fileName);
            return file.exists();
        }
        
        /**
         * 将一个InputStream里面的数据写入到SD卡中
         */
        public File write2SDFromInput(String path,String fileName,InputStream input){
            File file = null;
            OutputStream output = null;
            try{
    //            creatSDDir(path);
                file = creatSDFile(path + fileName);
                output = new FileOutputStream(file);
                byte buffer [] = new byte[4 * 1024];
                while((input.read(buffer)) != -1){
                    output.write(buffer);
                }
                output.flush();
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                try{
                    output.close();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
            return file;
        }
    
    }

    二、MainActivity

    package com.ct.save;
    
    import java.io.File;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MySaveActivity extends Activity {
        /** Called when the activity is first created. */
        private File file;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init();
        }
        
        private void init(){
            FileUtils fileUtils = new FileUtils();
            file = fileUtils.write2SDFromInput("", "lyj.mp3", 
                    getResources().openRawResource(R.raw.lyj));
            
        }
    }

    三、记得要加权限

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

    (个人感觉比用cmd会没效率得多,在F:\java\MySave)

  • 相关阅读:
    抽象类
    继承
    面向对象的静态属性,类方法,静态方法
    查找linux系统下的端口被占用进程的两种方法 【转】
    awk学习
    【转】借助LVS+Keepalived实现负载均衡
    ORA-28001: the password has expired解决方法
    ORACLE的还原表空间UNDO写满磁盘空间,解决该问题的具体步骤
    Oracle控制文件多路复用以及Oracle备份重建控制文件
    RedHat6.1通过配置yum server安装软件包
  • 原文地址:https://www.cnblogs.com/ct732003684/p/2864652.html
Copyright © 2020-2023  润新知