您的位置:首页 > 编程语言 > Java开发

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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: