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

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

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

File.getParentFile介绍

[英]Returns a new file made from the pathname of the parent of this file. This is the path up to but not including the last name. null is returned when there is no parent.
[中]返回由该文件父级的路径名生成的新文件。这是到达但不包括姓氏的路径。当没有父级时,返回null。

代码示例

代码示例来源: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: jenkinsci/jenkins

public CompressedFile(File file) {
  this.file = file;
  this.gz = new File(file.getParentFile(),file.getName()+".gz");
}

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

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

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

public static boolean isSymbolicLink(File parent, String name)
    throws IOException {
  if (parent == null) {
    File f = new File(name);
    parent = f.getParentFile();
    name = f.getName();
  }
  File toTest = new File(parent.getCanonicalPath(), name);
  return !toTest.getAbsolutePath().equals(toTest.getCanonicalPath());
}

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

public FileHandle parent () {
  File parent = file.getParentFile();
  if (parent == null) {
    if (type == FileType.Absolute)
      parent = new File("/");
    else
      parent = new File("");
  }
  return new HeadlessFileHandle(parent, type);
}

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

private static void parseClassPath(Manifest manifest, File archive, List<File> paths, String attributeName, String separator) throws IOException {
  String classPath = manifest.getMainAttributes().getValue(attributeName);
  if(classPath==null) return; // attribute not found
  for (String s : classPath.split(separator)) {
    File file = resolve(archive, s);
    if(file.getName().contains("*")) {
      // handle wildcard
      FileSet fs = new FileSet();
      File dir = file.getParentFile();
      fs.setDir(dir);
      fs.setIncludes(file.getName());
      for( String included : fs.getDirectoryScanner(new Project()).getIncludedFiles() ) {
        paths.add(new File(dir,included));
      }
    } else {
      if(!file.exists())
        throw new IOException("No such file: "+file);
      paths.add(file);
    }
  }
}

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

public AtomicFileOutputStream(File f) throws FileNotFoundException {
  // Code unfortunately must be duplicated below since we can't assign
  // anything
  // before calling super
  super(new FileOutputStream(new File(f.getParentFile(), f.getName()
      + TMP_EXTENSION)));
  origFile = f.getAbsoluteFile();
  tmpFile = new File(f.getParentFile(), f.getName() + TMP_EXTENSION)
      .getAbsoluteFile();
}

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

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

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

@Override
public void visit(File f, String relativePath) throws IOException {
  if (f.isFile()) {
    File target = new File(dest, relativePath);
    mkdirsE(target.getParentFile());
    Path targetPath = fileToPath(writing(target));
    exceptionEncountered = exceptionEncountered || !tryCopyWithAttributes(f, targetPath);
    if (exceptionEncountered) {
      Files.copy(fileToPath(f), targetPath, StandardCopyOption.REPLACE_EXISTING);
      if (!logMessageShown) {
        LOGGER.log(Level.INFO, 
          "JENKINS-52325: Jenkins failed to retain attributes when copying to {0}, so proceeding without attributes.", 
          dest.getAbsolutePath());
        logMessageShown = true;
      }
    }
    count.incrementAndGet();
  }
}
private boolean tryCopyWithAttributes(File f, Path targetPath) {

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

void openOutputStream_noParent(final boolean createFile) throws Exception {
  final File file = new File("test.txt");
  assertNull(file.getParentFile());
  try {
    if (createFile) {
      TestUtils.createLineBasedFile(file, new String[]{"Hello"});
    }
    try (FileOutputStream out = FileUtils.openOutputStream(file)) {
      out.write(0);
    }
    assertTrue(file.exists());
  } finally {
    if (!file.delete()) {
      file.deleteOnExit();
    }
  }
}

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

private static boolean createOrExistsFile(final File file) {
  if (file == null) return false;
  if (file.exists()) return file.isFile();
  if (!createOrExistsDir(file.getParentFile())) return false;
  try {
    return file.createNewFile();
  } catch (IOException e) {
    e.printStackTrace();
    return false;
  }
}

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

public static void createLineBasedFile(final File file, final String[] data) throws IOException {
  if (file.getParentFile() != null && !file.getParentFile().exists()) {
    throw new IOException("Cannot create file " + file + " as the parent directory does not exist");
  }
  try (final PrintWriter output = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
    for (final String element : data) {
      output.println(element);
    }
  }
}

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

@Before
public void setUp() throws IOException
{
 testDir = temporaryFolder.newFolder("testDir");
 testFile = new File(testDir, "test.dat");
 try (OutputStream outputStream = new FileOutputStream(testFile)) {
  outputStream.write(StringUtils.toUtf8(content));
 }
 Assert.assertTrue(testFile.getParentFile().equals(testDir));
}

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

public GraphDatabaseService newImpermanentDatabase( File storeDir )
{
  File absoluteDirectory = storeDir.getAbsoluteFile();
  GraphDatabaseBuilder databaseBuilder = newImpermanentDatabaseBuilder( absoluteDirectory );
  databaseBuilder.setConfig( GraphDatabaseSettings.active_database, absoluteDirectory.getName() );
  databaseBuilder.setConfig( GraphDatabaseSettings.databases_root_path, absoluteDirectory.getParentFile().getAbsolutePath() );
  return databaseBuilder.newGraphDatabase();
}

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

@Override
  public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
    symlinking(f);
    Util.createSymlink(f.getParentFile(), target, f.getName(), listener);
    return null;
  }
}

代码示例来源: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: 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);
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

private boolean checkFile(File file) {
 if (file.isFile()) {
  fileChooser.setCurrentDirectory(file.getParentFile());
  return true;
 } else {
  String message = "File Not Found: "+file.getAbsolutePath();
  displayError("File Not Found Error", message);
  return false;
 }
}

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

public static void copyFile(InputStream from, File to) throws IOException {
 if (! to.getParentFile().exists()) {
  to.getParentFile().mkdirs();
 }
 try (OutputStream os = new FileOutputStream(to)) {
  byte[] buffer = new byte[65536];
  int count = from.read(buffer);
  while (count > 0) {
   os.write(buffer, 0, count);
   count = from.read(buffer);
  }
 }
}

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

static public void main (String[] args) throws Exception {
    Settings settings = null;
    String input = null, output = null, packFileName = "pack.atlas";

    switch (args.length) {
    case 4:
      settings = new Json().fromJson(Settings.class, new FileReader(args[3]));
    case 3:
      packFileName = args[2];
    case 2:
      output = args[1];
    case 1:
      input = args[0];
      break;
    default:
      System.out.println("Usage: inputDir [outputDir] [packFileName] [settingsFileName]");
      System.exit(0);
    }

    if (output == null) {
      File inputFile = new File(input);
      output = new File(inputFile.getParentFile(), inputFile.getName() + "-packed").getAbsolutePath();
    }
    if (settings == null) settings = new Settings();

    process(settings, input, output, packFileName);
  }
}

相关文章