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

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

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

Files.copy介绍

[英]Copies to a file all bytes from an InputStream supplied by a factory.
[中]将工厂提供的InputStream中的所有字节复制到文件中。

代码示例

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

/**
 * Moves a file from one path to another. This method can rename a file and/or move it to a
 * different directory. In either case {@code to} must be the target path for the file itself; not
 * just the new name for the file or the path to the new parent directory.
 *
 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#move}.
 *
 * @param from the source file
 * @param to the destination file
 * @throws IOException if an I/O error occurs
 * @throws IllegalArgumentException if {@code from.equals(to)}
 */
public static void move(File from, File to) throws IOException {
 checkNotNull(from);
 checkNotNull(to);
 checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to);
 if (!from.renameTo(to)) {
  copy(from, to);
  if (!from.delete()) {
   if (!to.delete()) {
    throw new IOException("Unable to delete " + to);
   }
   throw new IOException("Unable to delete " + from);
  }
 }
}

代码示例来源:origin: opentripplanner/OpenTripPlanner

@Override
  public void write(OutputStream outStream) throws IOException {
    ZipOutputStream zip = new ZipOutputStream(outStream);
    for (File f : directory.listFiles()) {
      zip.putNextEntry(new ZipEntry(f.getName()));
      Files.copy(f, zip);
      zip.closeEntry();
      zip.flush();
    }
    zip.close();
  }
}

代码示例来源:origin: opentripplanner/OpenTripPlanner

@Override
  public void write(OutputStream outStream) throws IOException {
    ZipOutputStream zip = new ZipOutputStream(outStream);
    for (File f : directory.listFiles()) {
      zip.putNextEntry(new ZipEntry(f.getName()));
      Files.copy(f, zip);
      zip.closeEntry();
      zip.flush();
    }
    zip.close();
  }
}

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

private void addLocalFile(String src, String destDir) {
  try {
    File srcFile = new File(src);
    File destDirFile = null;
    if (!StringUtils.isEmpty(destDir)) {
      destDirFile = new File(exportDir, destDir);
      FileUtils.forceMkdir(destDirFile);
    } else {
      destDirFile = exportDir;
    }
    FileUtils.forceMkdir(destDirFile);
    Files.copy(srcFile, new File(destDirFile, srcFile.getName()));
  } catch (Exception e) {
    logger.warn("Failed to copy " + src + ".", e);
  }
}

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

private static URL makeJarUrlWithName(String name) throws IOException {
 File fullPath = new File(Files.createTempDir(), name);
 File jarFile = JarFileFinder.pickAnyJarFile();
 Files.copy(jarFile, fullPath);
 return fullPath.toURI().toURL();
}

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

@Override
  public void downloadBackup(Exhibitor exhibitor, BackupMetaData backup, OutputStream destination, Map<String, String> configValues) throws Exception
  {
    File        directory = new File(configValues.get(CONFIG_DIRECTORY.getKey()));
    File        nameDirectory = new File(directory, backup.getName());
    File        source = new File(nameDirectory, Long.toString(backup.getModifiedDate()));
    Files.copy(source, destination);
  }
}

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

public void testCopyToAppendable() throws IOException {
 File i18nFile = getTestFile("i18n.txt");
 StringBuilder sb = new StringBuilder();
 Files.copy(i18nFile, Charsets.UTF_8, sb);
 assertEquals(I18N, sb.toString());
}

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

public void testCopyToOutputStream() throws IOException {
 File i18nFile = getTestFile("i18n.txt");
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 Files.copy(i18nFile, out);
 assertEquals(I18N, out.toString("UTF-8"));
}

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

@Override
public void pushTaskLog(final String taskid, File file) throws IOException
{
 if (config.getDirectory().exists() || config.getDirectory().mkdirs()) {
  final File outputFile = fileForTask(taskid, file.getName());
  Files.copy(file, outputFile);
  log.info("Wrote task log to: %s", outputFile);
 } else {
  throw new IOE("Unable to create task log dir[%s]", config.getDirectory());
 }
}

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

@Override
public void pushTaskReports(String taskid, File reportFile) throws IOException
{
 if (config.getDirectory().exists() || config.getDirectory().mkdirs()) {
  final File outputFile = fileForTask(taskid, reportFile.getName());
  Files.copy(reportFile, outputFile);
  log.info("Wrote task report to: %s", outputFile);
 } else {
  throw new IOE("Unable to create task report dir[%s]", config.getDirectory());
 }
}

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

public void testCopyIdenticalFiles() throws IOException {
 File temp1 = createTempFile();
 Files.write(ASCII, temp1, Charsets.UTF_8);
 File temp2 = createTempFile();
 Files.write(ASCII, temp2, Charsets.UTF_8);
 Files.copy(temp1, temp2);
 assertEquals(ASCII, Files.toString(temp1, Charsets.UTF_8));
}

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

public void testCopySameFile() throws IOException {
 File temp = createTempFile();
 Files.write(ASCII, temp, Charsets.UTF_8);
 try {
  Files.copy(temp, temp);
  fail("Expected an IAE to be thrown but wasn't");
 } catch (IllegalArgumentException expected) {
 }
 assertEquals(ASCII, Files.toString(temp, Charsets.UTF_8));
}

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

public void testCopyFile() throws IOException {
 File i18nFile = getTestFile("i18n.txt");
 File temp = createTempFile();
 Files.copy(i18nFile, temp);
 assertEquals(I18N, Files.toString(temp, Charsets.UTF_8));
}

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

public void testCopyEqualFiles() throws IOException {
 File temp1 = createTempFile();
 File temp2 = file(temp1.getPath());
 assertEquals(temp1, temp2);
 Files.write(ASCII, temp1, Charsets.UTF_8);
 try {
  Files.copy(temp1, temp2);
  fail("Expected an IAE to be thrown but wasn't");
 } catch (IllegalArgumentException expected) {
 }
 assertEquals(ASCII, Files.toString(temp1, Charsets.UTF_8));
}

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

@Test(groups = "fast")
  public void testLoadCatalogFromExternalFile() throws CatalogApiException, IOException, URISyntaxException {
    final File originFile = new File(Resources.getResource("SpyCarBasic.xml").toURI());
    final File destinationFile = new File(Files.createTempDir().toString() + "/SpyCarBasicRelocated.xml");
    destinationFile.deleteOnExit();
    Files.copy(originFile, destinationFile);
    final VersionedCatalog c = loader.loadDefaultCatalog(destinationFile.toURI().toString());
    Assert.assertEquals(c.getCatalogName(), "SpyCarBasic");
  }
}

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

@Test
public void dump() throws Exception {
  Files.copy(new File("src/main/resources/incr-restore-cassandra.yaml"), target);
  tuner.writeAllProperties(target.getAbsolutePath(), "your_host", "YourSeedProvider");
}

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

@Before
public void setup() throws IOException {
  config = new FakeConfiguration();
  dseConfig = new DseConfigStub();
  auditLogTunerYaml = new AuditLogTunerYaml(dseConfig);
  auditLogTunerLog4j = new AuditLogTunerLog4J(config, dseConfig);
  File targetDir = new File(config.getCassHome() + "/conf");
  if (!targetDir.exists()) targetDir.mkdirs();
  targetFile = new File(config.getCassHome() + AuditLogTunerLog4J.AUDIT_LOG_FILE);
  Files.copy(new File("src/test/resources/" + AuditLogTunerLog4J.AUDIT_LOG_FILE), targetFile);
}

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

public void testMove() throws IOException {
 File i18nFile = getTestFile("i18n.txt");
 File temp1 = createTempFile();
 File temp2 = createTempFile();
 Files.copy(i18nFile, temp1);
 moveHelper(true, temp1, temp2);
 assertTrue(Files.equal(temp2, i18nFile));
}

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

public void testEqual() throws IOException {
 File asciiFile = getTestFile("ascii.txt");
 File i18nFile = getTestFile("i18n.txt");
 assertFalse(Files.equal(asciiFile, i18nFile));
 assertTrue(Files.equal(asciiFile, asciiFile));
 File temp = createTempFile();
 Files.copy(asciiFile, temp);
 assertTrue(Files.equal(asciiFile, temp));
 Files.copy(i18nFile, temp);
 assertTrue(Files.equal(i18nFile, temp));
 Files.copy(asciiFile, temp);
 RandomAccessFile rf = new RandomAccessFile(temp, "rw");
 rf.writeByte(0);
 rf.close();
 assertEquals(asciiFile.length(), temp.length());
 assertFalse(Files.equal(asciiFile, temp));
 assertTrue(Files.asByteSource(asciiFile).contentEquals(Files.asByteSource(asciiFile)));
 // 0-length files have special treatment (/proc, etc.)
 assertTrue(Files.equal(asciiFile, new BadLengthFile(asciiFile, 0)));
}

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

public void testMoveViaCopy() throws IOException {
 File i18nFile = getTestFile("i18n.txt");
 File temp1 = createTempFile();
 File temp2 = createTempFile();
 Files.copy(i18nFile, temp1);
 moveHelper(true, new UnmovableFile(temp1, false, true), temp2);
 assertTrue(Files.equal(temp2, i18nFile));
}

相关文章