/** * 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); } } }
@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(); } }
@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(); } }
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); } }
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(); }
@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); } }
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()); }
public void testCopyToOutputStream() throws IOException { File i18nFile = getTestFile("i18n.txt"); ByteArrayOutputStream out = new ByteArrayOutputStream(); Files.copy(i18nFile, out); assertEquals(I18N, out.toString("UTF-8")); }
@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()); } }
@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()); } }
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)); }
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)); }
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)); }
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)); }
@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"); } }
@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"); }
@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); }
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)); }
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))); }
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)); }