List of usage examples for java.nio.channels FileChannel transferFrom
public abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException;
From source file:Main.java
/** * Copy a file by using the file channels, it could be 10x faster than the traditional way of * copying file by using the file streams. * // w ww . j av a2s.c om * @param src the source file * @param dest the destination file * @throws IOException */ public static void copyFileUsingFileChannels(File src, File dest) throws IOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputChannel = new FileInputStream(src).getChannel(); outputChannel = new FileOutputStream(dest).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { inputChannel.close(); outputChannel.close(); } }
From source file:com.bellman.bible.service.common.FileManager.java
public static boolean copyFile(File fromFile, File toFile) { boolean ok = false; try {// w w w . j a v a 2 s . com // don't worry if tofile exists, allow overwrite if (fromFile.exists()) { //ensure the target dir exists or FileNotFoundException is thrown creating dst FileChannel File toDir = toFile.getParentFile(); toDir.mkdir(); long fromFileSize = fromFile.length(); log.debug("Source file length:" + fromFileSize); if (fromFileSize > CommonUtils.getFreeSpace(toDir.getPath())) { // not enough room on SDcard ok = false; } else { // move the file FileInputStream srcStream = new FileInputStream(fromFile); FileChannel src = srcStream.getChannel(); FileOutputStream dstStream = new FileOutputStream(toFile); FileChannel dst = dstStream.getChannel(); try { dst.transferFrom(src, 0, src.size()); ok = true; } finally { src.close(); dst.close(); srcStream.close(); dstStream.close(); } } } else { // fromfile does not exist ok = false; } } catch (Exception e) { log.error("Error moving file to sd card", e); } return ok; }
From source file:Main.java
public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();// w ww . j a v a2 s .co m } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:Main.java
public static void copyFile(File sourceFile, File destFile) throws IOException { new File(destFile.getParent()).mkdirs(); if (!destFile.exists()) { destFile.createNewFile();/*ww w. j a va 2 s .c o m*/ } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } catch (Exception e) { e.printStackTrace(); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:Main.java
public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.getParentFile().exists()) destFile.getParentFile().mkdirs(); if (!destFile.exists()) { destFile.createNewFile();/*w ww. ja v a 2s .c o m*/ } else { destFile.delete(); destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:Main.java
/** * Copy file using NOI//w w w .ja v a 2 s. c o m * * @param source * @param dest */ public static void copyFile(String source, String dest) throws IOException { File sourceFile = new File(source); File destFile = new File(dest); FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputChannel = new FileInputStream(sourceFile).getChannel(); outputChannel = new FileOutputStream(destFile).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { try { inputChannel.close(); outputChannel.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
/** * Copy a file./*from ww w . java 2s. c o m*/ * * @param sourceFile * The source * @param destDir * The destination directory * @throws IOException * Everything fails sometimes */ protected static void CopyFileToDir(File sourceFile, File destDir) throws IOException { File destFile = new File(destDir, sourceFile.getName()); if (!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:Main.java
public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();/*from w w w . j a va 2 s . c o m*/ } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { closeQuietly(source); closeQuietly(destination); } }
From source file:org.eclipse.thym.hybrid.test.TestUtils.java
public static File createTempFile(String fileName) throws IOException, FileNotFoundException { File f = new File(getTempDirectory(), fileName); f.createNewFile();//from w w w. ja va2s . c o m f.deleteOnExit(); FileOutputStream fout = null; FileChannel out = null; InputStream in = TestUtils.class.getResourceAsStream("/" + fileName); try { fout = new FileOutputStream(f); out = fout.getChannel(); out.transferFrom(Channels.newChannel(in), 0, Integer.MAX_VALUE); return f; } finally { if (out != null) out.close(); if (in != null) in.close(); if (fout != null) fout.close(); } }
From source file:org.wso2.carbon.esb.vfs.transport.test.ESBJAVA4679VFSPasswordSecurityTestCase.java
/** * Copy the given source file to the given destination * * @param sourceFile source file// www. j a va 2 s . co m * @param destFile destination file * @throws java.io.IOException */ public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(destFile); FileChannel source = fileInputStream.getChannel(); FileChannel destination = fileOutputStream.getChannel(); destination.transferFrom(source, 0, source.size()); } finally { IOUtils.closeQuietly(fileInputStream); IOUtils.closeQuietly(fileOutputStream); } }