com.google.common.io.Files.createParentDirs()方法的使用及代码示例

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

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

Files.createParentDirs介绍

[英]Creates any necessary but nonexistent parent directories of the specified file. Note that if this operation fails it may have succeeded in creating some (but not all) of the necessary parent directories.
[中]创建指定文件的任何必需但不存在的父目录。请注意,如果此操作失败,它可能已成功创建了一些(但不是全部)必需的父目录。

代码示例

代码示例来源:origin: prestodb/presto

private static File createOutputFile(String fileName)
    throws IOException
{
  File outputFile = new File(fileName);
  Files.createParentDirs(outputFile);
  return outputFile;
}

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

private void createDirectoryStructureIfNotExists(final File dir) throws IOException {
  if (!dir.exists()) {
    try {
      Files.createParentDirs(new File(dir, DUMMY_FILE_NAME));
    } catch (final IOException e) {
      throw new IOException("Failed to create directory: " + dir.getAbsolutePath(), e);
    }
  } else if (!dir.isDirectory()) {
    throw new IOException("This location is not a directory: " + dir.getAbsolutePath());
  }
}

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

@GuardedBy("tasks")
private void saveRunningTasks()
{
 final File restoreFile = getRestoreFile();
 final List<String> theTasks = new ArrayList<>();
 for (ForkingTaskRunnerWorkItem forkingTaskRunnerWorkItem : tasks.values()) {
  theTasks.add(forkingTaskRunnerWorkItem.getTaskId());
 }
 try {
  Files.createParentDirs(restoreFile);
  jsonMapper.writeValue(restoreFile, new TaskRestoreInfo(theTasks));
 }
 catch (Exception e) {
  log.warn(e, "Failed to save tasks to restore file[%s]. Skipping this save.", restoreFile);
 }
}

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

/**
 * Saves this {@link FileConfiguration} to the specified location.
 * <p>
 * If the file does not exist, it will be created. If already exists, it
 * will be overwritten. If it cannot be overwritten or created, an
 * exception will be thrown.
 * <p>
 * This method will save using the system default encoding, or possibly
 * using UTF8.
 *
 * @param file File to save to.
 * @throws IOException Thrown when the given file cannot be written to for
 *     any reason.
 * @throws IllegalArgumentException Thrown when file is null.
 */
public void save(File file) throws IOException {
  Validate.notNull(file, "File cannot be null");
  Files.createParentDirs(file);
  String data = saveToString();
  Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF8_OVERRIDE && !UTF_BIG ? Charsets.UTF_8 : Charset.defaultCharset());
  try {
    writer.write(data);
  } finally {
    writer.close();
  }
}

代码示例来源:origin: prestodb/presto

@VisibleForTesting
static MemoryHistory getHistory(File historyFile)
{
  MemoryHistory history;
  try {
    //  try creating the history file and its parents to check
    // whether the directory tree is readable/writeable
    createParentDirs(historyFile.getParentFile());
    historyFile.createNewFile();
    history = new FileHistory(historyFile);
    history.setMaxSize(10000);
  }
  catch (IOException e) {
    System.err.printf("WARNING: Failed to load history file (%s): %s. " +
            "History will not be available during this session.%n",
        historyFile, e.getMessage());
    history = new MemoryHistory();
  }
  history.setAutoTrim(true);
  return history;
}

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

public static void createSourceJsonFile(File sourceJsonFile) throws IOException {
 Files.createParentDirs(sourceJsonFile);
 Files.write(SOURCE_JSON_DOCS, sourceJsonFile, ConfigurationKeys.DEFAULT_CHARSET_ENCODING);
}

代码示例来源:origin: google/guava

public void testCreateParentDirs_relativePath() throws IOException {
 File file = file("nonexistent.file");
 assertNull(file.getParentFile());
 assertNotNull(file.getCanonicalFile().getParentFile());
 Files.createParentDirs(file);
}

代码示例来源:origin: google/guava

public void testCreateParentDirs_root() throws IOException {
 File file = root();
 assertNull(file.getParentFile());
 assertNull(file.getCanonicalFile().getParentFile());
 Files.createParentDirs(file);
}

代码示例来源:origin: google/guava

public void testCreateParentDirs_noParentsNeeded() throws IOException {
 File file = file(getTempDir(), "nonexistent.file");
 assertTrue(file.getParentFile().exists());
 Files.createParentDirs(file);
}

代码示例来源:origin: google/guava

public void testCreateParentDirs_multipleParentsNeeded() throws IOException {
 File file = file(getTempDir(), "grandparent", "parent", "nonexistent.file");
 File parent = file.getParentFile();
 File grandparent = parent.getParentFile();
 assertFalse(grandparent.exists());
 Files.createParentDirs(file);
 assertTrue(parent.exists());
}

代码示例来源:origin: google/guava

public void testCreateParentDirs_oneParentNeeded() throws IOException {
 File file = file(getTempDir(), "parent", "nonexistent.file");
 File parent = file.getParentFile();
 assertFalse(parent.exists());
 try {
  Files.createParentDirs(file);
  assertTrue(parent.exists());
 } finally {
  assertTrue(parent.delete());
 }
}

代码示例来源:origin: fabric8io/docker-maven-plugin

@Override
public synchronized void open() throws IOException {
  if (this.sps == null) {
    String file = outputSpec.getFile();
    if (outputSpec.isLogStdout() || file == null) {
      this.sps = new SharedPrintStream(System.out);
    } else {
      SharedPrintStream cachedPs = printStreamMap.get(file);
      if (cachedPs == null) {
        Files.createParentDirs(new File(file));
        PrintStream ps = new PrintStream(new FileOutputStream(file), true);
        cachedPs = new SharedPrintStream(ps);
        printStreamMap.put(file, cachedPs);
      } else {
        cachedPs.allocate();
      }
      this.sps = cachedPs;
    }
  }
}

代码示例来源:origin: google/guava

public void testCreateParentDirs_nonDirectoryParentExists() throws IOException {
 File parent = getTestFile("ascii.txt");
 assertTrue(parent.isFile());
 File file = file(parent, "foo");
 try {
  Files.createParentDirs(file);
  fail();
 } catch (IOException expected) {
 }
}

代码示例来源:origin: ninjaframework/ninja

Files.createParentDirs(new File(pathToApplicationConfInSrcDir));

代码示例来源:origin: prestodb/presto

public void generate()
    throws Exception
{
  initPlanTest();
  try {
    getQueryResourcePaths()
        .parallel()
        .forEach(queryResourcePath -> {
          try {
            Path queryPlanWritePath = Paths.get(
                getSourcePath().toString(),
                "src/test/resources",
                getQueryPlanResourcePath(queryResourcePath));
            createParentDirs(queryPlanWritePath.toFile());
            write(generateQueryPlan(read(queryResourcePath)).getBytes(UTF_8), queryPlanWritePath.toFile());
            System.out.println("Generated expected plan for query: " + queryResourcePath);
          }
          catch (IOException e) {
            throw new UncheckedIOException(e);
          }
        });
  }
  finally {
    destroyPlanTest();
  }
}

代码示例来源:origin: soabase/exhibitor

if ( usState.getUs() != null )
  Files.createParentDirs(idFile);
  String                  id = String.format("%d\n", usState.getUs().getServerId());
  Files.write(id.getBytes(), idFile);

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

/**
  * Write contents to a file
  *
  * @param file
  * @param contents
  * @return file handle
  * @throws IOException
  */
 public static File write(File file, String contents) throws IOException {
  com.google.common.io.Files.createParentDirs(file);
  com.google.common.io.Files.write(contents, file, StandardCharsets.UTF_8);
  return file;
 }
}

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

/**
  * Write contents to a file
  *
  * @param file
  * @param contents
  * @return file handle
  * @throws IOException
  */
 public static File write(File file, String contents) throws IOException {
  com.google.common.io.Files.createParentDirs(file);
  com.google.common.io.Files.write(contents, file, StandardCharsets.UTF_8);
  return file;
 }
}

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

/**
 * Open a CSV writer to write to a file.
 *
 * @param file        The file to write to.
 * @param layout      The layout of the table.
 * @param compression What compression, if any, to use.
 * @return A CSV writer outputting to {@code file}.
 * @throws IOException if there is an error opening the file or writing the column header.
 */
public static CSVWriter open(File file, TableLayout layout, CompressionMode compression) throws IOException {
  Files.createParentDirs(file);
  Writer writer = LKFileUtils.openOutput(file, Charset.defaultCharset(), compression);
  try {
    return new CSVWriter(writer, layout);
  } catch (Throwable th) {
    try {
      writer.close();
    } catch (Throwable th2) {
      th.addSuppressed(th2);
    }
    Throwables.propagateIfInstanceOf(th, IOException.class);
    throw Throwables.propagate(th);
  }
}

代码示例来源:origin: git-commit-id/maven-git-commit-id-plugin

Files.createParentDirs(gitPropsFile);
try (OutputStream outputStream = new FileOutputStream(gitPropsFile)) {
 SortedProperties sortedLocalProperties = new SortedProperties();

相关文章