/** * Copy a file. Overwrites the destination file if it exists. * @param fromFileName * @param toFileName * @throws Exception * @since 4.3 */ public static void copy(String fromFileName, String toFileName) throws IOException { copy(fromFileName, toFileName, true); }
copyRecursively(srcFile, childTargetDir, true); } else { copy(srcFile.getAbsolutePath(), targetDir.getAbsolutePath() + File.separatorChar + srcFile.getName());
/** * Copy a file. Overwrites the destination file if it exists. * @param fromFileName * @param toFileName * @throws Exception * @since 4.3 */ public static void copy(String fromFileName, String toFileName) throws IOException { copy(fromFileName, toFileName, true); }
copyRecursively(srcFile, childTargetDir, true); } else { copy(srcFile.getAbsolutePath(), targetDir.getAbsolutePath() + File.separatorChar + srcFile.getName());
/** * Copy file from orginating directory to the destination directory. * * @param orginDirectory * @param orginFileName * @param destDirectory * @param destFileName * @throws Exception * @since 4.4 */ public static void copyFile(String orginDirectory, String orginFileName, String destDirectory, String destFileName) throws Exception { FileUtils.copy(orginDirectory + File.separator + orginFileName, destDirectory + File.separator + destFileName); }
/** * Copy file from orginating directory to the destination directory. * * @param orginDirectory * @param orginFileName * @param destDirectory * @param destFileName * @throws Exception * @since 4.4 */ public static void copyFile(String orginDirectory, String orginFileName, String destDirectory, String destFileName) throws Exception { FileUtils.copy(orginDirectory + File.separator + orginFileName, destDirectory + File.separator + destFileName); }
/** * Rename a file. * @param oldFilePath * @param newFilePath * @param overwrite If true, overwrite the old file if it exists. If false, throw an exception if the old file exists. * @throws TeiidException * @since 4.3 */ public static void rename(String oldFilePath, String newFilePath, boolean overwrite) throws IOException { File oldFile = new File(oldFilePath); File newFile = new File(newFilePath); if (newFile.exists()) { if (overwrite) { newFile.delete(); } else { final String msg = CorePlugin.Util.getString("FileUtils.File_already_exists", newFilePath); //$NON-NLS-1$ throw new IOException(msg); } } boolean renamed = oldFile.renameTo(newFile); //Sometimes file.renameTo will silently fail, for example attempting to rename from different UNIX partitions. //Try to copy instead. if (!renamed) { copy(oldFilePath, newFilePath); oldFile.delete(); } }
/** * Rename a file. * @param oldFilePath * @param newFilePath * @param overwrite If true, overwrite the old file if it exists. If false, throw an exception if the old file exists. * @throws TeiidException * @since 4.3 */ public static void rename(String oldFilePath, String newFilePath, boolean overwrite) throws IOException { File oldFile = new File(oldFilePath); File newFile = new File(newFilePath); if (newFile.exists()) { if (overwrite) { newFile.delete(); } else { final String msg = CorePlugin.Util.getString("FileUtils.File_already_exists", newFilePath); //$NON-NLS-1$ throw new IOException(msg); } } boolean renamed = oldFile.renameTo(newFile); //Sometimes file.renameTo will silently fail, for example attempting to rename from different UNIX partitions. //Try to copy instead. if (!renamed) { copy(oldFilePath, newFilePath); oldFile.delete(); } }
/** * Tests FileUtils.remove() * @since 4.3 */ public void testRemove() throws Exception { FileUtils.copy(FILE_NAME, TEMP_FILE_NAME, true); //positive case FileUtils.remove(TEMP_FILE_NAME); assertFalse("Expected File to not exist", new File(TEMP_FILE_NAME).exists()); //$NON-NLS-1$ //call again - this should not throw an exception FileUtils.remove(TEMP_FILE_NAME); }
FileUtils.copy(FILE_NAME, TEMP_FILE_NAME, true); FileUtils.rename(TEMP_FILE_NAME, TEMP_FILE_NAME2, false); String contents2 = ObjectConverterUtil.convertFileToString(new File(TEMP_FILE_NAME2)); FileUtils.copy(FILE_NAME, TEMP_FILE_NAME, true); FileUtils.copy(FILE_NAME, TEMP_FILE_NAME2, true); try { FileUtils.rename(FILE_NAME, TEMP_FILE_NAME2, false); FileUtils.copy(FILE_NAME, TEMP_FILE_NAME, true); FileUtils.copy(FILE_NAME, TEMP_FILE_NAME2, true); FileUtils.rename(TEMP_FILE_NAME, TEMP_FILE_NAME2, true); contents2 = ObjectConverterUtil.convertFileToString(new File(TEMP_FILE_NAME2));
/** * Tests FileUtils.copy() * @since 4.3 */ public void testCopy() throws Exception { String contents1 = ObjectConverterUtil.convertFileToString(new File(FILE_NAME)); //positive case FileUtils.copy(FILE_NAME, TEMP_FILE_NAME, false); String contents2 = ObjectConverterUtil.convertFileToString(new File(TEMP_FILE_NAME)); assertEquals("Expected file contents to be the same", contents1, contents2); //$NON-NLS-1$ assertTrue("Expected original file to still exist", new File(FILE_NAME).exists()); //$NON-NLS-1$ //negative case: should fail because file already exists try { FileUtils.copy(FILE_NAME, TEMP_FILE_NAME, false); fail("Expected MetaMatrixException"); //$NON-NLS-1$ } catch (IOException e) { } //positive case: should succeed because we've specified to overwrite FileUtils.copy(FILE_NAME, TEMP_FILE_NAME, true); contents2 = ObjectConverterUtil.convertFileToString(new File(TEMP_FILE_NAME)); assertEquals("Expected file contents to be the same", contents1, contents2); //$NON-NLS-1$ assertTrue("Expected original file to still exist", new File(FILE_NAME).exists()); //$NON-NLS-1$ }