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:Main.java
public static void copyFile(File src, File dest) throws IOException { FileOutputStream fileOut = null; FileInputStream fileIn = null; try {/*from w w w .ja v a 2 s . com*/ fileOut = new FileOutputStream(dest); fileIn = new FileInputStream(src); FileChannel inChannel = fileIn.getChannel(); FileChannel outChannel = fileOut.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (fileIn != null) { fileIn.close(); } if (fileOut != null) { fileOut.close(); } } }
From source file:Main.java
public static void copyFile(File src, File dst) throws IOException { FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dst); FileChannel inChannel = in.getChannel(); FileChannel outChannel = out.getChannel(); try {/*from ww w . j a va2 s .com*/ inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } outChannel.close(); } in.close(); out.close(); }
From source file:Main.java
public static ByteBuffer readBytes(String file) throws IOException { ByteArrayOutputStream dataOut = new ByteArrayOutputStream(); FileChannel fChannel = new RandomAccessFile(file, "r").getChannel(); fChannel.transferTo(0, fChannel.size(), Channels.newChannel(dataOut)); fChannel.close();/*from w ww. java2s .co m*/ return ByteBuffer.wrap(dataOut.toByteArray()); }
From source file:Main.java
public static void copy(File src, File dst) throws IOException { FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close();//from www . j a va 2s .c om outStream.flush(); outStream.close(); }
From source file:Main.java
public static void copyCompletely(InputStream input, OutputStream output) throws IOException { // if both are file streams, use channel IO if ((output instanceof FileOutputStream) && (input instanceof FileInputStream)) { try {//from www.jav a 2 s . co m FileChannel target = ((FileOutputStream) output).getChannel(); FileChannel source = ((FileInputStream) input).getChannel(); source.transferTo(0, Integer.MAX_VALUE, target); source.close(); target.close(); return; } catch (Exception e) { /* failover to byte stream version */ } } byte[] buf = new byte[8192]; while (true) { int length = input.read(buf); if (length < 0) break; output.write(buf, 0, length); } try { input.close(); } catch (IOException ignore) { } try { output.close(); } catch (IOException ignore) { } }
From source file:Main.java
/** * Copies the contents of one file to the other using {@link FileChannel}s. * /*from w ww. j av a2s . c o m*/ * @param src * source {@link File} * @param dst * destination {@link File} */ public static void copyFile(File src, File dst) throws IOException { FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dst); FileChannel inChannel = in.getChannel(); FileChannel outChannel = out.getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } in.close(); out.close(); }
From source file:org.geomajas.plugin.deskmanager.utility.FileUtils.java
/** * Zip all files in a directory, only relative filenames are used, does not recurse. * /* w ww . j a va2s . com*/ * @param sourceFolder * @param targetFileName * @return * @throws IOException */ public static File zipDirectory(File sourceFolder, String targetFileName) throws IOException { File target = new File(sourceFolder, targetFileName + (targetFileName.endsWith(".zip") ? "" : ".zip")); if (target.isFile()) { throw new IOException("Bestand bestaat reeds! " + target.getAbsolutePath()); } File[] files = sourceFolder.listFiles(); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(target)); WritableByteChannel out = Channels.newChannel(zos); FileInputStream is = null; try { for (File file : files) { zos.putNextEntry(new ZipEntry(file.getName())); is = new FileInputStream(file); FileChannel in = is.getChannel(); in.transferTo(0, in.size(), out); is.close(); } } catch (IOException e) { throw e; } finally { if (is != null) { is.close(); } if (zos != null) { zos.close(); } } return target; }
From source file:Main.java
private static void copyFile(File src, File dst) throws IOException { FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dst); FileChannel fromChannel = null, toChannel = null; try {//from w ww. j a v a 2 s .c om fromChannel = in.getChannel(); toChannel = out.getChannel(); fromChannel.transferTo(0, fromChannel.size(), toChannel); } finally { if (fromChannel != null) fromChannel.close(); if (toChannel != null) toChannel.close(); } }
From source file:Main.java
public static void copyFdToFile(FileDescriptor src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try {//from www. j a v a2s.c om inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } }
From source file:Main.java
private static void copyFile(File src, File dst) throws Exception { FileChannel inChannel = (new FileInputStream(src)).getChannel(); FileChannel outChannel = (new FileOutputStream(dst)).getChannel(); try {/*w w w . j a v a 2s. c om*/ inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }