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

x33g5p2x  于2022-01-17 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(428)

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

Files.copy介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    Files.copy(file, dest.resolve(src.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
    return FileVisitResult.CONTINUE;
  }
});

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

public static void copyFile( File srcFile, File dstFile ) throws IOException
{
  //noinspection ResultOfMethodCallIgnored
  dstFile.getParentFile().mkdirs();
  Files.copy( srcFile.toPath(), dstFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
}

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

long bytes = java.nio.file.Files.copy( 
        new java.io.File("<filepath1>").toPath(), 
        new java.io.File("<filepath2>").toPath(),
        java.nio.file.StandardCopyOption.REPLACE_EXISTING,
        java.nio.file.StandardCopyOption.COPY_ATTRIBUTES,
        java.nio.file.LinkOption.NOFOLLOW_LINKS);

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

private void copyCreationMetadataIfExists(File currentDir, File v3Dir)
  throws IOException {
 File v2CreationFile = new File(currentDir, V1Constants.SEGMENT_CREATION_META);
 if (v2CreationFile.exists()) {
  File v3CreationFile = new File(v3Dir, V1Constants.SEGMENT_CREATION_META);
  Files.copy(v2CreationFile.toPath(), v3CreationFile.toPath());
 }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

final File file = new File(FILE);
assertTrue("Log file does not exist", file.exists());
logger.debug("This is test message number 1");
Thread.sleep(2500);
final File dir = new File(DIR);
assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
  Files.copy(src, os);

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

private static void copyFile(final File input, final File destination) throws IOException {
  if (!input.exists()) {
    return;
  }
  Files.copy(input.toPath(), destination.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Test
public void testWatchManager() throws Exception {
  Assume.assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY));
  final ConfigurationScheduler scheduler = new ConfigurationScheduler();
  scheduler.incrementScheduledItems();
  final WatchManager watchManager = new WatchManager(scheduler);
  watchManager.setIntervalSeconds(1);
  scheduler.start();
  watchManager.start();
  try {
    final File sourceFile = new File(originalFile);
    Path source = Paths.get(sourceFile.toURI());
    try (final FileOutputStream targetStream = new FileOutputStream(testFile)) {
      Files.copy(source, targetStream);
    }
    final File updateFile = new File(newFile);
    final File targetFile = new File(testFile);
    final BlockingQueue<File> queue = new LinkedBlockingQueue<>();
    watchManager.watchFile(targetFile, new TestWatcher(queue));
    Thread.sleep(1000);
    source = Paths.get(updateFile.toURI());
    Files.copy(source, Paths.get(targetFile.toURI()), StandardCopyOption.REPLACE_EXISTING);
    Thread.sleep(1000);
    final File f = queue.poll(1, TimeUnit.SECONDS);
    assertNotNull("File change not detected", f);
  } finally {
    watchManager.stop();
    scheduler.stop();
  }
}

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

@Override
public void copyToLocalFile(URI srcUri, File dstFile)
  throws Exception {
 if (dstFile.exists()) {
  if (dstFile.isDirectory()) {
   FileUtils.deleteDirectory(dstFile);
  } else {
   FileUtils.deleteQuietly(dstFile);
  }
 }
 try (InputStream adlStream = _adlStoreClient.getReadStream(srcUri.getPath())) {
  Path dstFilePath = Paths.get(dstFile.toURI());
  /* Copy the source file to the destination directory as a file with the same name as the source,
   * replace an existing file with the same name in the destination directory, if any.
   * Set new file permissions on the copied file.
   */
  Files.copy(adlStream, dstFilePath);
 } catch (Exception ex) {
  throw ex;
 }
}

代码示例来源:origin: ankidroid/Anki-Android

@Override
  public long copyFile(InputStream source, String target) throws IOException {
    return Files.copy(source, Paths.get(target), StandardCopyOption.REPLACE_EXISTING);
  }
}

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

static void init() throws Exception {
  jarDir = TMP.newFolder().toPath();
  // properties are set property by surefire plugin
  final String parameterProgramJarName = System.getProperty("parameterJarName") + ".jar";
  final String parameterProgramWithoutManifestJarName = System.getProperty("parameterJarWithoutManifestName") + ".jar";
  final Path jarLocation = Paths.get(System.getProperty("targetDir"));
  jarWithManifest = Files.copy(
    jarLocation.resolve(parameterProgramJarName),
    jarDir.resolve("program-with-manifest.jar"));
  jarWithoutManifest = Files.copy(
    jarLocation.resolve(parameterProgramWithoutManifestJarName),
    jarDir.resolve("program-without-manifest.jar"));
  restfulGateway = new TestingDispatcherGateway.Builder()
    .setBlobServerPort(BLOB_SERVER_RESOURCE.getBlobServerPort())
    .setSubmitFunction(jobGraph -> {
      LAST_SUBMITTED_JOB_GRAPH_REFERENCE.set(jobGraph);
      return CompletableFuture.completedFuture(Acknowledge.get());
    })
    .build();
  gatewayRetriever = () -> CompletableFuture.completedFuture(restfulGateway);
  localAddressFuture = CompletableFuture.completedFuture("shazam://localhost:12345");
  timeout = Time.seconds(10);
  responseHeaders = Collections.emptyMap();
  executor = TestingUtils.defaultExecutor();
}

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

/**
 * Copy file to working directory.
 *
 * @param filePath File path.
 * @return File name.
 * @throws IOException If coping failed.
 */
String copyToWorkDir(String filePath) throws IOException {
  Path srcFile = Paths.get(filePath);
  if (Files.exists(srcFile)) {
    checkDownloadFolder();
    Path newDir = Paths.get(downloadFolder);
    Path fileName = srcFile.getFileName();
    Files.copy(srcFile, newDir.resolve(fileName), StandardCopyOption.REPLACE_EXISTING);
    return fileName.toString();
  }
  return null;
}

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

void copyDiskInitFile(DiskStoreImpl diskStore) throws IOException {
 File diskInitFile = diskStore.getDiskInitFile().getIFFile();
 String subDirName = Integer.toString(diskStore.getInforFileDirIndex());
 Path subDir = temporaryFiles.getDirectory().resolve(subDirName);
 Files.createDirectories(subDir);
 Files.copy(diskInitFile.toPath(), subDir.resolve(diskInitFile.getName()),
   StandardCopyOption.COPY_ATTRIBUTES);
 backupDefinition.addDiskInitFile(diskStore, subDir.resolve(diskInitFile.getName()));
}

代码示例来源:origin: loklak/loklak_server

private static void extract(JarFile jar, JarEntry file) throws IOException {
  Path workingDirectory = Paths.get("").toAbsolutePath();
  File target = new File(workingDirectory.toString() + File.separator + file.getName());
  if (target.exists())
    return;
  if (file.isDirectory()) {
    target.mkdir();
    return;
  }
  Files.copy(jar.getInputStream(file), target.toPath());
}

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

private void createTempLauncherJar() {
  try {
    inUseLauncher.getParentFile().mkdirs();
    Files.copy(new File(Downloader.AGENT_LAUNCHER).toPath(), inUseLauncher.toPath());
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  LauncherTempFileHandler.startTempFileReaper();
}

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

@Override
  public Void invoke(File f, VirtualChannel channel) throws IOException {
    File targetFile = new File(target.remote);
    File targetDir = targetFile.getParentFile();
    filterNonNull().mkdirs(targetDir);
    Files.createDirectories(fileToPath(targetDir));
    Files.copy(fileToPath(reading(f)), fileToPath(writing(targetFile)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
    return null;
  }
}

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

private Optional<File> compressJar(PluginInfo plugin, File jar) {
 if (!configuration.getBoolean(PROPERTY_PLUGIN_COMPRESSION_ENABLE).orElse(false)) {
  return Optional.empty();
 }
 Path targetPack200 = getPack200Path(jar.toPath());
 Path sourcePack200Path = getPack200Path(plugin.getNonNullJarFile().toPath());
 // check if packed file was deployed alongside the jar. If that's the case, use it instead of generating it (SONAR-10395).
 if (sourcePack200Path.toFile().exists()) {
  try {
   LOG.debug("Found pack200: " + sourcePack200Path);
   Files.copy(sourcePack200Path, targetPack200);
  } catch (IOException e) {
   throw new IllegalStateException("Failed to copy pack200 file from " + sourcePack200Path + " to " + targetPack200, e);
  }
 } else {
  pack200(jar.toPath(), targetPack200, plugin.getKey());
 }
 return Optional.of(targetPack200.toFile());
}

代码示例来源:origin: ankidroid/Anki-Android

@Override
public long copyFile(String source, OutputStream target) throws IOException {
  return Files.copy(Paths.get(source), target);
}

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

+ "deine Boten, Herr, verehren Das sanfte Wandeln deines Tags.";
final java.nio.file.Path compressDir = tmp.newFolder("compressDir").toPath();
final java.nio.file.Path extractDir = tmp.newFolder("extractDir").toPath();
final java.nio.file.Path originalDir = Paths.get("rootDir");
final java.nio.file.Path emptySubDir = originalDir.resolve("emptyDir");
final java.nio.file.Path fullSubDir = originalDir.resolve("fullDir");
final java.nio.file.Path file1 = originalDir.resolve("file1");
final java.nio.file.Path file2 = originalDir.resolve("file2");
final java.nio.file.Path file3 = fullSubDir.resolve("file3");
Files.createDirectory(compressDir.resolve(emptySubDir));
Files.createDirectory(compressDir.resolve(fullSubDir));
Files.copy(new ByteArrayInputStream(testFileContent.getBytes(StandardCharsets.UTF_8)), compressDir.resolve(file1));
Files.createFile(compressDir.resolve(file2));
Files.copy(new ByteArrayInputStream(testFileContent.getBytes(StandardCharsets.UTF_8)), compressDir.resolve(file3));

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

@Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    Files.copy(file, dest.resolve(src.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
    return FileVisitResult.CONTINUE;
  }
});

相关文章

微信公众号

最新文章

更多