android 拷贝文件到其他目录下

来源:互联网 发布:mplayerx mac 编辑:程序博客网 时间:2024/05/18 03:05

今天,讲讲如何拷贝文件到指定目录下。


一,简单的使用输入流进行拷贝。

/**      * 复制单个文件      * @param oldPath String 原文件路径 如:c:/fqf.txt      * @param newPath String 复制后路径 如:f:/fqf.txt      * @return boolean      */    public void copyFile(String oldPath, String newPath) {        try {            int bytesum = 0;            int byteread = 0;            File oldfile = new File(oldPath);            if (oldfile.exists()) { //文件存在时                InputStream inStream = new FileInputStream(oldPath); //读入原文件                FileOutputStream fs = new FileOutputStream(newPath);                byte[] buffer = new byte[1444];                int length;                while ( (byteread = inStream.read(buffer)) != -1) {                    bytesum += byteread; //字节数 文件大小                    System.out.println(bytesum);                    fs.write(buffer, 0, byteread);                }                inStream.close();            }        }        catch (Exception e) {            System.out.println("复制单个文件操作出错");            e.printStackTrace();        }    }    /**      * 复制整个文件夹内容      * @param oldPath String 原文件路径 如:c:/fqf      * @param newPath String 复制后路径 如:f:/fqf/ff      * @return boolean      */    public void copyFolder(String oldPath, String newPath) {        try {            (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹            File a=new File(oldPath);            String[] file=a.list();            File temp=null;            for (int i = 0; i < file.length; i++) {                if(oldPath.endsWith(File.separator)){                    temp=new File(oldPath+file[i]);                }                else{                    temp=new File(oldPath+File.separator+file[i]);                }                if(temp.isFile()){                    FileInputStream input = new FileInputStream(temp);                    FileOutputStream output = new FileOutputStream(newPath + "/" +                            (temp.getName()).toString());                    byte[] b = new byte[1024 * 5];                    int len;                    while ( (len = input.read(b)) != -1) {                        output.write(b, 0, len);                    }                    output.flush();                    output.close();                    input.close();                }                if(temp.isDirectory()){//如果是子文件夹                    copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);                }            }        }        catch (Exception e) {            System.out.println("复制整个文件夹内容操作出错");            e.printStackTrace();        }    }


二,使用缓冲流进行拷贝

import java.io.*;/** * 复制文件夹或文件夹 */public class CopyDirectory {    // 源文件夹     static String url1 = "f:/photos";    // 目标文件夹     static String url2 = "d:/tempPhotos";    public static void main(String args[]) throws IOException {        // 创建目标文件夹         (new File(url2)).mkdirs();        // 获取源文件夹当前下的文件或目录         File[] file = (new File(url1)).listFiles();        for (int i = 0; i < file.length; i++) {            if (file[i].isFile()) {                // 复制文件                 copyFile(file[i],new File(url2+file[i].getName()));            }            if (file[i].isDirectory()) {                // 复制目录                 String sourceDir=url1+File.separator+file[i].getName();                String targetDir=url2+File.separator+file[i].getName();                copyDirectiory(sourceDir, targetDir);            }        }    }// 复制文件 public static void copyFile(File sourceFile,File targetFile) throws IOException{        // 新建文件输入流并对它进行缓冲         FileInputStream input = new FileInputStream(sourceFile);        BufferedInputStream inBuff=new BufferedInputStream(input);        // 新建文件输出流并对它进行缓冲         FileOutputStream output = new FileOutputStream(targetFile);        BufferedOutputStream outBuff=new BufferedOutputStream(output);                // 缓冲数组         byte[] b = new byte[1024 * 5];        int len;        while ((len =inBuff.read(b)) != -1) {            outBuff.write(b, 0, len);        }        // 刷新此缓冲的输出流         outBuff.flush();                //关闭流         inBuff.close();        outBuff.close();        output.close();        input.close();    }    // 复制文件夹     public static void copyDirectiory(String sourceDir, String targetDir)            throws IOException {        // 新建目标目录         (new File(targetDir)).mkdirs();        // 获取源文件夹当前下的文件或目录         File[] file = (new File(sourceDir)).listFiles();        for (int i = 0; i < file.length; i++) {            if (file[i].isFile()) {                // 源文件                 File sourceFile=file[i];                // 目标文件                File targetFile=new File(new File(targetDir).getAbsolutePath()+File.separator+file[i].getName());                copyFile(sourceFile,targetFile);            }            if (file[i].isDirectory()) {                // 准备复制的源文件夹                 String dir1=sourceDir + "/" + file[i].getName();                // 准备复制的目标文件夹                 String dir2=targetDir + "/"+ file[i].getName();                copyDirectiory(dir1, dir2);            }        }    }}


第二种的代码比第一种的代码进行拷贝的效率快很多,所以建议使用第二种。



android 拷贝文件到其他目录下就讲完了。


就这么简单。























阅读全文
0 0