zoukankan      html  css  js  c++  java
  • Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)

    控制台程序,创建和删除目录以及复制和移动文件。

      1 import java.nio.file.*;
      2 import java.nio.file.attribute.*;
      3 import java.io.IOException;
      4 
      5 public class MoveAndCopyFiles {
      6     static void createSingleDirectory(Path path){
      7         try{
      8             Files.createDirectories(path);
      9             System.out.println("
    " + path + " directory created.");
     10         } catch (IOException e) {
     11             System.out.println("
    Directory creation failed:
    " + e);
     12         }
     13     }
     14 
     15     static boolean isDirectory(Path path) {
     16         try{
     17             BasicFileAttributes attr =Files.readAttributes(path,BasicFileAttributes.class);
     18             return attr.isDirectory();
     19         } catch (IOException e) {
     20             System.err.println("I/O error in isDirectory method. " + e);
     21         }
     22         return false;
     23     }
     24 
     25     static boolean copyFiles(Path from, Path to) {
     26         if(!isDirectory(from)) {
     27             System.out.println("Cannot copy files. " + from + " is not a directory.");
     28             return false;
     29         }
     30         if(!isDirectory(to)) {
     31             System.out.println("Cannot copy files. " + to + " is not a directory.");
     32             return false;
     33         }
     34         
     35         try (DirectoryStream<Path> files =Files.newDirectoryStream(from, "*.*")) {
     36             System.out.println("Starting copy...");
     37             for(Path file : files) {
     38                 Files.copy(file, to.resolve(file.getFileName()));
     39                 System.out.println("  " + file.getFileName() + " copied.");
     40             }
     41         } catch (IOException e) {
     42             System.err.println("I/O error in copyFiles. " + e);
     43             return false;
     44         }
     45         return true;
     46     }
     47 
     48     static boolean moveFiles(Path from, Path to) {
     49         if(!isDirectory(from)) {
     50             System.out.println("Cannot move files. " + from + " is not a directory.");
     51             return false;
     52         }
     53         if(!isDirectory(to)) {
     54             System.out.println("Cannot move files. " + to + " is not a directory.");
     55             return false;
     56         }
     57         
     58         try (DirectoryStream<Path> files =Files.newDirectoryStream(from, "*.*")) {
     59             System.out.println("Starting move...");
     60             for(Path file : files) {
     61                 Files.move(file, to.resolve(file.getFileName()));
     62                 System.out.println("  " + file.getFileName() + " moved.");
     63             }
     64         } catch (IOException e) {
     65             System.err.println("I/O error in copyFiles. " + e);
     66             return false;
     67         }
     68         return true;
     69     }
     70 
     71     static void waitForEnter() {
     72         try {
     73             System.out.println("waiting...");
     74             System.in.read();
     75         }catch(IOException e) {
     76             System.err.println(e);
     77         }
     78     }
     79 
     80     public static void main(String[] args) {
     81         Path current =Paths.get("E:/JavaProject/BeginningJava/Ch9_Directories/MoveAndCopyFiles");
     82         Path newDir =Paths.get("junkDir");
     83         newDir=newDir.toAbsolutePath();
     84         createSingleDirectory(newDir);
     85         
     86         System.out.println("Copying files from " + current + " to " + newDir);
     87         if(!copyFiles(current,newDir)) {
     88             System.out.println("Copying files failed.");
     89             return;
     90         }
     91         System.out.println("You can look at the directory to verify that the copy has worked.");
     92         System.out.println("Press Enter to continue.");
     93         waitForEnter();
     94         
     95         Path newDir2 =Paths.get("junkDirBackup");
     96         newDir2=newDir2.toAbsolutePath();
     97         createSingleDirectory(newDir2);
     98         
     99         System.out.println("Moving files from " + newDir + " to " + newDir2);
    100         if(!moveFiles(newDir,newDir2)) {
    101             System.out.println("Moving files failed.");
    102             return;
    103         }
    104         
    105         System.out.println("You can look at the directory to verify that the move has worked.");
    106         System.out.println("Press Enter to continue.");
    107         waitForEnter();
    108         
    109         try {
    110             System.out.println("Deleting " + newDir + "...");
    111             Files.delete(newDir);
    112         }catch(IOException e) {
    113             System.err.println("Deleting " + newDir + " failed:
    " + e);
    114         }
    115         
    116         try (DirectoryStream<Path> files =Files.newDirectoryStream(newDir2, "*.*")) {
    117             System.out.println("Deleting files from " + newDir2 + "...");
    118             for(Path file:files) {
    119                 Files.delete(file);
    120                 System.out.println(" " + file.getFileName() + " deleted.");
    121             }
    122             System.out.println("Deleting " + newDir2 + "...");
    123             Files.delete(newDir2);
    124         } catch (IOException e) {
    125             System.err.println("I/O error deleting files. " + e);
    126         }
    127     }
    128 }
  • 相关阅读:
    与灵感之源的vb.net对应的SmartExcel的C#版本
    winform下提高control在UI中的响应速度
    做了一个petoolkit的gui版本,放几张图上来。
    Reflection中对于out类型的获取
    对企业管理软件“代码后”问题的思考
    看了dannyr的java中文类,自己写了一个。
    所有的WMI可以使用的class
    庆祝浪潮集团成为微软在中国的第四家全球战略合作伙伴!(暂时放首页两天)
    一种系统间集成的同步事务异常处理方法和装置
    一种实现多线并行拣选的方法
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3386708.html
Copyright © 2011-2022 走看看