Javaで、あるディレクトリ配下のフォルダとファイルを再帰的にコピーしたいと考えています。
以下のようにソースを書いてみたのですが、ファイルはコピーされるのですが、フォルダがコピーされません。
(eachFile(変数)+"作成に失敗しました。が出力される)
mkdirs()の処理にて失敗してるようですが原因がわかりません。
予想できる方、ソースの誤りにお気づきの方いらっしゃいましたらご指摘ください。
以下ソース↓
public int copyFiles(String srcDir, String dstDir) throws IOException {
int result = 0; File file = new File(srcDir); for (File eachFile : file.listFiles()) { if (eachFile.isFile()) { copyTransfer(eachFile.getAbsolutePath(), dstDir + "\\" + eachFile.getName()); result += 1; }else{ if(eachFile.mkdirs()){ copyFiles(eachFile.toString(),dstDir+"\\"+eachFile.getName()); result += 1; }else{ System.out.println(eachFile+"作成に失敗しました。"); } } } return result; }
public static void copyTransfer(String srcPath, String destPath)
throws IOException {
FileChannel srcChannel = new FileInputStream(srcPath).getChannel(); FileChannel destChannel = new FileOutputStream(destPath).getChannel(); try { srcChannel.transferTo(0, srcChannel.size(), destChannel); } finally { srcChannel.close(); destChannel.close(); } }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/01 04:53