TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
copy目录下的所有的文件和子目录到另一个目录- import java.io.*;
- public class CopyDirectory{
- public static void main(String[] args) throws IOException{
- CopyDirectory cd = new CopyDirectory();
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter the source directory or file name : ");
- String source = in.readLine();
- File src = new File(source);
- System.out.println("Enter the destination directory or file name : ");
- String destination = in.readLine();
- File dst = new File(destination);
- cd.copyDirectory(src, dst);
- }
- public void copyDirectory(File srcPath, File dstPath) throws IOException{
- if (srcPath.isDirectory()){
- if (!dstPath.exists()){
- dstPath.mkdir();
- }
- String files[] = srcPath.list();
- for(int i = 0; i < files.length; i++){
- copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
- }
- } else{
- if(!srcPath.exists()){
- System.out.println("File or directory does not exist.");
- System.exit(0);
- }else{
- InputStream in = new FileInputStream(srcPath);
- OutputStream out = new FileOutputStream(dstPath);// Transfer bytes from in to out
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- in.close();
- out.close();
- }
- }
- System.out.println(srcPath.toString()+" copied.");
- }
- }
- 运行:
- C: est>java CopyDirectory
- Enter the source directory or file name :
- c:work
- Enter the destination directory or file name :
- c: estwork1
- c:work123Node.class copied.
- c:work123 copied.
- c:workBMTest.class copied.
- c:workBMTest.java copied.
- c:workKMP.class copied.
- c:workKMP.java copied.
- c:workKMP_Next.class copied.
- c:workKMP_Next.java copied.
- c:workMain.class copied.
- c:workMain.java copied.
- c:workPM_BM.class copied.
- c:workPM_BM.java copied.
- c:workPM_BruteForce.class copied.
- c:workPM_BruteForce.java copied.
- c:workPM_KMP.class copied.
- c:work片段.shs copied.
- c:work copied.
复制代码
源码下载:http://file.javaxxz.com/2014/11/4/235837343.zip |
|