/** * Copy method to copy a file to another file * * @param sourceFile * @param targetFile * @param move true: move file; false: copy file * @return true: success; false: failure */ public static boolean copyFileToFile(File sourceFile, File targetFile, boolean move) { try { if (sourceFile.isDirectory() || targetFile.isDirectory()) { return false; } // create target directories targetFile.getParentFile().mkdirs(); // don't check for success... would return false on // catch move/copy of "same" file -> buggy under Windows. if (sourceFile.getCanonicalPath().equals(targetFile.getCanonicalPath())) return true; if (move) { // try to rename it first - operation might only be successful on a local filesystem! if (sourceFile.renameTo(targetFile)) return true; // it failed, so continue with copy code! } bcopy(sourceFile, targetFile, "copyFileToFile"); if (move) { // to finish the move accross different filesystems we need to delete the source file sourceFile.delete(); } } catch (IOException e) { log.error( "Could not copy file::" + sourceFile.getAbsolutePath() + " to file::" + targetFile.getAbsolutePath(), e); return false; } return true; } // end copy
/** * @param target * @param data * @param encoding */ public static void save(OutputStream target, String data, String encoding) { try { byte[] ba = data.getBytes(StringHelper.check4xMacRoman(encoding)); ByteArrayInputStream bis = new ByteArrayInputStream(ba); bcopy(bis, target, "saveDataToFile"); } catch (IOException e) { throw new OLATRuntimeException(FileUtils.class, "could not save to output stream", e); } }
/** * Copy a file from one spot on hard disk to another. Will create any target dirs if necessary. * * @param sourceFile file to copy on local hard disk. * @param targetDir new file to be created on local hard disk. * @param move * @param filter file filter or NULL if no filter applied * @return true if the copy was successful. */ public static boolean copyFileToDir( File sourceFile, File targetDir, boolean move, FileFilter filter, String wt) { try { // copy only if filter allows. filtered items are considered a success // and not a failure of the operation if (filter != null && !filter.accept(sourceFile)) return true; // catch if source is directory by accident if (sourceFile.isDirectory()) { return copyDirToDir(sourceFile, targetDir, move, filter, wt); } // create target directories targetDir.mkdirs(); // don't check for success... would return false on // existing dirs if (!targetDir.isDirectory()) return false; File targetFile = new File(targetDir, sourceFile.getName()); // catch move/copy of "same" file -> buggy under Windows. if (sourceFile.getCanonicalPath().equals(targetFile.getCanonicalPath())) return true; if (move) { // try to rename it first - operation might only be successful on a local filesystem! if (sourceFile.renameTo(targetFile)) return true; // it failed, so continue with copy code! } bcopy(sourceFile, targetFile, "copyFileToDir:" + wt); if (move) { // to finish the move accross different filesystems we need to delete the source file sourceFile.delete(); } } catch (IOException e) { log.error( "Could not copy file::" + sourceFile.getAbsolutePath() + " to dir::" + targetDir.getAbsolutePath(), e); return false; } return true; } // end copy
public static void bcopy(InputStream src, File dst, String wt) throws FileNotFoundException, IOException { bcopy(src, new FileOutputStream(dst), "copyStreamToFile:" + wt); }
public static void bcopy(File src, File dst, String wt) throws FileNotFoundException, IOException { bcopy(new FileInputStream(src), new FileOutputStream(dst), wt); }