java 文件拷贝操作
2014-04-09 13:31
281 查看
package com.hqlh.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* file copy operate
*
* @author jiapengxun
*
*/
public class FileOperateUtil {
/**
* 复制整个文件夹内容
*
* @param oldPath
* String 原文件路径 如:c:/fqf
* @param newPath
* String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public static void copyFolder(String oldpath, String newPath) {
try {
// 如果文件夹不存在 则建立新文件夹
(new File(newPath)).mkdirs();
File file = new File(oldpath);
String[] files = file.list();
File temp = null;
for (int i = 0; i < files.length; i++) {
if (oldpath.endsWith(File.separator)) {
temp = new File(oldpath + files[i]);
} else {
temp = new File(oldpath + File.separator + files[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 + "/" + files[i], newPath + "/"
+ files[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制单个文件
*
* @param oldPath
* String 原文件路径 如:c:/fqf.txt
* @param newPath
* String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (!oldfile.exists()) { // 文件不存在时
FileInputStream inStream = new FileInputStream(oldPath); // 读入原文件
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
new FileOutputStream(newPath).write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* file copy operate
*
* @author jiapengxun
*
*/
public class FileOperateUtil {
/**
* 复制整个文件夹内容
*
* @param oldPath
* String 原文件路径 如:c:/fqf
* @param newPath
* String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public static void copyFolder(String oldpath, String newPath) {
try {
// 如果文件夹不存在 则建立新文件夹
(new File(newPath)).mkdirs();
File file = new File(oldpath);
String[] files = file.list();
File temp = null;
for (int i = 0; i < files.length; i++) {
if (oldpath.endsWith(File.separator)) {
temp = new File(oldpath + files[i]);
} else {
temp = new File(oldpath + File.separator + files[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 + "/" + files[i], newPath + "/"
+ files[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制单个文件
*
* @param oldPath
* String 原文件路径 如:c:/fqf.txt
* @param newPath
* String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (!oldfile.exists()) { // 文件不存在时
FileInputStream inStream = new FileInputStream(oldPath); // 读入原文件
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
new FileOutputStream(newPath).write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- Java基础——IO(拷贝文件+字节流-字符流缓冲区(装饰设计)-转换流-IO操作规律-异常日志-系统信息列表输出)
- java的输入输出流与文件操作(3 .文件拷贝)
- javaIO_将一个目录下的文件拷贝到另一个目录下(边读边写操作)
- java File的基本操作实例(目录及文件的 新建/拷贝/移动)
- Java文件操作:拷贝、读取文件等
- 【Java】利用文件输入输出流完成把一个文件夹内的所有文件拷贝的另一的文件夹的操作
- java对文件拷贝的简单操作
- 关于java基础--PIO操作xls文件、持久化对象以及拷贝文件
- Java NIO.2 —— 文件或目录拷贝操作
- java操作文件――拷贝文件
- Java如何实现文件拷贝操作和如何正确关闭资源
- java 实现 文件操作工具集。包括文件、目录树的拷贝、删除、移动、查找等工具函数
- JavaIO操作-拷贝文件
- Java如何实现文件拷贝操作和如何正确关闭资源
- java IO操作实现文件及文件夹的拷贝
- java对文件拷贝的简单操作
- java 进行文件的压缩,解压,删除,拷贝操作
- android/java遍历拷贝文件和删除文件
- java操作csv文件(读、写)
- java之文件基本操作