package com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class TestHtml {
/**
* 复制单个文件
* @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();

}

}
public static void main(String[] args)throws Exception {
// //这是你的源文件,本身是存在的
// File beforefile = new File("C:/Users/Administrator/Desktop/Untitled-2.html");
//
// //这是你要保存之后的文件,是自定义的,本身不存在
// File afterfile = new File("C:/Users/Administrator/Desktop/jiekou0/Untitled-2.html");
//
// //定义文件输入流,用来读取beforefile文件
// FileInputStream fis = new FileInputStream(beforefile);
//
// //定义文件输出流,用来把信息写入afterfile文件中
// FileOutputStream fos = new FileOutputStream(afterfile);
//
// //文件缓存区
// byte[] b = new byte[1024];
// //将文件流信息读取文件缓存区,如果读取结果不为-1就代表文件没有读取完毕,反之已经读取完毕
// while(fis.read(b)!=-1){
// //将缓存区中的内容写到afterfile文件中
// fos.write(b);
// fos.flush();
// }
String oldPath="C:/Users/Administrator/Desktop/Untitled-2.html";
String newPath="C:/Users/Administrator/Desktop/jiekou0/Untitled-2.html";
TestHtml t=new TestHtml();
t.copyFile(oldPath, newPath);

}
}

引用自:http://zhidao.baidu.com/link?url=83SQQKvuLR1aiA_QPPLE7K_TfZKIAPKUqjFalts6AHVCvtXoKZmNayuwrMQKyTObQkrJUCZjwvpxuqBP1FtTYK

http://www.educity.cn/wenda/214981.html 这两个

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

  1. Java_io_02_从一个目录拷贝文件到另一个目录下

    java从一个目录拷贝文件到另一个目录下   http://www.cnblogs.com/langtianya/p/4857524.html ** * 复制单个文件 * @param oldPath ...

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

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

  3. Java中获取本地某一个目录下的所有文件和文件夹

    在从事web开发工作中,经常需要对本地某一个目录下的文件进行处理,而在这之前,我们需要做的就是获取到这个目录下的文件. String filepath = "D:\file";// ...

  4. Java 复制一个文件到另外一个目录下

    因为项目部署在jboss上面,在上传一些图片的时候,把他上传到当前项目的下,比如:(这里是以Windows服务器为例的,当然linux也是一样的) D:\jboss-eap-6.4\domain\se ...

  5. python 拷贝某个文件到另一个目录下

    python的shutil包含有很多文件拷贝的函数,各种各样的,要实现我文章题目的目的,使用shutil.copy函数即可 shutil.copy(文件的路径,另一个目录)

  6. Java移动文件到另外一个目录

    private void moveTotherFolders(String pathName,String fileName,String ansPath){ String startPath = t ...

  7. Java遍历一个目录下的所有文件

    Java遍历一个目录下的所有文件   Java工具中为我们提供了一个用于管理文件系统的类,这个类就是File类,File类与其他流类不同的是,流类关心的是文件的内容,而File类关心的是磁盘上文件的存 ...

  8. windows命令行下批量拷贝同一后缀的文件到另外一个目录

    一个目录下有很多文件夹,想拷贝每个文件夹下面的wmv文件到另外一个目录,如果鼠标打开一个文件,拷贝一个,再打开其他的,逐一操作,很麻烦的,百度了一下,xcopy命令就可以实现:例如将C盘x1目录下所有 ...

  9. java利用WatchService实时监控某个目录下的文件变化并按行解析(注:附源代码)

    首先说下需求:通过ftp上传约定格式的文件到服务器指定目录下,应用程序能实时监控该目录下文件变化,如果上传的文件格式符合要求,将将按照每一行读取解析再写入到数据库,解析完之后再将文件改名. 一. 一开 ...

随机推荐

  1. ABAP-动态编程

    转载:http://www.cnblogs.com/jiangzhengjun/p/4293407.html 动态编程 动态的基本语法 多种不同的动态编程 动态字段 动态类型 指定结构.内表组件字段的 ...

  2. Egret - timer

    相关:http://edn.egret.com/cn/index.php/article/index/id/154 1.Timer 的使用方法非常简单,我们只需要关心两个属性,三个方法和两个事件即可. ...

  3. python生成可执行文件保护源码

    工作中由于需要防止源代泄漏,需要将源代码隐藏,找到两种方法: 1.使用python生成的pyc文件. 这种方法的优点就是pyc文件生成很容易,缺点则是很容易通过工具得到源码,并且python版本不一致 ...

  4. ntohs, ntohl, htons,htonl的比较和详解【转】

    ntohs =net to host short int 16位 htons=host to net short int 16位 ntohs =net to host long int 32位 hto ...

  5. JAVA中会存在内存泄露吗

    所谓内存泄露就是指一个不再被程序使用的对象或变量一直被占据在内存中.java中有垃圾回收机制,它可以保证一对象不再被引用的时候,即对象编程了孤儿的时候,对象将自动被垃圾回收器从内存中清除掉.由于Jav ...

  6. 关于sql 增删改

    1.更改数据库的名称 --更改数据库的名称,逗号前面是之前的,后面是改成的名 sp_renamedb student,xuesheng 2.表中有数据的情况下再添加列.删除列 --后来添加列,只能默认 ...

  7. webrtc 开发之前必须了解的东西

    1.创建offer的时候带上参数:{ offerToReceiveAudio: true, offerToReceiveVideo: true } 2.onicecandidate 必须写在 setL ...

  8. 第五章 二叉树(c)二叉树

  9. 微信小程序及开发工具介绍

    http://mp.weixin.qq.com/wiki  这里下载开发者工具

  10. vortex

    vortex - Bing dictionary US['vɔr.teks]UK['vɔː(r)teks] n.旋涡:涡旋:低涡:感情(或局势)的旋涡 网络漩涡:涡流:旋风 变形Plural Form ...