java和剪切板 实现多层目录的复制和删除
2012-04-17 15:53
309 查看
关于剪切板上一节已讲过,这节重点介绍文件的操作。
public class FileOperUtils {
private static final String FILE_COPY = "copy";
private static final String FILE_CUT = "cut";
private Clipboard clipboard = Toolkit.getDefaultToolkit()
.getSystemClipboard();
private List<MyFile> dir = new ArrayList<FileOperUtils.MyFile>();
private String copyFilename;
private static String oper = "";
// private List<String> paths = new ArrayList<String>();
public FileOperUtils() {
super();
}
/*
* 文件的复制
*/
public void fileCopy(String m_copyDir) {
Transferable dir = new StringSelection(m_copyDir);
clipboard.setContents(dir, null);
oper = this.FILE_COPY;
}
/*
* 文件的剪切
*/
public void fileCut(String m_cutDir) {
Transferable dir = new StringSelection(m_cutDir);
clipboard.setContents(dir, null);
oper = this.FILE_CUT;
//deleteFileOrdirectory(m_cutDir);
}
/*
* 文件的粘贴
*/
public void filePaste(String to_pasteDir) {
try {
String path = this.getPathFromClipboard(clipboard);
copyFilename = path.substring(path.lastIndexOf("/"), path.length());
System.out.println("copyFilename " + copyFilename);
File file = new File(path);
if (file != null && file.exists()) {
if (file.isDirectory()) {
findAllFilePaths(file);
pasteDirToDir(file.getAbsolutePath(), to_pasteDir);
} else {
pasteFileToDir(path, to_pasteDir);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 从剪切板获得路径
*/
private String getPathFromClipboard(Clipboard clip) throws Exception,
IOException {
Transferable mclip = clip.getContents(null);
if (mclip != null) {
return (String) mclip.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
/*
* 将整个文件夹复制到指定路径下
*/
private void pasteDirToDir(String dirPath, String to_pasteDir) {
for (int i = dir.size()-1; i >=0; i--) {
MyFile myfile = dir.get(i);
String newpath = to_pasteDir+myfile.path.substring(dir.get(0).path.length(),myfile.path.length());
File file = new File(newpath);
if (!file.exists()) {
file.mkdir();
}
for (int j = 0; j < myfile.subFilePath.size(); j++) {
String str_path = myfile.subFilePath.get(j);
System.out.println(str_path+"--------------");
pasteFileToDir(str_path, to_pasteDir);
}
if (this.FILE_CUT.equals(oper)) {
File mf = new File(dir.get(i).path);
if (mf.isDirectory()) {
mf.delete();
}
}
}
}
/*
* 把文件复制到制定的路径
*/
private void pasteFileToDir(String fromPath, String toPath) {
int byteread = 0;
File newFile = new File(toPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
File oldfile = new File(fromPath);
String ss = toPath+fromPath.substring(dir.get(0).path.lastIndexOf("/"),fromPath.length());
File c_file= new File(ss.substring(0,ss.lastIndexOf("/")));
if (!c_file.exists()) {
c_file.mkdirs();
}
System.out.println(ss+"======");
if (oldfile.exists()) {
InputStream is;
try {
is = new FileInputStream(oldfile);
FileOutputStream fs = new FileOutputStream(new File(ss));
byte[] buffer = new byte[2048];
while ((byteread = is.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
is.close();
if (this.FILE_CUT.equals(oper)) {
oldfile.delete();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 找出当前文件夹下所有文件的路径,
*/
private void findAllFilePaths(File m_file) {
if (m_file != null) {
MyFile myfile = new MyFile();
myfile.path = m_file.getAbsolutePath();
dir.add(myfile);
File[] files = m_file.listFiles();
for (int i = 0; i < files.length; i++) {
File newfile = files[i];
if (newfile.isFile()) {
myfile.subFilePath.add(newfile.getAbsolutePath());
} else if (newfile.isDirectory()) {
findAllFilePaths(newfile);
}
}
}
}
/*
* 删除指定路径下的文件
*/
private void deleteFileOrdirectory(String dir) {
File file = new File(dir);
if (file.isFile() && file.exists()) {
file.delete();
} else if (file.isDirectory() && file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File file_s = files[i];
deleteFileOrdirectory(file_s.getAbsolutePath());
}
}
}
class MyFile {
private String path;
private List<String> subFilePath = new ArrayList<String>();
public MyFile() {
super();
}
public MyFile(String path, List<String> subFilePath) {
super();
this.path = path;
this.subFilePath = subFilePath;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public List<String> getSubFilePath() {
return subFilePath;
}
public void setSubFilePath(List<String> subFilePath) {
this.subFilePath = subFilePath;
}
}
}
测试类:
public class FileTest {
static String p = "/home/wangjia/test/test1/wj";
static String path = "/home/wangjia/test";
static String todir = "/home/wangjia/test1";
static String dir = "/home/wangjia/qq";
public static void main(String[] args) {
// System.out.println(path.substring(path.lastIndexOf("/"),path.length()));
//System.out.println(path.indexOf(p.length()));
// System.out.println(path.substring(p.length(),path.length()));
//System.out.println(dir+ path.substring(path.indexOf("/test1"), path.length()));
FileOperUtils utils = new FileOperUtils();
//utils.fileCopy(todir);
utils.fileCut(todir);
utils.filePaste(path);
}
}
public class FileOperUtils {
private static final String FILE_COPY = "copy";
private static final String FILE_CUT = "cut";
private Clipboard clipboard = Toolkit.getDefaultToolkit()
.getSystemClipboard();
private List<MyFile> dir = new ArrayList<FileOperUtils.MyFile>();
private String copyFilename;
private static String oper = "";
// private List<String> paths = new ArrayList<String>();
public FileOperUtils() {
super();
}
/*
* 文件的复制
*/
public void fileCopy(String m_copyDir) {
Transferable dir = new StringSelection(m_copyDir);
clipboard.setContents(dir, null);
oper = this.FILE_COPY;
}
/*
* 文件的剪切
*/
public void fileCut(String m_cutDir) {
Transferable dir = new StringSelection(m_cutDir);
clipboard.setContents(dir, null);
oper = this.FILE_CUT;
//deleteFileOrdirectory(m_cutDir);
}
/*
* 文件的粘贴
*/
public void filePaste(String to_pasteDir) {
try {
String path = this.getPathFromClipboard(clipboard);
copyFilename = path.substring(path.lastIndexOf("/"), path.length());
System.out.println("copyFilename " + copyFilename);
File file = new File(path);
if (file != null && file.exists()) {
if (file.isDirectory()) {
findAllFilePaths(file);
pasteDirToDir(file.getAbsolutePath(), to_pasteDir);
} else {
pasteFileToDir(path, to_pasteDir);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 从剪切板获得路径
*/
private String getPathFromClipboard(Clipboard clip) throws Exception,
IOException {
Transferable mclip = clip.getContents(null);
if (mclip != null) {
return (String) mclip.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
/*
* 将整个文件夹复制到指定路径下
*/
private void pasteDirToDir(String dirPath, String to_pasteDir) {
for (int i = dir.size()-1; i >=0; i--) {
MyFile myfile = dir.get(i);
String newpath = to_pasteDir+myfile.path.substring(dir.get(0).path.length(),myfile.path.length());
File file = new File(newpath);
if (!file.exists()) {
file.mkdir();
}
for (int j = 0; j < myfile.subFilePath.size(); j++) {
String str_path = myfile.subFilePath.get(j);
System.out.println(str_path+"--------------");
pasteFileToDir(str_path, to_pasteDir);
}
if (this.FILE_CUT.equals(oper)) {
File mf = new File(dir.get(i).path);
if (mf.isDirectory()) {
mf.delete();
}
}
}
}
/*
* 把文件复制到制定的路径
*/
private void pasteFileToDir(String fromPath, String toPath) {
int byteread = 0;
File newFile = new File(toPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
File oldfile = new File(fromPath);
String ss = toPath+fromPath.substring(dir.get(0).path.lastIndexOf("/"),fromPath.length());
File c_file= new File(ss.substring(0,ss.lastIndexOf("/")));
if (!c_file.exists()) {
c_file.mkdirs();
}
System.out.println(ss+"======");
if (oldfile.exists()) {
InputStream is;
try {
is = new FileInputStream(oldfile);
FileOutputStream fs = new FileOutputStream(new File(ss));
byte[] buffer = new byte[2048];
while ((byteread = is.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
is.close();
if (this.FILE_CUT.equals(oper)) {
oldfile.delete();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 找出当前文件夹下所有文件的路径,
*/
private void findAllFilePaths(File m_file) {
if (m_file != null) {
MyFile myfile = new MyFile();
myfile.path = m_file.getAbsolutePath();
dir.add(myfile);
File[] files = m_file.listFiles();
for (int i = 0; i < files.length; i++) {
File newfile = files[i];
if (newfile.isFile()) {
myfile.subFilePath.add(newfile.getAbsolutePath());
} else if (newfile.isDirectory()) {
findAllFilePaths(newfile);
}
}
}
}
/*
* 删除指定路径下的文件
*/
private void deleteFileOrdirectory(String dir) {
File file = new File(dir);
if (file.isFile() && file.exists()) {
file.delete();
} else if (file.isDirectory() && file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File file_s = files[i];
deleteFileOrdirectory(file_s.getAbsolutePath());
}
}
}
class MyFile {
private String path;
private List<String> subFilePath = new ArrayList<String>();
public MyFile() {
super();
}
public MyFile(String path, List<String> subFilePath) {
super();
this.path = path;
this.subFilePath = subFilePath;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public List<String> getSubFilePath() {
return subFilePath;
}
public void setSubFilePath(List<String> subFilePath) {
this.subFilePath = subFilePath;
}
}
}
测试类:
public class FileTest {
static String p = "/home/wangjia/test/test1/wj";
static String path = "/home/wangjia/test";
static String todir = "/home/wangjia/test1";
static String dir = "/home/wangjia/qq";
public static void main(String[] args) {
// System.out.println(path.substring(path.lastIndexOf("/"),path.length()));
//System.out.println(path.indexOf(p.length()));
// System.out.println(path.substring(p.length(),path.length()));
//System.out.println(dir+ path.substring(path.indexOf("/test1"), path.length()));
FileOperUtils utils = new FileOperUtils();
//utils.fileCopy(todir);
utils.fileCut(todir);
utils.filePaste(path);
}
}
相关文章推荐
- 使用java实现dos窗口文件操作命令(复制,删除,列出文件目录)
- Java笔记之io——递归创建与删除多层文件夹,字符流实现文件复制
- java实现ftp连接、登陆、上传、下载、删除文件、获取目录、文件列表
- 【转载】Java实现文件夹的移动、内容复制、删除代码
- java实现递归删除非空目录
- java图片上传后台实现以及删除时删除目录
- java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息
- Java - 遍厉目录,实现目录删除,取得目录大小及深度
- java 实现 文件操作工具集。包括文件、目录树的拷贝、删除、移动、查找等工具函数
- JAVA 如何创建/删除/修改/复制目录及文件
- Java实现将目录下指定类型文件全部删除_FileFilter文件过滤
- java 流 复制,重命名,删除目录
- Java非递归实现删除任意目录的方法
- JAVA 如何创建/删除/修改/复制目录及文件 (转载)
- java单个文件复制,递归删除、复制目录下所有内容,递归创建文件夹,.7z文件解压等
- Java实现文件夹的复制、移动、删除代码
- JAVA 如何创建/删除/修改/复制目录及文件
- java怎么创建目录(删除/修改/复制目录及文件)代码实例
- java删除文件夹 Java中实现复制文件或文件夹
- Java实现文件或文件夹的复制到指定目录实例