org.apache.commons.io.FileUtils.moveFile()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(381)

本文整理了Java中org.apache.commons.io.FileUtils.moveFile()方法的一些代码示例,展示了FileUtils.moveFile()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils.moveFile()方法的具体详情如下:
包路径:org.apache.commons.io.FileUtils
类名称:FileUtils
方法名:moveFile

FileUtils.moveFile介绍

[英]Moves a file.

When the destination file is on another file system, do a "copy and delete".
[中]

代码示例

代码示例来源:origin: alibaba/jstorm

public static void mv(String src, String dest) {
  try {
    FileUtils.moveFile(new File(src), new File(dest));
  } catch (IOException ignored) {
  }
}

代码示例来源:origin: alibaba/jstorm

public static void mv(String src, String dest) {
  try {
    FileUtils.moveFile(new File(src), new File(dest));
  } catch (IOException ignored) {
  }
}

代码示例来源:origin: alibaba/jstorm

public static void mv(String src, String dest) {
    try {
      FileUtils.moveFile(new File(src), new File(dest));
    } catch (Exception ignored) {
    }
  }
}

代码示例来源:origin: apache/incubator-pinot

/**
 * Deletes the destination file if it exists then calls org.apache.commons moveFile.
 * @param srcFile
 * @param destFile
 */
public static void moveFileWithOverwrite(File srcFile, File destFile)
  throws IOException {
 if (destFile.exists()) {
  org.apache.commons.io.FileUtils.deleteQuietly(destFile);
 }
 org.apache.commons.io.FileUtils.moveFile(srcFile, destFile);
}

代码示例来源:origin: Netflix/Priam

public File createTmpMetaFile() throws IOException {
  File metafile = File.createTempFile("meta", ".json");
  File destFile = new File(metafile.getParent(), "meta.json");
  if (destFile.exists()) destFile.delete();
  FileUtils.moveFile(metafile, destFile);
  return destFile;
}

代码示例来源:origin: commons-io/commons-io

throw new IOException("Destination '" + destDir + "' is not a directory");
moveFile(srcFile, new File(destDir, srcFile.getName()));

代码示例来源:origin: apache/geode

private void renameJarWithOldNamingConvention(File oldJar) throws IOException {
 Matcher matcher = oldNamingPattern.matcher(oldJar.getName());
 if (!matcher.matches()) {
  throw new IllegalArgumentException("The given jar " + oldJar.getCanonicalPath()
    + " does not match the old naming convention");
 }
 String unversionedJarNameWithoutExtension = matcher.group(1);
 String jarVersion = matcher.group(2);
 String newJarName = unversionedJarNameWithoutExtension + ".v" + jarVersion + ".jar";
 File newJar = new File(this.deployDirectory, newJarName);
 logger.debug("Renaming deployed jar from {} to {}", oldJar.getCanonicalPath(),
   newJar.getCanonicalPath());
 FileUtils.moveFile(oldJar, newJar);
}

代码示例来源:origin: gocd/gocd

@Override void createCachedFile(ArtifactFolder artifactFolder) throws IOException {
  File originalFolder = artifactFolder.getRootFolder();
  File cachedZip = cachedFile(artifactFolder);
  File cachedTempZip = zipToTempFile(cachedZip);
  cachedTempZip.getParentFile().mkdirs();
  try {
    zipUtil.zip(originalFolder, cachedTempZip, Deflater.DEFAULT_COMPRESSION);
  } catch (IOException e) {
    cachedTempZip.delete();
    throw e;
  }
  FileUtils.moveFile(cachedTempZip, cachedZip);
}

代码示例来源:origin: gocd/gocd

public void moveConsoleArtifacts(LocatableEntity locatableEntity) {
    try {
      File from = chooser.temporaryConsoleFile(locatableEntity);

      // Job cancellation skips temporary file creation. Force create one if it does not exist.
      FileUtils.touch(from);

      File to = consoleLogArtifact(locatableEntity);
      FileUtils.moveFile(from, to);
    } catch (IOException | IllegalArtifactLocationException e) {
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: hs-web/hsweb-framework

private static State saveTmpFile(File tmpFile, String path) {
  State state = null;
  File targetFile = new File(path);
  if (targetFile.canWrite()) {
    return new BaseState(false, AppInfo.PERMISSION_DENIED);
  }
  try {
    FileUtils.moveFile(tmpFile, targetFile);
  } catch (IOException e) {
    return new BaseState(false, AppInfo.IO_ERROR);
  }
  state = new BaseState(true);
  state.putInfo("size", targetFile.length());
  state.putInfo("title", targetFile.getName());
  return state;
}

代码示例来源:origin: SonarSource/sonarqube

private FileAndMd5 moveToCache(String pluginKey, FileAndMd5 jar) throws IOException {
 File jarInCache = new File(userHome, "cache/" + jar.md5 + "/sonar-" + pluginKey + "-plugin.jar");
 moveFile(jar.file, jarInCache);
 return new FileAndMd5(jarInCache, jar.md5);
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testMoveFile_CopyDelete() throws Exception {
  final File destination = new File(getTestDirectory(), "move2.txt");
  final File src = new File(testFile1.getAbsolutePath()) {
    private static final long serialVersionUID = 1L;
    // Force renameTo to fail, as if destination is on another
    // filesystem
    @Override
    public boolean renameTo(final File f) {
      return false;
    }
  };
  FileUtils.moveFile(src, destination);
  assertTrue("Check Exist", destination.exists());
  assertTrue("Original deleted", !src.exists());
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testMoveFile_Rename() throws Exception {
  final File destination = new File(getTestDirectory(), "move1.txt");
  FileUtils.moveFile(testFile1, destination);
  assertTrue("Check Exist", destination.exists());
  assertTrue("Original deleted", !testFile1.exists());
}

代码示例来源:origin: syncany/syncany

@Override
public void move(RemoteFile sourceFile, RemoteFile targetFile) throws StorageException {
  connect();
  File sourceRemoteFile = getRemoteFile(sourceFile);
  File targetRemoteFile = getRemoteFile(targetFile);
  if (!sourceRemoteFile.exists()) {
    throw new StorageMoveException("Unable to move file " + sourceFile + " because it does not exist.");
  }
  try {
    FileUtils.moveFile(sourceRemoteFile, targetRemoteFile);
  }
  catch (IOException ex) {
    throw new StorageException("Unable to move file " + sourceRemoteFile + " to destination " + targetRemoteFile, ex);
  }
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testMoveFile_Errors() throws Exception {
  try {
    FileUtils.moveFile(null, new File("foo"));
    fail("Expected NullPointerException when source is null");
  } catch (final NullPointerException e) {
    FileUtils.moveFile(new File("foo"), null);
    fail("Expected NullPointerException when destination is null");
  } catch (final NullPointerException e) {
    FileUtils.moveFile(new File("nonexistant"), new File("foo"));
    fail("Expected FileNotFoundException for source");
  } catch (final FileNotFoundException e) {
    FileUtils.moveFile(getTestDirectory(), new File("foo"));
    fail("Expected IOException when source is a directory");
  } catch (final IOException e) {
    FileUtils.moveFile(testSourceFile, testDestFile);
    fail("Expected FileExistsException when dest already exists");
  } catch (final FileExistsException e) {

代码示例来源:origin: commons-io/commons-io

@Test
public void testMoveFile_CopyDelete_Failed() throws Exception {
  final File destination = new File(getTestDirectory(), "move3.txt");
  final File src = new File(testFile1.getAbsolutePath()) {
    private static final long serialVersionUID = 1L;
    // Force renameTo to fail, as if destination is on another
    // filesystem
    @Override
    public boolean renameTo(final File f) {
      return false;
    }
    // Force delete failure
    @Override
    public boolean delete() {
      return false;
    }
  };
  try {
    FileUtils.moveFile(src, destination);
    fail("move should have failed as src has not been deleted");
  } catch (final IOException e) {
    // exepected
    assertTrue("Check Rollback", !destination.exists());
    assertTrue("Original exists", src.exists());
  }
}

代码示例来源:origin: syncany/syncany

public void moveFile(String fileFrom, String fileTo) throws Exception {
  File fromLocalFile = getLocalFile(fileFrom);
  File toLocalFile = getLocalFile(fileTo);
  try {
    if (fromLocalFile.isDirectory()) {
      FileUtils.moveDirectory(fromLocalFile, toLocalFile);
    }
    else {
      FileUtils.moveFile(fromLocalFile, toLocalFile);
    }
  }
  catch (Exception e) {
    throw new Exception("Move failed: " + fileFrom + " --> " + fileTo, e);
  }
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldNotifyRemoveEventBeforeAddEventInCaseOfFileRename() throws Exception {
  monitor.addPluginJarChangeListener(changeListener);
  monitor.start();
  copyPluginToThePluginDirectory(bundledPluginDir, "descriptor-aware-test-plugin-1.jar");
  waitUntilNextRun(monitor);
  PluginFileDetails orgFile = pluginFileDetails(bundledPluginDir, "descriptor-aware-test-plugin-1.jar", true);
  verify(changeListener).pluginJarAdded(orgFile);
  PluginFileDetails newFile = pluginFileDetails(bundledPluginDir, "descriptor-aware-test-plugin-1-new.jar", true);
  FileUtils.moveFile(orgFile.file(), newFile.file());
  waitUntilNextRun(monitor);
  InOrder inOrder = inOrder(changeListener);
  inOrder.verify(changeListener).pluginJarRemoved(orgFile);
  inOrder.verify(changeListener).pluginJarAdded(newFile);
  verifyNoMoreInteractions(changeListener);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldNotifyRemoveEventBeforeAddEventInCaseOfFileRename() throws Exception {
  monitor.addPluginJarChangeListener(changeListener);
  monitor.start();
  copyPluginToThePluginDirectory(pluginExternalDir, "descriptor-aware-test-external-plugin-1.jar");
  waitUntilNextRun(monitor);
  PluginFileDetails orgExternalFile = pluginFileDetails(pluginExternalDir, "descriptor-aware-test-external-plugin-1.jar", false);
  verify(changeListener).pluginJarAdded(orgExternalFile);
  PluginFileDetails newExternalFile = pluginFileDetails(pluginExternalDir, "descriptor-aware-test-external-plugin-1-new.jar", false);
  FileUtils.moveFile(orgExternalFile.file(), newExternalFile.file());
  waitUntilNextRun(monitor);
  InOrder inOrder = inOrder(changeListener);
  inOrder.verify(changeListener).pluginJarRemoved(orgExternalFile);
  inOrder.verify(changeListener).pluginJarAdded(newExternalFile);
  verifyNoMoreInteractions(changeListener);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldCreatePluginZipIfPluginJarIsUpdated() throws Exception {
  monitor.addPluginJarChangeListener(changeListener);
  monitor.start();
  copyPluginToThePluginDirectory(bundledPluginDir, "descriptor-aware-test-plugin-1.jar");
  waitUntilNextRun(monitor);
  PluginFileDetails orgFile = pluginFileDetails(bundledPluginDir, "descriptor-aware-test-plugin-1.jar", true);
  verify(changeListener).pluginJarAdded(orgFile);
  PluginFileDetails newFile = pluginFileDetails(bundledPluginDir, "descriptor-aware-test-plugin-1-new.jar", true);
  FileUtils.moveFile(orgFile.file(), newFile.file());
  waitUntilNextRun(monitor);
}

相关文章

FileUtils类方法