List of usage examples for java.nio.channels FileChannel transferTo
public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;
From source file:org.easysoa.registry.frascati.FileUtils.java
/** * Copy a file in an other file/* w ww. j a v a 2 s . c o m*/ * * @param source The source file * @param target The target file * @throws Exception If a problem occurs */ public static final void copyTo(File source, File target) throws Exception { if (source == null || target == null) { throw new IllegalArgumentException("Source and target files must not be null"); } // Input and outputs channels log.debug("source file = " + source); log.debug("target file = " + target); FileChannel in = null; FileChannel out = null; try { // Init in = new FileInputStream(source).getChannel(); out = new FileOutputStream(target).getChannel(); // File copy in.transferTo(0, in.size(), out); } catch (Exception ex) { throw ex; } finally { // finally close all streams if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } }
From source file:nz.govt.natlib.ndha.common.FileUtils.java
/** * Name is self explanatory/*from w w w. j av a 2s.c o m*/ * * @param from * @param to * @throws IOException */ public static void copyFile(File from, File to) throws IOException { FileChannel inChannel = new FileInputStream(from).getChannel(); FileChannel outChannel = new FileOutputStream(to).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { throw e; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
From source file:Main.java
public static File copyFile(File destinationFile, File sourceFile) throws IOException { FileInputStream inputStream = null; FileChannel inputChannel = null; FileOutputStream outputStream = null; FileChannel outputChannel = null; try {/*from w ww .j ava 2 s . c o m*/ inputStream = new FileInputStream(sourceFile); inputChannel = inputStream.getChannel(); outputStream = new FileOutputStream(destinationFile); outputChannel = outputStream.getChannel(); inputChannel.transferTo(0, inputChannel.size(), outputChannel); return destinationFile; } catch (IOException exception) { throw exception; } finally { if (inputChannel != null) { inputChannel.close(); } if (inputStream != null) { inputStream.close(); } if (outputChannel != null) { outputChannel.close(); } if (outputStream != null) { outputStream.close(); } } }
From source file:org.gnucash.android.export.ExporterTask.java
/** * Copies a file from <code>src</code> to <code>dst</code> * @param src Absolute path to the source file * @param dst Absolute path to the destination file * @throws IOException if the file could not be copied *//*from w ww. j a v a2 s . com*/ public static void copyFile(File src, File dst) throws IOException { //TODO: Make this asynchronous at some time, t in the future. FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
From source file:Main.java
public static void fastFileCopy(File source, File target) { FileChannel in = null; FileChannel out = null;//from w w w. j av a 2s . c o m long start = System.currentTimeMillis(); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(source); in = fis.getChannel(); fos = new FileOutputStream(target); out = fos.getChannel(); long size = in.size(); long transferred = in.transferTo(0, size, out); while (transferred != size) { transferred += in.transferTo(transferred, size - transferred, out); } } catch (IOException e) { e.printStackTrace(); } finally { close(fis); close(fos); close(in); close(out); } long end = System.currentTimeMillis(); Log.d(target.getAbsolutePath(), nf.format(source.length()) + "B: " + ((end - start > 0) ? nf.format(source.length() / (end - start)) : 0) + " KB/s"); }
From source file:ValidateLicenseHeaders.java
/** * Replace a legacy jboss header with the current default header * /*from ww w.j av a2 s. c om*/ * @param javaFile - * the java source file * @param endOfHeader - * the offset to the end of the legacy header * @throws IOException - * thrown on failure to replace the header */ static void replaceHeader(File javaFile, long endOfHeader) throws IOException { if (log.isLoggable(Level.FINE)) log.fine("Replacing legacy jboss header in: " + javaFile); RandomAccessFile raf = new RandomAccessFile(javaFile, "rw"); File bakFile = new File(javaFile.getAbsolutePath() + ".bak"); FileOutputStream fos = new FileOutputStream(bakFile); fos.write(DEFAULT_HEADER.getBytes()); FileChannel fc = raf.getChannel(); long count = raf.length() - endOfHeader; fc.transferTo(endOfHeader, count, fos.getChannel()); fc.close(); fos.close(); raf.close(); if (javaFile.delete() == false) log.severe("Failed to delete java file: " + javaFile); if (bakFile.renameTo(javaFile) == false) throw new SyncFailedException("Failed to replace: " + javaFile); }
From source file:org.nuclos.common2.IOUtils.java
/** * @param fileIn/*from www. j av a2s .c o m*/ * @param fileOut * @throws IOException */ public static void copyFile(File fileIn, File fileOut) throws IOException { FileChannel sourceChannel = null; FileChannel destinationChannel = null; try { sourceChannel = new FileInputStream(fileIn).getChannel(); destinationChannel = new FileOutputStream(fileOut).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); } finally { try { if (sourceChannel != null) { sourceChannel.close(); } } finally { if (destinationChannel != null) { destinationChannel.close(); } } } }
From source file:com.nridge.core.base.std.FilUtl.java
/** * Copies an input file to the output file. In an effort to promote * better performance, I/O channels are used for the operations. * * @param anInFile Represents the source file. * @param anOutFile Represents the destination file. * @throws IOException Related to stream building and read/write operations. *///from w ww . j av a 2 s .com static public void copy(File anInFile, File anOutFile) throws IOException { FileChannel srcChannel, dstChannel; srcChannel = new FileInputStream(anInFile).getChannel(); dstChannel = new FileOutputStream(anOutFile).getChannel(); srcChannel.transferTo(0, srcChannel.size(), dstChannel); srcChannel.close(); dstChannel.close(); }
From source file:Main.java
public static void copyFiles(String sourcePath, String targetPath) throws IOException { File srcDir = new File(sourcePath); File[] files = srcDir.listFiles(); FileChannel in = null; FileChannel out = null;// ww w . jav a 2 s. c om for (File file : files) { try { in = new FileInputStream(file).getChannel(); File outDir = new File(targetPath); if (!outDir.exists()) { outDir.mkdirs(); } File outFile = new File(targetPath + File.separator + file.getName()); out = new FileOutputStream(outFile).getChannel(); in.transferTo(0, in.size(), out); } finally { if (in != null) in.close(); if (out != null) out.close(); } } }
From source file:dk.netarkivet.common.utils.StreamUtils.java
/** * Will copy everything from input stream to output stream, closing input * stream afterwards./*from w w w . jav a 2 s. c o m*/ * * @param in Inputstream to copy from * @param out Outputstream to copy to * @throws ArgumentNotValid if either parameter is null * @throws IOFailure if a read or write error happens during copy */ public static void copyInputStreamToOutputStream(InputStream in, OutputStream out) { ArgumentNotValid.checkNotNull(in, "InputStream in"); ArgumentNotValid.checkNotNull(out, "OutputStream out"); try { try { if (in instanceof FileInputStream && out instanceof FileOutputStream) { FileChannel inChannel = ((FileInputStream) in).getChannel(); FileChannel outChannel = ((FileOutputStream) out).getChannel(); long transferred = 0; final long fileLength = inChannel.size(); do { transferred += inChannel.transferTo(transferred, Math.min(Constants.IO_CHUNK_SIZE, fileLength - transferred), outChannel); } while (transferred < fileLength); } else { byte[] buf = new byte[Constants.IO_BUFFER_SIZE]; int bytesRead; while ((bytesRead = in.read(buf)) != -1) { out.write(buf, 0, bytesRead); } } out.flush(); } finally { in.close(); } } catch (IOException e) { String errMsg = "Trouble copying inputstream " + in + " to outputstream " + out; log.warn(errMsg, e); throw new IOFailure(errMsg, e); } }