如何用JAVA实现根据文件后缀名分类文件,并且将文件复制到不同的文件夹  求完整代码!!谢谢大家了

解决方案 »

  1.   

    判断文件后缀名可以用File file = new File("路径");
            if (file.getName().toLowerCase().endsWith(".txt")) {
                //复制操作
            }
    文件复制操作如下    public static boolean copy(File fileFrom, File fileTo) {
            try {
                FileInputStream in = new java.io.FileInputStream(fileFrom);
                FileOutputStream out = new FileOutputStream(fileTo);
                byte[] bt = new byte[1024];
                int count;
                while ((count = in.read(bt)) > 0) {
                    out.write(bt, 0, count);
                }
                in.close();
                out.close();
                return true;
            } catch (IOException ex) {
                return false;
            }
        }
      

  2.   


    Path dir = ...;
    Path target = ...;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
       for (Path entry: stream) {
           Files.copy(entry,target.resolve(entry.getFileName()));
       }
    } catch (DirectoryIteratorException ex) {
       // I/O error encounted during the iteration, the cause is an IOException
       throw ex.getCause();
    }
    需要 jdk7
      

  3.   

    如果是word、excel,拷贝是不是会复杂些?