AdeFileUtils { public static void copyFile(File source, File target) throws AdeUsageException { final String closeFail = " failed to close."; FileInputStream fisSource = null; FileOutputStream fosTarget = null; FileChannel inputChannel = null; FileChannel outputChannel = null; try { fisSource = new FileInputStream(source); fosTarget = new FileOutputStream(target); inputChannel = fisSource.getChannel(); outputChannel = fosTarget.getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); inputChannel.close(); inputChannel = null; outputChannel.close(); outputChannel = null; fisSource.close(); fisSource = null; fosTarget.close(); fosTarget = null; } catch (IOException e) { throw new AdeUsageException("Copy file utility error", e); } finally { if (inputChannel != null) { try { inputChannel.close(); } catch (IOException e1) { logger.warn(inputChannel.toString() + closeFail, e1); } } if (outputChannel != null) { try { outputChannel.close(); } catch (IOException e1) { logger.warn(outputChannel.toString() + closeFail, e1); } } if (fisSource != null) { try { fisSource.close(); } catch (IOException e1) { logger.warn(fisSource.toString() + closeFail, e1); } } if (fosTarget != null) { try { fosTarget.close(); } catch (IOException e1) { logger.warn(fosTarget.toString() + closeFail, e1); } } } } private  AdeFileUtils(); static void copyFile(File source, File target); static BufferedReader openLogFile(File file); static InputStream openLogFileAsInputStream(File file); static void deleteFile(File file); static void deleteFileOrLog(File file); static void createDir(File target); static void createDirs(File target); static boolean deleteRecursiveOrLog(File path);  }
@Test public void testCopyFile() throws IOException, AdeUsageException { final File targetFile = temporaryFolder.newFile(); AdeFileUtils.copyFile(testFile, targetFile); byte[] f1 = Files.readAllBytes(testFile.toPath()); byte[] f2 = Files.readAllBytes(targetFile.toPath()); assertTrue("The target file should be exactly the same as the source.", Arrays.equals(f1, f2)); }
@Test(expected = AdeUsageException.class) public void testCopyFileNoWriteAccess() throws IOException, AdeUsageException { final File targetFile = temporaryFolder.newFile(); assertTrue("Make sure we can set the file to read only.", targetFile.setReadOnly()); AdeFileUtils.copyFile(testFile, targetFile); }