Java递归实现操作系统文件的复制、粘贴和删除功能

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

通过javaIO递归实现操作系统对文件的复制、粘贴和删除功能,剪切=复制+粘贴+删除

代码示例:

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;/**

* achieve file copy and delete using recursion

*

* @author ForeverLover

**/publicclassFileCopyAndDelete {//delete files of this pathpublicvoiddeleteFile(String path) {

File f=newFile(path);if(f.isDirectory()) {

File[] file=f.listFiles();for(File file1 : file) {this.deleteFile(file1.toString());

file1.delete();

}

}else{

f.delete();

}

f.delete();

}//copy files from path1 to path2publicvoidcopyFiles(String path1, String path2) throws IOException {

File f=newFile(path1);if(f.isDirectory()) {

File file=newFile(path2);if(!file.exists())

file.mkdir();

File[] file1=f.listFiles();for(File file2 : file1) {

copyFiles(file2.toString(), path2+"/"+file2.getName());

}

}else{

copy(path1, path2);

}

}//copy file from path1 to path2 one by onepublicvoidcopy(String path1, String path2) throws IOException {

DataInputStreamin=newDataInputStream(newBufferedInputStream(newFileInputStream(path1)));byte[] b =newbyte[in.available()];//available返回实际可读字节数,即总大小in.read(b);

DataOutputStreamout=newDataOutputStream(newBufferedOutputStream(newFileOutputStream(path2)));out.write(b);in.close();out.close();

}//main methodpublicstaticvoidmain(String[] args) {

FileCopyAndDelete f=newFileCopyAndDelete();//test copy files using recursive/** { String path1 = "D://Folder1"; String path2 = "D://Folder2"; try {

* f.copyFiles(path1, path2); System.out.PRintln("OK,COPY FINISH"); }

* catch (IOException e) { e.printStackTrace(); } }*///test delete files using recursive/** { f.deleteFile("C://Folder1");

* System.out.println("OK,DELETE FINISH"); }*/}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航