List of usage examples for java.io File renameTo
public boolean renameTo(File dest)
From source file:org.shept.util.FileUtils.java
/** * Compare the source path and the destination path for filenames with the filePattern * e.g. *.bak, *tmp and copy these files to the destination dir if they are not present at the * destination or if they have changed (by modifactionDate) * /*www .j av a 2 s. co m*/ * @param destPath * @param sourcePath * @param filePattern * @return the number of files being copied */ public static Integer syncAdd(String sourcePath, String destPath, String filePattern) { // check for new files since the last check which need to be copied Integer number = 0; SortedMap<FileNameDate, File> destMap = fileMapByNameAndDate(destPath, filePattern); SortedMap<FileNameDate, File> sourceMap = fileMapByNameAndDate(sourcePath, filePattern); // identify the list of source files different from their destinations for (FileNameDate fk : destMap.keySet()) { sourceMap.remove(fk); } // copy the list of files from source to destination for (File file : sourceMap.values()) { log.debug(file.getName() + ": " + new Date(file.lastModified())); File copy = new File(destPath, file.getName()); try { if (!copy.exists()) { // copy to tmp file first to avoid clashes during lengthy copy action File tmp = File.createTempFile("vrs", ".tmp", copy.getParentFile()); FileCopyUtils.copy(file, tmp); if (!tmp.renameTo(copy)) { tmp.delete(); // cleanup if we fail } number++; } } catch (IOException ex) { log.error("FileCopy error for file " + file.getName(), ex); } } return number; }
From source file:com.flexive.shared.FxFileUtils.java
/** * Move a file if possible, fallback to normal copy/delete if it fails. * * @param source the source file * @param destination the target file * @return true on success/*from w w w. ja v a 2 s .c o m*/ * @since 3.2.0 */ public static boolean moveFile(File source, File destination) { if (!source.renameTo(destination)) { if (!copyFile(source, destination)) { return false; } removeFile(source); } return true; }
From source file:Main.java
public static void saveBArrToFile(String fileName, byte[] barr) throws IOException { File file = new File(fileName); file.getParentFile().mkdirs();//from ww w . ja va 2s . com File tempFile = new File(fileName + ".tmp"); FileOutputStream fos = new FileOutputStream(tempFile); BufferedOutputStream bos = new BufferedOutputStream(fos); bos.write(barr, 0, barr.length); bos.flush(); fos.flush(); bos.close(); fos.close(); file.delete(); tempFile.renameTo(file); }
From source file:Main.java
public static void readStreamToFile(InputStream inStream, String filePath) throws Exception { File file = new File(filePath + ".wei"); RandomAccessFile outStream = new RandomAccessFile(file, "rw"); outStream.seek(0);/*from ww w . ja va2s . co m*/ byte[] buffer = new byte[1024]; int len = -1; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } outStream.close(); inStream.close(); file.renameTo(new File(filePath)); return; }
From source file:UtilFunctions.java
public static int batchRename(File[] files, String prefix, String postfix, String replaceThis, String withThis) {/*from ww w . j a v a 2s . com*/ try { int filesChanged = 0; for (File f : files) { //This bit should have better duplicate name support if (f.getName().contains(replaceThis)) { String name = f.getName(); name = name.replace(replaceThis, withThis); f.renameTo(new File(f.getParentFile().getCanonicalPath() + "/" + name)); } String name = prefix + f.getName() + postfix; f.renameTo(new File(f.getParentFile().getCanonicalPath() + "/" + name)); filesChanged++; } return filesChanged; } catch (Exception e) { return -1; } }
From source file:com.sk89q.craftbook.mech.area.CopyManager.java
/** * Renames a namespace.//from w w w .j a v a 2 s. c o m * * @param originalName The old name. * @param newName The new name. (Post rename) */ public static void renameNamespace(File dataFolder, String originalName, String newName) { File oldDir = new File(dataFolder, "areas/" + originalName); File newDir = new File(dataFolder, "areas/" + newName); if (oldDir.isDirectory()) { oldDir.renameTo(newDir); } else { oldDir.mkdir(); oldDir.renameTo(newDir); } }
From source file:Main.java
public static void writeToFile(Collection<?> collection, File file) throws IOException { if (collection != null && file != null) { file.getParentFile().mkdirs();/*ww w .j a v a2 s .c o m*/ File tempFile = new File(file.getAbsolutePath() + ".tmp"); FileWriter fw = new FileWriter(tempFile); for (Object obj : collection) { fw.write(new StringBuilder(obj.toString()).append("\r\n").toString()); } fw.flush(); fw.close(); file.delete(); tempFile.renameTo(file); } }
From source file:org.openmrs.module.report.util.FileUtils.java
public static void renameFile(String fileOlder, String newName) { File file = new File(fileOlder); File newFile = new File(newName); if (file != null && file.isFile()) { file.renameTo(newFile); }/*from www. j av a 2s . c om*/ }
From source file:Main.java
public static void writeToFile(Iterator<?> iter, File file) throws IOException { if (iter != null && file != null) { file.getParentFile().mkdirs();/* www . j ava 2 s. c om*/ File tempFile = new File(file.getAbsolutePath() + ".tmp"); FileWriter fw = new FileWriter(tempFile); for (; iter.hasNext();) { fw.write(new StringBuilder(iter.next().toString()).append("\r\n").toString()); } fw.flush(); fw.close(); file.delete(); tempFile.renameTo(file); } }
From source file:Main.java
public static void saveObj(Object obj, String fileName) throws IOException { File f = new File(fileName + ".tmp"); f.getParentFile().mkdirs();/*from w w w . ja v a 2 s .c om*/ FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(obj); out.flush(); out.close(); fos.flush(); fos.close(); File file = new File(fileName); file.delete(); f.renameTo(file); }