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

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

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

FileUtils.forceMkdirParent介绍

[英]Makes any necessary but nonexistent parent directories for a given File. If the parent directory cannot be created then an IOException is thrown.
[中]为给定文件创建任何必要但不存在的父目录。如果无法创建父目录,则会引发IOException。

代码示例

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

private static File extractJarEntry(JarFile jarFile, JarEntry jarEntry, File targetFile) {
  LOG.debug("Extracting {}!/{} -> {}", jarFile, jarEntry, targetFile);
  try (InputStream inputStream = jarFile.getInputStream(jarEntry)) {
    FileUtils.forceMkdirParent(targetFile);
    Files.copy(inputStream, targetFile.toPath());
    return targetFile;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

/**
 * Switches the underlying output stream from a memory based stream to one
 * that is backed by disk. This is the point at which we realise that too
 * much data is being written to keep in memory, so we elect to switch to
 * disk-based storage.
 *
 * @throws IOException if an error occurs.
 */
@Override
protected void thresholdReached() throws IOException
{
  if (prefix != null) {
    outputFile = File.createTempFile(prefix, suffix, directory);
  }
  FileUtils.forceMkdirParent(outputFile);
  final FileOutputStream fos = new FileOutputStream(outputFile);
  try {
    memoryOutputStream.writeTo(fos);
  } catch (final IOException e){
    fos.close();
    throw e;
  }
  currentOutputStream = fos;
  memoryOutputStream = null;
}

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

public String createFile(String fileName, Instant lastModifiedTime) throws Exception {
  Path path = Paths.get(dataDir, keyspace, columnfamily, fileName);
  FileUtils.forceMkdirParent(path.toFile());
  try (FileWriter fileWriter = new FileWriter(path.toFile())) {
    fileWriter.write("");
  }
  path.toFile().setLastModified(lastModifiedTime.toEpochMilli());
  return path.toString();
}

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

@Test
public void testForceMkdirParent() throws Exception {
  // Tests with existing directory
  assertTrue(getTestDirectory().exists());
  final File testParentDir = new File(getTestDirectory(), "testForceMkdirParent");
  testParentDir.delete();
  assertFalse(testParentDir.exists());
  final File testFile = new File(testParentDir, "test.txt");
  assertFalse(testParentDir.exists());
  assertFalse(testFile.exists());
  // Create
  FileUtils.forceMkdirParent(testFile);
  assertTrue(testParentDir.exists());
  assertFalse(testFile.exists());
  // Again
  FileUtils.forceMkdirParent(testFile);
  assertTrue(testParentDir.exists());
  assertFalse(testFile.exists());
}

代码示例来源:origin: io.cloudslang.tools/cs-content-packager-sources

public static void saveDescriptionAsProperties(@NotNull final Path parentDir, @NotNull final Path propertiesFile) {
  final LinkedHashMap<String, String> cpProperties = getCpProperties(parentDir);
  try {
    if (!propertiesFile.toFile().exists()) {
      FileUtils.forceMkdirParent(propertiesFile.toFile());
      FileUtils.touch(propertiesFile.toFile());
    }
    final String propertyFilesContent = cpProperties.entrySet().stream()
        .map(entry -> String.format("%s=%s", entry.getKey(), entry.getValue()))
        .collect(Collectors.joining(System.lineSeparator()));
    writeStringToFile(propertiesFile.toFile(), propertyFilesContent, UTF_8, false);
  } catch (IOException e) {
    log.error(format("Could not write properties to file [%s].", propertiesFile));
  }
}

代码示例来源:origin: eroispaziali/ForceFlow

private static File serializeXml(File output, Object object) throws IOException {
  Serializer serializer = new Persister();
  FileUtils.forceMkdirParent(output);
  try {
    serializer.write(object, output);
    return output;
  } catch (Exception e) {
    e.printStackTrace();
    throw new IOException("Unable to serialize");
  }
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

/**
 * Switches the underlying output stream from a memory based stream to one
 * that is backed by disk. This is the point at which we realise that too
 * much data is being written to keep in memory, so we elect to switch to
 * disk-based storage.
 *
 * @throws IOException if an error occurs.
 */
@Override
protected void thresholdReached() throws IOException
{
  if (prefix != null) {
    outputFile = File.createTempFile(prefix, suffix, directory);
  }
  FileUtils.forceMkdirParent(outputFile);
  final FileOutputStream fos = new FileOutputStream(outputFile);
  try {
    memoryOutputStream.writeTo(fos);
  } catch (final IOException e){
    fos.close();
    throw e;
  }
  currentOutputStream = fos;
  memoryOutputStream = null;
}

代码示例来源:origin: gr8pefish/IronBackpacks

/**
 * Converts a {@link T} to JSON and writes it to file. If the file does not exist, a new one is created. If the file
 * does exist, the contents are overwritten with the new value.
 *
 * @param type  The object to write to JSON.
 * @param token The token type to use for serialization.
 * @param file  The file to write the JSON to.
 * @param <T>   The object type to write.
 */
public static <T> void toJson(@Nonnull T type, @Nonnull TypeToken<T> token, @Nonnull File file) {
  if (!file.exists()) {
    try {
      FileUtils.forceMkdirParent(file);
      file.createNewFile();
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
  }
  FileWriter writer = null;
  try {
    writer = new FileWriter(file);
    writer.write(getJson(type, token));
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    IOUtils.closeQuietly(writer);
  }
}

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

private File customAnimation(final Job job, final URI input, final Map<String, String> metadata)
    throws IOException, NotFoundException {
 logger.debug("Start customizing the animation");
 File output = new File(workspace.rootDirectory(), String.format("animate/%d/%s.%s", job.getId(),
     FilenameUtils.getBaseName(input.getPath()), FilenameUtils.getExtension(input.getPath())));
 FileUtils.forceMkdirParent(output);
 String animation;
 try {
  animation = FileUtils.readFileToString(new File(input), "UTF-8");
 } catch (IOException e) {
  // Maybe no local file?
  logger.debug("Falling back to workspace to read {}", input);
  try (InputStream in = workspace.read(input)) {
   animation = IOUtils.toString(in, "UTF-8");
  }
 }
 // replace all metadata
 for (Map.Entry<String, String> entry: metadata.entrySet()) {
  String value = StringEscapeUtils.escapeXml11(entry.getValue());
  animation = animation.replaceAll("\\{\\{" + entry.getKey() + "\\}\\}", value);
 }
 // write new animation file
 FileUtils.write(output, animation, "utf-8");
 return output;
}

代码示例来源:origin: org.fujion/fujion-core

} else {
  File file = new File(output);
  FileUtils.forceMkdirParent(file);

代码示例来源:origin: org.codehaus.izpack/izpack-compiler

private JarOutputStream getJarOutputStream(File jarFile) throws IOException
{
  FileUtils.deleteQuietly(jarFile);
  if (compilerData.isMkdirs())
  {
    FileUtils.forceMkdirParent(jarFile);
  }
  FileOutputStream fileOutputStream = new FileOutputStream(jarFile);
  JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream);
  int level = compilerData.getComprLevel();
  if (level >= 0 && level < 10)
  {
    jarOutputStream.setLevel(level);
  } else
  {
    jarOutputStream.setLevel(Deflater.BEST_COMPRESSION);
  }
  return jarOutputStream;
}

代码示例来源:origin: org.codehaus.izpack/izpack-compiler

FileUtils.forceMkdirParent(file);

代码示例来源:origin: org.codehaus.izpack/izpack-installer

FileUtils.forceMkdirParent(new File(pattern));

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

FileUtils.forceMkdirParent(output);

代码示例来源:origin: eroispaziali/ForceFlow

try {
  File destFile = new File(destPath + "/" + ff.getFilenameNextVersion());
  FileUtils.forceMkdirParent(destFile);
  FileUtils.copyFile(ff.getFile(), destFile);
} catch (IOException e) {

代码示例来源:origin: xautlx/s2jh4net

} else {
  logger.debug("Process imageWatermark Local File from {} to {}", relativeFilePath, newRelativeFilePath);
  FileUtils.forceMkdirParent(toFile);
  Thumbnails.of(fromFilePath)
      .watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.2f)

代码示例来源:origin: xautlx/s2jh4net

FileUtils.forceMkdirParent(toFile);
  FileUtils.moveFile(tempFile, toFile);
} else {

代码示例来源:origin: org.apereo.cas/cas-server-support-saml-idp-metadata

@Override
public Collection<? extends MetadataResolver> resolve(final SamlRegisteredService service) {
  HttpResponse response = null;
  try {
    val metadataLocation = getMetadataLocationForService(service);
    LOGGER.info("Loading SAML metadata from [{}]", metadataLocation);
    val metadataResource = new UrlResource(metadataLocation);
    val backupFile = getMetadataBackupFile(metadataResource, service);
    val canonicalPath = backupFile.getCanonicalPath();
    LOGGER.debug("Metadata backup file will be at [{}]", canonicalPath);
    FileUtils.forceMkdirParent(backupFile);
    response = fetchMetadata(metadataLocation);
    cleanUpExpiredBackupMetadataFilesFor(metadataResource, service);
    if (response != null) {
      val status = HttpStatus.valueOf(response.getStatusLine().getStatusCode());
      if (shouldHttpResponseStatusBeProcessed(status)) {
        val metadataProvider = getMetadataResolverFromResponse(response, backupFile);
        configureAndInitializeSingleMetadataResolver(metadataProvider, service);
        return CollectionUtils.wrap(metadataProvider);
      }
    }
  } catch (final Exception e) {
    LOGGER.error(e.getMessage(), e);
  } finally {
    HttpUtils.close(response);
  }
  return new ArrayList<>(0);
}

代码示例来源:origin: eroispaziali/ForceFlow

@Override
public void execute() throws BuildException { 
  try {
    SoapConnection tc = ConnectionFactory.getToolingConnection(getConfig());
    RunTestsRequest request = new RunTestsRequest();
    
    log("Running all tests...");
    request.setAllTests(true);
    RunTestsResult result = tc.runTests(request);
    Collection<TestSuite> suites = buildTestReport(result);
    Integer i = 1;
    for (TestSuite testSuite : suites) {
      String filename = String.format("%s/test-report-%s.xml", testReportsDir, i++);
      try {
        Serializer serializer = new Persister();
        File source = new File(filename);
        FileUtils.forceMkdirParent(source);
        serializer.write(testSuite, source);
      } catch (Exception e) {
        log("Error: " + e);
        e.printStackTrace();
      }
    }
    
    
  } catch (ConnectionException e) {
    handleException(e);
  }
}

代码示例来源:origin: com.cerner.ccl.comm/j4ccl-ssh

if (!logfileLocation.isEmpty()) {
  try {
    FileUtils.forceMkdirParent(new File(logfileLocation));
  } catch (IOException e) {
    stream = new ByteArrayOutputStream();

相关文章

微信公众号

最新文章

更多

FileUtils类方法