java.io.File.mkdirs()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.4k)|赞(0)|评价(0)|浏览(146)

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

File.mkdirs介绍

[英]Creates the directory named by this file, creating missing parent directories if necessary. Use #mkdir if you don't want to create missing parents.

Note that this method does not throw IOException on failure. Callers must check the return value. Note also that this method returns false if the directory already existed. If you want to know whether the directory exists on return, either use (f.mkdirs() || f.isDirectory())or simply ignore the return value from this method and simply call #isDirectory.
[中]创建由该文件命名的目录,必要时创建缺少的父目录。如果不想创建丢失的父项,请使用#mkdir。
请注意,此方法不会在失败时引发IOException。调用者必须检查返回值。还要注意,如果目录已经存在,则此方法返回false。如果您想知道返回时目录是否存在,可以使用(f.mkdirs()| | f.isDirectory())或忽略此方法的返回值,只需调用#isDirectory即可。

代码示例

代码示例来源:origin: stackoverflow.com

// Create a directory; all non-existent ancestor directories are
// automatically created
success = (new File("../potentially/long/pathname/without/all/dirs")).mkdirs();
if (!success) {
  // Directory creation failed
}

代码示例来源:origin: stackoverflow.com

// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);

代码示例来源:origin: Blankj/AndroidUtilCode

private static boolean createOrExistsDir(final File file) {
  return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
}

代码示例来源:origin: Tencent/tinker

public InfoWriter(Configuration config, String infoPath) throws IOException {
  this.config = config;
  this.infoPath = infoPath;
  if (infoPath != null) {
    this.infoFile = new File(infoPath);
    if (!infoFile.getParentFile().exists()) {
      infoFile.getParentFile().mkdirs();
    }
  } else {
    this.infoFile = null;
  }
}

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

private File ensureOutputDirectory() {
  File outputDir = new File(OUTPUT_DIRECTORY);
  if (!outputDir.exists()) {
    outputDir.mkdirs();
  }
  return outputDir;
}

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

@Override
  File ensureDirectoryExists( FileSystemAbstraction fileSystem, File dir )
  {
    if ( !dir.exists() && !dir.mkdirs() )
    {
      String message = String.format( "Unable to create directory path[%s] for Neo4j store" + ".",
          dir.getAbsolutePath() );
      throw new RuntimeException( message );
    }
    return dir;
  }
},

代码示例来源:origin: stackoverflow.com

File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");
file.getParentFile().mkdirs();
FileWriter writer = new FileWriter(file);

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

@Test
  public void shouldNotValidateNestingOfMaterialDirectoriesBasedOnServerSideFileSystem() throws IOException {
    final File workingDir = temporaryFolder.newFolder("go-working-dir");
    final File material1 = new File(workingDir, "material1");
    material1.mkdirs();

    final Path material2 = Files.createSymbolicLink(Paths.get(new File(workingDir, "material2").getPath()), Paths.get(material1.getPath()));

    material.setFolder(material1.getAbsolutePath());
    material.validateNotSubdirectoryOf(material2.toAbsolutePath().toString());

    assertNull(material.errors().getAllOn(FOLDER));
  }
}

代码示例来源:origin: Tencent/tinker

public static void ensureFileDirectory(File file) {
  if (file == null) {
    return;
  }
  File parentFile = file.getParentFile();
  if (!parentFile.exists()) {
    parentFile.mkdirs();
  }
}

代码示例来源:origin: jenkinsci/jenkins

public void mkdirs() {
  file.getParentFile().mkdirs();
}

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

public void streamToFile(InputStream stream, File dest) throws IOException {
  dest.getParentFile().mkdirs();
  FileOutputStream out = new FileOutputStream(dest, true);
  try {
    IOUtils.copyLarge(stream, out);
  } finally {
    IOUtils.closeQuietly(out);
  }
}

代码示例来源:origin: stackoverflow.com

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
dir.mkdirs();
File file = new File(dir, "filename");

FileOutputStream f = new FileOutputStream(file);
...

代码示例来源:origin: Tencent/tinker

/**
 * create directory
 *
 * @param directoryPath
 */
public static void createDirectory(final String directoryPath) {
  File file = new File(directoryPath);
  if (!file.exists()) {
    file.setReadable(true, false);
    file.setWritable(true, true);
    file.mkdirs();
  }
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Rewrite log file.
 */
protected File getLogFile() {
  File base = getBaseDir();
  base.mkdirs();
  return new File(base,"log");
}

代码示例来源:origin: org.testng/testng

private void addOutputDir(List<String> argv) {
 if (null != m_outputDir) {
  if (!m_outputDir.exists()) {
   m_outputDir.mkdirs();
  }
  if (m_outputDir.isDirectory()) {
   argv.add(CommandLineArgs.OUTPUT_DIRECTORY);
   argv.add(m_outputDir.getAbsolutePath());
  } else {
   throw new BuildException("Output directory is not a directory: " + m_outputDir);
  }
 }
}

代码示例来源:origin: stackoverflow.com

String path = "C:" + File.separator + "hello" + File.separator + "hi.txt";
// Use relative path for Unix systems
File f = new File(path);

f.getParentFile().mkdirs(); 
f.createNewFile();

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

@Override
  protected void processDir (Entry entryDir, ArrayList<Entry> value) throws Exception {
    if (!entryDir.outputDir.exists()) {
      if (!entryDir.outputDir.mkdirs())
        throw new Exception("Couldn't create output directory '" + entryDir.outputDir + "'");
    }
  }
}

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

@Test
  public void shouldNotValidateNestingOfMaterialDirectoriesBasedOnServerSideFileSystem() throws IOException {
    final File workingDir = temporaryFolder.newFolder("go-working-dir");
    final File material1 = new File(workingDir, "material1");
    material1.mkdirs();

    final Path material2 = Files.createSymbolicLink(Paths.get(new File(workingDir, "material2").getPath()), Paths.get(material1.getPath()));

    pluggableSCMMaterialConfig.setFolder(material1.getAbsolutePath());
    pluggableSCMMaterialConfig.validateNotSubdirectoryOf(material2.toAbsolutePath().toString());

    assertNull(pluggableSCMMaterialConfig.errors().getAllOn(FOLDER));
  }
}

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

public String createTempFile(String fileName, String contents) throws IOException {
  File f = createAndRegisterTempFile(fileName);
  if (!f.getParentFile().exists()) {
    f.getParentFile().mkdirs();
  }
  f.createNewFile();
  FileUtils.writeFileUtf8(f, contents);
  return f.toURI().toString();
}

代码示例来源:origin: jenkinsci/jenkins

public void on() {
  try {
    file.getParentFile().mkdirs();
    Files.newOutputStream(file.toPath()).close();
    get();  // update state
  } catch (IOException | InvalidPathException e) {
    LOGGER.log(Level.WARNING, "Failed to touch "+file);
  }
}

相关文章

微信公众号

最新文章

更多