/** * Called to overwrite * current version with backup file */ @Override protected void replace(File dst, File backup) throws IOException { dst.delete(); // any failure up to here is no big deal if(!backup.renameTo(dst)) { throw new IOException("Failed to rename "+backup+" to "+dst); } }
@Override public void rename(File from, File to) throws IOException { delete(to); if (!from.renameTo(to)) { throw new IOException("failed to rename " + from + " to " + to); } }
// File (or directory) with old name File file = new File("oldname"); // File (or directory) with new name File file2 = new File("newname"); if (file2.exists()) throw new java.io.IOException("file exists"); // Rename file (or directory) boolean success = file.renameTo(file2); if (!success) { // File was not successfully renamed }
/*package*/ static void relocateOldLogs(File dir) { final Pattern logfile = Pattern.compile("slave-(.*)\\.log(\\.[0-9]+)?"); File[] logfiles = dir.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return logfile.matcher(name).matches(); } }); if (logfiles==null) return; for (File f : logfiles) { Matcher m = logfile.matcher(f.getName()); if (m.matches()) { File newLocation = new File(dir, "logs/slaves/" + m.group(1) + "/slave.log" + Util.fixNull(m.group(2))); newLocation.getParentFile().mkdirs(); boolean relocationSuccessful=f.renameTo(newLocation); if (relocationSuccessful) { // The operation will fail if mkdir fails LOGGER.log(Level.INFO, "Relocated log file {0} to {1}",new Object[] {f.getPath(),newLocation.getPath()}); } else { LOGGER.log(Level.WARNING, "Cannot relocate log file {0} to {1}",new Object[] {f.getPath(),newLocation.getPath()}); } } else { assert false; } } }
/** * If the file created by {@link #writeTempCacheFile(InputStream, FileExtension)} was successfully parsed, * this should be called to remove the temporary part of its name which will allow it to be a cache hit in the future. */ void renameTempFile(FileExtension extension) { String fileName = filenameForUrl(url, extension, true); File file = new File(appContext.getCacheDir(), fileName); String newFileName = file.getAbsolutePath().replace(".temp", ""); File newFile = new File(newFileName); boolean renamed = file.renameTo(newFile); L.debug("Copying temp file to real file (" + newFile + ")"); if (!renamed) { L.warn( "Unable to rename cache file " + file.getAbsolutePath() + " to " + newFile.getAbsolutePath() + "."); } }
/** * Backs up a directory <tt>dir</tt> by creating a .bak next to it. * * @param dir The directory to back up. */ public static void backupDirectory(File dir) throws IOException { File bak = new File(dir.getCanonicalPath() + ".bak"); if (bak.exists()) { FileUtils.deleteDirectory(bak); } dir.renameTo(bak); }
File rootDir = getRootDir(); if (!rootDir.isDirectory()) { throw new IOException(this + ": " + rootDir + " looks to have already been deleted; siblings: " + Arrays.toString(project.getBuildDir().list())); File tmp = new File(rootDir.getParentFile(),'.'+rootDir.getName()); if (tmp.exists()) { Util.deleteRecursive(tmp); boolean renamingSucceeded = rootDir.renameTo(tmp); Util.deleteRecursive(tmp); if(tmp.exists()) tmp.deleteOnExit(); throw new IOException(rootDir+" is in use"); LOGGER.log(FINE, "{0}: {1} successfully deleted", new Object[] {this, rootDir});
public static void unpack(File localrsrc, File dst, boolean symLinksDisabled) throws IOException { String lowerDst = localrsrc.getName().toLowerCase(); if (lowerDst.endsWith(".jar") || lowerDst.endsWith("_jar")) { unJar(localrsrc, dst); } else if (lowerDst.endsWith(".zip") || lowerDst.endsWith("_zip")) { unZip(localrsrc, dst); } else if (lowerDst.endsWith(".tar.gz") || lowerDst.endsWith("_tar_gz") || lowerDst.endsWith(".tgz") || lowerDst.endsWith("_tgz") || lowerDst.endsWith(".tar") || lowerDst.endsWith("_tar")) { unTar(localrsrc, dst, symLinksDisabled); } else { LOG.warn("Cannot unpack " + localrsrc); if (!localrsrc.renameTo(dst)) { throw new IOException("Unable to rename file: [" + localrsrc + "] to [" + dst + "]"); } } if (localrsrc.isFile()) { localrsrc.delete(); } }
public static void moveFile(File src, File targetDirectory) throws IOException { if (!src.renameTo(new File(targetDirectory, src.getName()))) { // If rename fails we must do a true deep copy instead. Path sourcePath = src.toPath(); Path targetDirPath = targetDirectory.toPath(); try { Files.move(sourcePath, targetDirPath.resolve(sourcePath.getFileName()), StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { throw new IOException("Failed to move " + src + " to " + targetDirectory + " - " + ex.getMessage()); } } }
private static void doMoveDirectory(File src, File dest) throws IOException { if (dest.exists()) { if (!dest.isDirectory()) { throw new IOException(MSG_NOT_A_DIRECTORY + dest); } dest = file(dest, dest.getName()); dest.mkdir(); } final boolean rename = src.renameTo(dest); if (!rename) { doCopyDirectory(src, dest, params()); deleteDir(src); } }
private static void moveFile(File sourceFile, File targetFile) { boolean rename = sourceFile.renameTo(targetFile); // Check if the file was cached by another process during download if (!rename && !targetFile.exists()) { LOGGER.warn("Unable to rename {} to {}", sourceFile.getAbsolutePath(), targetFile.getAbsolutePath()); LOGGER.warn("A copy/delete will be tempted but with no guarantee of atomicity"); try { Files.move(sourceFile.toPath(), targetFile.toPath()); } catch (IOException e) { throw new IllegalStateException("Fail to move " + sourceFile.getAbsolutePath() + " to " + targetFile, e); } } }
/** * Rename a legacy file to a new name, with care to Windows where {@link File#renameTo(File)} * doesn't work if the destination already exists. */ private void rename(File legacyFile, File newFile) throws IOException { if (!legacyFile.exists()) return; if (newFile.exists()) { Util.deleteFile(newFile); } if (!legacyFile.renameTo(newFile)) { LOGGER.warning("Failed to rename " + legacyFile + " to " + newFile); } }
public static void renameOrMoveByCopying(File from, File to) throws IOException { deleteFileIfExists(to); boolean renameFailed = !from.renameTo(to); if (renameFailed) { copyFile(from, to); boolean deleteFromFailed = !from.delete(); if (deleteFromFailed) { Timber.e("Unable to delete source file after copying to destination!"); } } }
@Override public Void invoke(File f, VirtualChannel channel) throws IOException { // JENKINS-16846: if f.getName() is the same as one of the files/directories in f, // then the rename op will fail File tmp = new File(f.getAbsolutePath()+".__rename"); if (!f.renameTo(tmp)) throw new IOException("Failed to rename "+f+" to "+tmp); File t = new File(target.getRemote()); for(File child : reading(tmp).listFiles()) { File target = new File(t, child.getName()); if(!stating(child).renameTo(creating(target))) throw new IOException("Failed to rename "+child+" to "+target); } deleting(tmp).delete(); return null; } }
private void trimSilences(File wavFile) throws UnsupportedAudioFileException, IOException { // We hard-code the values here. Use marytts.tools.voiceimport.EndpointDetector if you want to tune them. int energyBufferLength = 20; double speechStartLikelihood = 0.1; double speechEndLikelihood = 0.1; double shiftFromMinimumEnergyCenter = 0.0; int numClusters = 4; double minimumStartSilenceInSeconds = 0.5; double minimumEndSilenceInSeconds = 0.5; File tmpFile = new File("tmpAudio.wav"); AudioConverterUtils.removeEndpoints(wavFile.getAbsolutePath(), tmpFile.getAbsolutePath(), energyBufferLength, speechStartLikelihood, speechEndLikelihood, shiftFromMinimumEnergyCenter, numClusters, minimumStartSilenceInSeconds, minimumEndSilenceInSeconds); if (!tmpFile.renameTo(wavFile)) FileUtils.copy(tmpFile.getAbsolutePath(), wavFile.getAbsolutePath()); } }