通过JFileChooser完成文件的拷贝
来源:互联网 发布:自媒体发展的数据 编辑:程序博客网 时间:2024/06/05 19:30
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Iterator;
/**
* 拷贝文件
* @author martin
*/
public class CopyFile {
/**
* 完成文件的拷贝
* @param source 源文件 E:/lyc/ITMS_H_0407/record/2010/4/10/0217940.wav
* @param destination 目标文件 E:/lyc/ITMS_H_0407/record/2010/4/9/0217940.wav
* @throws IOException
*/
public static void fileCopy(HashMap<Integer, String> hashMap, String dest) throws IOException{
Iterator<Integer> iterator = hashMap.keySet().iterator();
while(iterator.hasNext()) {
String source = hashMap.get(iterator.next());
String destination = dest;
if(source != null) {
destination += "/" + new File(source).getName();
// Create channel on the source
FileChannel srcChannel = new FileInputStream(source).getChannel();
// Create channel on the destination
FileChannel dstChannel = new FileOutputStream(destination).getChannel();
// Copy file contents from source to destination
//dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
srcChannel.transferTo(0, srcChannel.size(), dstChannel);
// Close the channels
srcChannel.close();
dstChannel.close();
}
}
}
——————————————————下面为调用部分(Start)————————————————
/**
* 执行备份
* @param evt
*/
private void btnBackupActionPerformed(ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser(); //实例化一个文件选择器
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //只能选择目录
/*fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //只能选择文件*/
fileChooser.showOpenDialog(this);
try{
if(fileChooser.getSelectedFile() != null) { //获取选择的目录或文件
CopyFile.fileCopy(hashMap, fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "完毕,共计"+hashMap.size()+"个文件);
}
} catch(IOException e) {
JOptionPane.showMessageDialog(null, "失败!", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
——————————————————上面为调用部分(End)————————————————
- 通过JFileChooser完成文件的拷贝
- 通过JFileChooser完成导出文件(.xls)
- JFileChooser文件过滤的实现
- JFileChooser文件过滤的实现
- JFileChooser文件过滤的实现
- 文件选择器JFileChooser的创建
- 通过反射完成不同对象之间的属性拷贝
- 通过父子进程完成文件io对文件的拷贝,父进程从文件开始到文件 的一半开始拷贝,子进程从文件的一半到文件末尾。
- JFileChooser文件的选择与保存
- JFileChooser(文件选择器)的使用
- JFileChooser文件的选择与保存
- 文件选择框JFileChooser的使用
- JFileChooser文件的选择与保存
- JFileChooser所用到的文件过滤器
- 使用IO完成文件拷贝
- 完成内存之间的拷贝
- JAVA 文件对话框JFileChooser
- JFileChooser文件过滤
- 余丰慧:谁是人民币对内贬值的受害者
- 看似简单!解读C#程序员最易犯的7大错误
- ...
- C#中对"Cross-thread operation not valid"错误的权宜之计
- [Flash/Flex] 图片显示效果:下落
- 通过JFileChooser完成文件的拷贝
- 解决VC++与VS冲突造成具体做法如下:(英文原文文章和FileTool.exe都在附件里了)无法打开文件和无法向工程中添加文件的问题
- UltraEdit 快捷键操作
- 编写一个程序,打印输入中单词长度的直方图。水平方向的直方图比较容易
- c++对齐方式总结(上)
- wsprintf不支持%f的解决方法
- 简单的界面换肤术----Swing
- 网站设计的全过程12步
- with check option