java.nio.file.Path.toFile()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(129)

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

Path.toFile介绍

暂无

代码示例

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

private void createDestination( Path destination ) throws IOException
{
  if ( !destination.toFile().exists() )
  {
    Files.createDirectories( destination );
  }
}

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

@Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    Path relativePath = oldApkPath.relativize(file);
    Path newPath = newApkPath.resolve(relativePath);
    String patternKey = relativePath.toString().replace("\\", "/");
    if (Utils.checkFileInPattern(config.mResFilePattern, patternKey)) {
      //not contain in new path, is deleted
      if (!newPath.toFile().exists()) {
        deletedFiles.add(patternKey);
        writeResLog(newPath.toFile(), file.toFile(), TypedValue.DEL);
      }
      return FileVisitResult.CONTINUE;
    }
    return FileVisitResult.CONTINUE;
  }
}

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

public static void writePluginServices(Iterable<String> plugins, File root)
    throws IOException
{
  Path path = root.toPath().resolve(SERVICES_FILE);
  createDirectories(path.getParent());
  try (Writer out = new OutputStreamWriter(new FileOutputStream(path.toFile()), UTF_8)) {
    for (String plugin : plugins) {
      out.write(plugin + "\n");
    }
  }
}

代码示例来源:origin: twosigma/beakerx

private File getFile(String path) {
 File file = Paths.get(path).toFile();
 if (!file.exists()) {
  throw new RuntimeException("Path does not exist: " + path);
 }
 return file;
}

代码示例来源:origin: AdoptOpenJDK/jitwatch

public JavapProcess() throws FileNotFoundException
{
  super();
  Path javaHome = Paths.get(System.getProperty("java.home"));
  
  executablePath = Paths.get(javaHome.toString(), "..", "bin", EXECUTABLE_NAME);
  if (!executablePath.toFile().exists())
  {
    executablePath = Paths.get(javaHome.toString(), "bin", EXECUTABLE_NAME);
    if (!executablePath.toFile().exists())
    {
      throw new FileNotFoundException("Could not find " + EXECUTABLE_NAME);
    }
  }
  executablePath = executablePath.normalize();
}

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

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  Path relative = sourcePath.relativize(dir);
  Path target = targetPath.resolve(relative);
  File folder = target.toFile();
  if (!folder.exists()) {
    mkdirs(folder);
  }
  return FileVisitResult.CONTINUE;
}

代码示例来源:origin: jooby-project/jooby

@Override public File resolve(final CacheDescriptor item) {
  String version = item.getVersion()
    .replace("^", "")
    .replace("v", "");

  String classifier = Optional.ofNullable(item.getClassifier())
    .map(it -> "-" + it)
    .orElse("");
  Path artifactId = Paths.get(item.getName()).resolve(version)
    .resolve(item.getName() + "-" + version + classifier + "." + item.getExtension());
  return repository.resolve(groupId).resolve(artifactId).toAbsolutePath().toFile();
 }
}

代码示例来源:origin: ballerina-platform/ballerina-lang

private static void updateBallerinaPathModificationTracker(Project project, ProjectStatus status) {
  String balSdkPath = getBallerinaSdk(project);
  if (balSdkPath != null) {
    Path balxPath = Paths.get(balSdkPath, ballerinaSourcePath);
    if (balxPath.toFile().isDirectory()) {
      if (status == ProjectStatus.OPENED) {
        BallerinaPathModificationTracker.addPath(balxPath.toString());
      } else if (status == ProjectStatus.CLOSED) {
        BallerinaPathModificationTracker.removePath(balxPath.toString());
      }
    }
  }
}

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

@Override
public File[] listFiles( File directory, final FilenameFilter filter )
{
  try ( Stream<Path> listing = Files.list( path( directory ) ) )
  {
    return listing
        .filter( entry -> filter.accept( entry.getParent().toFile(), entry.getFileName().toString() ) )
        .map( Path::toFile )
        .toArray( File[]::new );
  }
  catch ( IOException e )
  {
    return null;
  }
}

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

@Override
 protected void cleanup(
   Context context
 ) throws IOException
 {
  final String tmpDirLoc = context.getConfiguration().get(TMP_FILE_LOC_KEY);
  final File tmpDir = Paths.get(tmpDirLoc).toFile();
  FileUtils.deleteDirectory(tmpDir);
  context.progress();
  context.setStatus("Clean");
 }
}

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

private Path createDownloadDir(String workDir, String fileUrl) {
 Path downloadDirPath = Paths.get(workDir, DOWNLOAD_DIR);
 File downloadDirFile = downloadDirPath.toFile();
 try {
  Log.info(String.format("Creating download dir %s", downloadDirFile.toPath().toString()));
  FileUtils.forceMkdir(downloadDirFile);
 } catch (IOException e) {
  throw new RuntimeException(String
    .format("Unable to create download location for archive: %s at %s", fileUrl, downloadDirPath.toString()));
 }
 Log.info(String.format("Created download dir %s", downloadDirFile.toPath().toString()));
 return downloadDirPath;
}

代码示例来源:origin: SonarSource/sonarqube

public boolean tryLock() {
 try {
  lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
  lockChannel = lockRandomAccessFile.getChannel();
  lockFile = lockChannel.tryLock(0, 1024, false);
  return lockFile != null;
 } catch (IOException e) {
  throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
 }
}

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

@Test
public void testMoveForgottenFiles() {
  Collection<File> files = allFiles.stream().map(Path::toFile).collect(Collectors.toList());
  forgottenFilesManager.moveForgottenFiles(
      new File(configuration.getDataFileLocation()), files);
  Path lostFoundDir =
      Paths.get(configuration.getDataFileLocation(), forgottenFilesManager.LOST_FOUND);
  Collection<File> movedFiles = FileUtils.listFiles(lostFoundDir.toFile(), null, false);
  Assert.assertEquals(allFiles.size(), movedFiles.size());
  for (Path file : allFiles)
    Assert.assertTrue(
        movedFiles.contains(
            Paths.get(lostFoundDir.toString(), file.getFileName().toString())
                .toFile()));
}

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

private Optional<Long> getPid()
  {
    Path pidFile = this.homeDir.resolve( "run/neo4j.pid" );
    if ( this.fs.fileExists( pidFile.toFile() ) )
    {
      // The file cannot be opened with write permissions on Windows
      try ( InputStream inputStream = Files.newInputStream( pidFile, StandardOpenOption.READ );
          BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) ) )
      {
        String pidFileContent = reader.readLine();
        try
        {
          return Optional.of( Long.parseLong( pidFileContent ) );
        }

        catch ( NumberFormatException e )
        {
          printError( pidFile.toString() + " does not contain a valid id. Found: " + pidFileContent );
        }
      }
      catch ( IOException e )
      {
        printError( "Error reading the .pid file. Reason: " + e.getMessage(), e );
      }
    }
    return Optional.empty();
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

private String createClassOutsideClasspath(String className) throws Exception {
 File dir = Files.createTempDirectory("vertx").toFile();
 dir.deleteOnExit();
 File source = new File(dir, className + ".java");
 Files.write(source.toPath(), ("public class " + className + " extends io.vertx.core.AbstractVerticle {} ").getBytes());
 URLClassLoader loader = new URLClassLoader(new URL[]{dir.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
 CompilingClassLoader compilingClassLoader = new CompilingClassLoader(loader, className + ".java");
 compilingClassLoader.loadClass(className);
 byte[] bytes = compilingClassLoader.getClassBytes(className);
 assertNotNull(bytes);
 File classFile = new File(dir, className + ".class");
 Files.write(classFile.toPath(), bytes);
 return dir.getAbsolutePath();
}

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

private void readVersion() throws IOException {
  Path versionFile = topologyBasicBlobsRootDir.resolve(type.getVersionFileName());
  if (!fsOps.fileExists(versionFile)) {
    version = NOT_DOWNLOADED_VERSION;
  } else {
    String ver = FileUtils.readFileToString(versionFile.toFile(), "UTF8").trim();
    version = Long.parseLong(ver);
  }
}

代码示例来源:origin: SonarSource/sonarqube

protected static File resolvePath(File baseDir, String path) {
 Path filePath = Paths.get(path);
 if (!filePath.isAbsolute()) {
  filePath = baseDir.toPath().resolve(path);
 }
 return filePath.normalize().toFile();
}

代码示例来源:origin: oracle/opengrok

@Override
public boolean fileHasHistory(File file) {
  String parentFile = file.getParent();
  String name = file.getName();
  File f = Paths.get(parentFile, "SCCS", "s." + name).toFile();
  return f.exists();
}

代码示例来源:origin: AdoptOpenJDK/jitwatch

public JavapProcess() throws FileNotFoundException
{
  super();
  Path javaHome = Paths.get(System.getProperty("java.home"));
  
  executablePath = Paths.get(javaHome.toString(), "..", "bin", EXECUTABLE_NAME);
  if (!executablePath.toFile().exists())
  {
    executablePath = Paths.get(javaHome.toString(), "bin", EXECUTABLE_NAME);
    if (!executablePath.toFile().exists())
    {
      throw new FileNotFoundException("Could not find " + EXECUTABLE_NAME);
    }
  }
  executablePath = executablePath.normalize();
}

代码示例来源:origin: SonarSource/sonarqube

private static void cleanTempFolders(Path path) throws IOException {
 if (path.toFile().exists()) {
  try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) {
   for (Path p : stream) {
    deleteQuietly(p.toFile());
   }
  }
 }
}

相关文章