关于文件拷贝你会了吗
来源:互联网 发布:页面间如何传递数据 编辑:程序博客网 时间:2024/04/27 21:58
在真正的编程中,很多时候我们会遇到一些关于文件拷贝的问题!
下面是自己写的一个实例,希望对各位有用!!!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Date;
public class copyFile {
public static void main(String[] args) {
String path = "d://src";
String tow = "d://copy//";
File f = new File(path);
File copy = new File(tow);
copyFile c = new copyFile();
File cf = c.testFile(f);
try {
long time = forTransfer(cf, copy);
System.out.println("拷贝文件花费时间:"+time+"毫秒");
} catch (Exception e) {
System.out.println("拷贝出错了");
}
}
public File testFile(File dir) {
if (dir.exists()) {
if (dir.isDirectory()) {
System.out.println("目录已经存在无需创建");
}
} else {
try {
dir.mkdirs();
System.out.println("目录创建成功");
} catch (Exception ef) {
System.out.println("目录创建出错");
}
}
File f = new File(dir.getPath(), "file.txt");
if (f.exists()) {
System.out.println("文件已经存在" + f.getAbsolutePath());
} else {
try {
f.createNewFile();
RandomAccessFile raf = new RandomAccessFile(dir.getPath()
+ "//" + f.getName(), "rw");
raf.write("拷贝我吧,给你自由".getBytes());
raf.close();
System.out.println("需要拷贝的文件创建成功:" + f.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
return f;
}
public static long forTransfer(File f1, File f2) throws Exception {
String Name = null;
String path = f1.getPath();
System.out.println("源文件所在位置:" + path);
// 获得需要拷贝的文件的名称
int a = path.lastIndexOf("//");
if ((a > 0) && (a < (path.length() - 1)))
Name = path.substring(a + 1);
String fileName = "Copy"+Name;
System.out.println("您当前拷贝的文件:" + fileName);
System.out.println(f2.getAbsolutePath()+"kankna");
File f = new File(f2, fileName);
if (!f2.exists()){
try {
f2.mkdirs();
} catch (Exception ef) {
System.out.println("目录创建出错");
}
}
if (f.exists()) {
System.out.println("文件已经存在:" + f.getAbsolutePath());
} else {
try {
f.createNewFile();
System.out.println("文件拷贝成功:"+f.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
long time = new Date().getTime();
int length = 2097152;
FileInputStream in = new FileInputStream(f1);
FileOutputStream out = new FileOutputStream(f);
FileChannel inC = in.getChannel();
FileChannel outC = out.getChannel();
int i = 0;
while (true) {
if (inC.position() == inC.size()) {
inC.close();
outC.close();
return new Date().getTime() - time;
}
if ((inC.size() - inC.position()) < 20971520)
length = (int) (inC.size() - inC.position());
else
length = 20971520;
inC.transferTo(inC.position(), length, outC);
inC.position(inC.position() + length);
i++;
}
}
}
- 关于文件拷贝你会了吗
- 这道题你会了吗?
- 你真的会写深度拷贝吗?
- 关于Eclipse快捷键操作会了这些你就是大神!
- DataGrid+CheckBox组合》你会了吗?
- DataGrid/DataList, 你会用了吗?
- DataGrid/DataList, 你会用了吗?
- DataGrid/DataList, 你会用了吗?
- DataGrid/DataList,你会用了吗?
- VS2005 打印你会了吗 ?
- 项目经理,这些你会了吗?
- typename 你会用了吗?
- DataGrid/DataList,你会用了吗?
- 你真的会学习了吗?
- 委托事件你会用了吗?
- jquery ajax你会了吗?
- 研究生了,你会查文献吗?
- Apache 重写你会了吗?
- 每周一总结(8)、(9)
- 桌面助手特价机票软件更新公告
- 数据导入浅析之---SqlBulkCopy(转载)
- WinCE控制面板添加应用程序
- struts2上传文件
- 关于文件拷贝你会了吗
- mysql数据库到oracle数据库的迁移(mysql为gbk编码,oracle为US7ASCII 编码)
- 简单学生管理系统中用到的一些知识点
- IStock 设计
- 一点一点学ASP.NET之基础概念——HttpModule (二)
- VS2008在资源管理器中按右键添加ASP文件夹时没有App_Code选项,自行新增后放在里面的类文件不能调用!
- MFC Dialog 屏蔽Enter和Esc按下时自动退出程序
- VI学习笔记
- php遇到乱码问题