java从一个目录拷贝文件到另一个目录下   http://www.cnblogs.com/langtianya/p/4857524.html

**
* 复制单个文件
* @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(); } }

Java_io_02_从一个目录拷贝文件到另一个目录下的更多相关文章

  1. scp从远程指定目录拷贝文件到本地指定目录

    scp从远程指定目录拷贝文件到本地指定目录 [root@picts ~]# cat /root/scp_pictures.sh #!/bin/bash # Function: copy files f ...

  2. Linux从一个服务器拷贝文件到另一个服务器上

    ***复制文件夹到另外一个服务器scp -r tmp root@114.215.80.12:/work/temp输入密码 scp -r customer root@114.215.80.12:/hom ...

  3. java从一个目录拷贝文件到另一个目录下

    ** * 复制单个文件 * @param oldPath String 原文件路径 如:c:/fqf.txt * @param newPath String 复制后路径 如:f:/fqf.txt * ...

  4. C#,拷贝文件到另一个文件夹下,替换文件夹中的文件

    /// <summary> /// 拷贝文件到另一个文件夹下 /// </summary> /// <param name="sourceName"& ...

  5. Jenkins 如何实现 拷贝文件到网络共享目录

    在使用jenkins中,发现拷贝文件时,不能在脚本中直接添加脚本实现. 我实现的一种方法,希望能对您有用. net use y: \\server_name\workspace "passw ...

  6. 使用element的upload组件实现一个完整的文件上传功能(下)

    本篇文章是<使用element的upload组件实现一个完整的文件上传功能(上)>的续篇. 话不多说,接着上一篇直接开始 一.功能完善—保存表格中每一列的文件列表状态 1.思路 保存表格中 ...

  7. java拷贝文件到另一个目录下

    package com.util; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream ...

  8. java、android 对比两个目录或文件是否是同一个目录或文件的方法

    由于软链接及android的外部卡mount方式存在,导致一个文件夹可能同时有两个路径,如: /mnt/sdcard1      /storage/ext_sdcard ,如果通过某种方式(如moun ...

  9. Linux系统编程---实现目录或者文件拷贝

    关于拷贝文件,前面写过一个例子:点击打开链接 ,可以看看,实现cp命令. 这次我们实现一个目录和文件的拷贝,综合点. #include <stdio.h> #include <fcn ...

随机推荐

  1. laravel学习之路3 数据库相关

    读写分离之多个读? 有 'host' => $readHosts[array_rand($readHosts)], 上面的好像有缓存问题php artisan config:cache ] ); ...

  2. linux 解决 安装软件一直出现重复的信息

    State : Sleeping, pid: Another app is currently holding the yum lock; waiting for it to exit... The ...

  3. div+css 画三角形

            <style type="text/css"> .rightdirection { width:0;height:0; line-height:0; b ...

  4. Warning: (3719, “‘utf8’ is currently an alias for the character set UTF8MB3, which will be replaced by UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.”)

    [1]本地版本 Mysql 8.0.12 创建表SQL: DROP TABLE IF EXISTS students; CREATE TABLE `students` ( `sId` ) UNSIGN ...

  5. 多媒体开发之视频格式---1080p逐行和1080i隔行

    (1)简介 (2)1080p和1080i的区别 (3) ------------------autho:pkf ----------------------time:2015-1-4 (1)简介 (2 ...

  6. Unity5 怎样做资源管理和增量更新

    工具 Unity 中的资源来源有三个途径:一个是Unity自己主动打包资源.一个是Resources.一个是AssetBundle. Unity自己主动打包资源是指在Unity场景中直接使用到的资源会 ...

  7. 全文检索引擎Solr的配置

    描述: 在Linux环境下实现高速的全文检索 一.当前环境: CentOS (Linux) 6.3 64 bit 二.所需软件 1.Java的JDK Java jdk 1.7.0[注意:solr5.x ...

  8. Mac Python建立简单的本地服务器

    由于Mac自带Python 所以省去我们去下载了 打开终端  执行python stm-macmini:~ apple$ pythonPython 2.7.10 (default, Jul 14 20 ...

  9. Android动画详解

    一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画 ...

  10. 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数

    typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...