1. 將指定目錄下的所有文件復制到指定目錄。 該方式只能獲取指定目錄中只有一個文件,且該文件在最底層的形式。
2. 文件復制,使用的是jdk1.7中的api。
3. 如果想獲取目錄下所有的文件, 可以定義一個map, 遍歷目錄,判斷File是文件時,將其添加到map中, 最后在對其操作。
public static void main(String[] args) throws IOException { String rootDir = "E://tmp//video//videos"; String destDir = "E://tmp//video//videosall//"; Path source = null; Path target = null; File rootFile = new File(rootDir); File[] files = rootFile.listFiles(); StringBuilder sb = new StringBuilder(); File retFile = null; for (File file : files) { retFile = getVideoFile(file, sb);//執行完成后, retFile為當前目錄下的最底層文件, sb為該文件的路徑 source = Paths.get(rootDir + sb.toString()); target = Paths.get(destDir + retFile.getName()); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); // 復制文件到指定目錄, 文件已存在則覆蓋原有文件 sb.setLength(0); // 清空目錄記錄 } } /** * * @desc: 獲取目標文件中最底層的文件,及其路徑, 只能用于目標目錄中只有一個文件的情況 * @auth: zona * 2017年2月18日 上午10:35:13 * @param filee * @param sb * @return */ public static File getVideoFile(File filee, StringBuilder sb) { if(filee.isDirectory()) { sb.append("/"+filee.getName()); File[] rootFiles = filee.listFiles(); for (File file : rootFiles) { // TODO 這里需要return, 但是還不知道原因 return getVideoFile(file, sb); } }else { sb.append("/"+filee.getName()); } return filee; }
新聞熱點
疑難解答