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
- android 拷贝文件到其他目录下
- Android 读取Assets资源文件或目录,并拷贝到其他目录下
- 【Android】拷贝文件到另一个目录下
- 【Android】拷贝文件到另一个目录下
- 【Android】拷贝文件到另一个目录下
- 【Android】拷贝文件到另一个目录下
- 【Android】拷贝文件到另一个目录下
- Android 拷贝文件到另一个目录下
- 【Android】拷贝文件到另一个目录下
- 【Android】拷贝文件到另一个目录下
- android拷贝assets目录下文件到其他目录
- 把包含相应字符串的文件拷贝到其他目录
- Android makefile文件拷贝一个文件夹下所有文件到out/system目录下的方法
- java从一个目录拷贝文件到另一个目录下
- ubuntu 拷贝文件夹下所有文件到其他文件夹操作
- cmd命令 拷贝某目录及其子目录文件到其他目录
- Android 拷贝assets目录下所有文件及文件夹到指定路径
- VS2012把解决方案里的xml文件,其他文件自动拷贝输出到debug目录、程序执行目录、部署目录
- mysql三大范式
- iOS 国际化的踩坑
- go基本语法
- nginx url rewrite 的问号规则
- unix环境高级编程第三版源代码编译及使用
- android 拷贝文件到其他目录下
- 以太坊连载(三):Web 3:去中心化应用平台
- VersionUtils
- 常用的设计模式与设计原则
- 百度搜索屏蔽掉右侧的推荐
- Android TouchEvent事件分发机制详解(1)
- ConcurrentHashMap 原理
- tomcat的处理过程
- ESP8266 How-to