Esempio n. 1
0
  /**
   * Rename file using copy and delete strategy. This is primarily used in environments where the
   * regular rename operation is unreliable.
   *
   * @param from the file to be renamed
   * @param to the new target file
   * @return <tt>true</tt> if the file was renamed successfully, otherwise <tt>false</tt>
   * @throws IOException If an I/O error occurs during copy or delete operations.
   */
  public static boolean renameFileUsingCopy(File from, File to) throws IOException {
    // do not try to rename non existing files
    if (!from.exists()) {
      return false;
    }

    boolean renamed = false;

    LOG.debug("Rename file '{}' to '{}' using copy/delete strategy.", from, to);

    copyFile(from, to);
    if (!deleteFile(from)) {
      throw new IOException(
          "Renaming file from '"
              + from
              + "' to '"
              + to
              + "' failed: Cannot delete file '"
              + from
              + "' after copy succeeded");
    } else {
      renamed = true;
    }

    return renamed;
  }
  public void testLoadFileWithSpace() throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.start();

    createDirectory("target/my space");
    FileUtil.copyFile(
        new File("src/test/resources/log4j2.properties"),
        new File("target/my space/log4j2.properties"));

    InputStream is =
        ResourceHelper.resolveMandatoryResourceAsInputStream(
            context, "file:target/my%20space/log4j2.properties");
    assertNotNull(is);

    String text = context.getTypeConverter().convertTo(String.class, is);
    assertNotNull(text);
    assertTrue(text.contains("log4j"));
    is.close();

    context.stop();
  }