Java 文件复制 效率
来源:互联网 发布:java和前端哪个招的多 编辑:程序博客网 时间:2024/06/11 14:06
package web.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtil {
public static void main(String[] args) {
String oldPath = "E:/咱们结婚吧.mp4";
String newPath = "E:/test/咱们结婚吧.mp4";
FileUtil test1 = new FileUtil();
test1.copyFile2(oldPath, newPath);
}
/**
* 复制单个文件 40327
* @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);
long begin = System.currentTimeMillis();
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();
long end = System.currentTimeMillis();
System.out.println("总共耗时:"+ (end - begin));
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
// 利用缓冲流 复制文件 总共耗时:34244
public void copyFile2(String oldPath, String newPath) {
InputStream in = null;
OutputStream out = null;
long begin = System.currentTimeMillis();
try {
in = new BufferedInputStream(new FileInputStream(new File(oldPath)), 1024);
out = new BufferedOutputStream(new FileOutputStream(new File(newPath)), 1024);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
long end = System.currentTimeMillis();
System.out.println("总共耗时:"+ (end - begin));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtil {
public static void main(String[] args) {
String oldPath = "E:/咱们结婚吧.mp4";
String newPath = "E:/test/咱们结婚吧.mp4";
FileUtil test1 = new FileUtil();
test1.copyFile2(oldPath, newPath);
}
/**
* 复制单个文件 40327
* @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);
long begin = System.currentTimeMillis();
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();
long end = System.currentTimeMillis();
System.out.println("总共耗时:"+ (end - begin));
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
// 利用缓冲流 复制文件 总共耗时:34244
public void copyFile2(String oldPath, String newPath) {
InputStream in = null;
OutputStream out = null;
long begin = System.currentTimeMillis();
try {
in = new BufferedInputStream(new FileInputStream(new File(oldPath)), 1024);
out = new BufferedOutputStream(new FileOutputStream(new File(newPath)), 1024);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
long end = System.currentTimeMillis();
System.out.println("总共耗时:"+ (end - begin));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
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();
}
}
}
0 0
- Java 文件复制 效率
- java中复制文件的效率测试
- Java NIO文件复制以及与传统IO效率对比
- java io 复制文件与 nio 复制文件的效率问题
- java数组复制的效率
- 文件复制函数的效率比较
- 文件复制函数的效率比较
- IO流文件复制缓冲效率测试
- Java数组拷贝/复制的效率比较
- Java数组拷贝/复制的效率比较
- java的数组复制效率比较
- Java NIO——与传统IO文件复制的效率比较(channel.transferTo、Files.copy 、channel.write)
- java读取文件效率问题
- JAVA文件复制
- JAVA文件复制
- Java复制文件
- JAVA多线程复制文件
- Java实现文件复制
- hadoop SecondaryNameNode和NameNode
- 放大改进版~
- JS-JSON数组
- php面向对象(OOP)—final关键字
- linux 命令之 tar
- Java 文件复制 效率
- c++ 手写Makefile 简单实用的方法
- 第十四周 项目一:小玩文件(2)
- Android 强制横屏或竖屏注意事项及onConfigurationChanged的使用
- http、浏览器---工作原理讲解-----阿冬专栏!!!!
- 欢迎使用CSDN-markdown编辑器
- Navicat for Mysql 只导出数据表结构(.sql文件)的方法
- 图像滤镜特效(反色、浮雕、雕刻、怀旧、冰冻、暗调)(一)
- 123123