List of usage examples for java.nio.channels FileChannel size
public abstract long size() throws IOException;
From source file:MainClass.java
private static void test() throws Exception { long[] primes = new long[] { 1, 2, 3, 5, 7 }; File aFile = new File("C:/test/primes.txt"); FileOutputStream outputFile = null; outputFile = new FileOutputStream(aFile); FileChannel file = outputFile.getChannel(); ByteBuffer[] buffers = new ByteBuffer[3]; buffers[0] = ByteBuffer.allocate(8); buffers[2] = ByteBuffer.allocate(8); String primeStr = null;//from ww w . j a v a 2 s.c om for (long prime : primes) { primeStr = "prime = " + prime; buffers[0].putDouble((double) primeStr.length()).flip(); buffers[1] = ByteBuffer.allocate(primeStr.length()); buffers[1].put(primeStr.getBytes()).flip(); buffers[2].putLong(prime).flip(); file.write(buffers); buffers[0].clear(); buffers[2].clear(); } System.out.println("File written is " + file.size() + "bytes."); outputFile.close(); }
From source file:edu.mit.csail.sdg.alloy4.Terminal.java
public static void copyFile(File sourceFile, File destFile) throws IOException { if (!sourceFile.exists()) { return;//from w w w .j a va 2s. c om } 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:it.isislab.dmason.util.SystemManagement.Worker.Updater.java
public static void copyFile(File sfile, File dfile) throws Exception { FileChannel source = new FileInputStream(sfile).getChannel(); FileChannel dest = new FileOutputStream(dfile).getChannel(); source.transferTo(0, source.size(), dest); source.close();//from w w w.ja v a 2 s . co m dest.close(); }
From source file:com.bjorsond.android.timeline.utilities.Utilities.java
public static void copyFile(String fromFile, String toPath, String toFilename) { System.out.println("COPY!"); if (Environment.getExternalStorageState().equals("mounted")) { File sdCardDirectory = Environment.getExternalStorageDirectory(); try {//from w ww . j a v a2 s .c om if (sdCardDirectory.canWrite()) { File destinationDirectory = new File(toPath); File sourceFile = new File(fromFile); File destinationFile = new File(destinationDirectory, toFilename); if (!destinationDirectory.exists()) { destinationDirectory.mkdirs(); } FileChannel source = new FileInputStream(sourceFile).getChannel(); FileChannel destination = new FileOutputStream(destinationFile).getChannel(); destination.transferFrom(source, 0, source.size()); source.close(); destination.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
From source file:com.alibaba.otter.shared.common.utils.NioUtils.java
/** * ??copy/* w w w . j av a 2 s. co m*/ */ public static long copy(InputStream input, OutputStream output) throws IOException { long count = 0; long n = 0; if (input instanceof FileInputStream) { FileChannel inChannel = ((FileInputStream) input).getChannel(); WritableByteChannel outChannel = Channels.newChannel(output); count = inChannel.transferTo(0, inChannel.size(), outChannel); } else if (output instanceof FileOutputStream) { FileChannel outChannel = ((FileOutputStream) output).getChannel(); ReadableByteChannel inChannel = Channels.newChannel(input); do { n = outChannel.transferFrom(inChannel, count, DEFAULT_BUFFER_SIZE); count += n; } while (n > 0); } else { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, (int) n); count += n; } // ReadableByteChannel inChannel = Channels.newChannel(input); // WritableByteChannel outChannel = Channels.newChannel(output); // // //ByteBuffer buffer = new ByteBuffer(DEFAULT_BUFFER_SIZE); // ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE); // while (-1 != (n = inChannel.read(buffer))) { // outChannel.write(buffer); // count += n; // } } return count; }
From source file:es.uvigo.darwin.jmodeltest.io.HtmlReporter.java
@SuppressWarnings("resource") public static void copyFile(File in, File out) throws IOException { FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try {/* w w w. j ava 2 s . c o m*/ 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:org.yamj.core.service.file.tools.FileTools.java
/** * Copy the source file to the destination * * @param src//from www. j a v a 2 s .c o m * @param dst * @return */ public static boolean copyFile(File src, File dst) { boolean returnValue = Boolean.FALSE; if (!src.exists()) { LOG.error("The file '{}' does not exist", src); return returnValue; } if (dst.isDirectory()) { makeDirectories(dst); returnValue = copyFile(src, new File(dst + File.separator + src.getName())); } else { FileInputStream inSource = null; FileOutputStream outSource = null; FileChannel inChannel = null; FileChannel outChannel = null; try { // gc: copy using file channels, potentially much faster inSource = new FileInputStream(src); outSource = new FileOutputStream(dst); inChannel = inSource.getChannel(); outChannel = outSource.getChannel(); long p = 0, s = inChannel.size(); while (p < s) { p += inChannel.transferTo(p, 1024 * 1024, outChannel); } return Boolean.TRUE; } catch (IOException error) { LOG.error("Failed copying file '{}' to '{}'", src, dst); LOG.error("File copying error", error); returnValue = Boolean.FALSE; } finally { if (inChannel != null) { try { inChannel.close(); } catch (IOException ex) { // Ignore } } if (inSource != null) { try { inSource.close(); } catch (IOException ex) { // Ignore } } if (outChannel != null) { try { outChannel.close(); } catch (IOException ex) { // Ignore } } if (outSource != null) { try { outSource.close(); } catch (IOException ex) { // Ignore } } } } return returnValue; }
From source file:com.alibaba.otter.shared.common.utils.NioUtils.java
/** * ??copy/* w ww.j a v a 2 s .co m*/ */ public static long copy(InputStream input, OutputStream output, long offset) throws IOException { long count = 0; long n = 0; if (input instanceof FileInputStream) { FileChannel inChannel = ((FileInputStream) input).getChannel(); WritableByteChannel outChannel = Channels.newChannel(output); count = inChannel.transferTo(offset, inChannel.size() - offset, outChannel); } else if (output instanceof FileOutputStream) { FileChannel outChannel = ((FileOutputStream) output).getChannel(); ReadableByteChannel inChannel = Channels.newChannel(input); do { n = outChannel.transferFrom(inChannel, offset + count, DEFAULT_BUFFER_SIZE); count += n; } while (n > 0); } else { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; input.skip(offset); while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, (int) n); count += n; } // ReadableByteChannel inChannel = Channels.newChannel(input); // WritableByteChannel outChannel = Channels.newChannel(output); // // //ByteBuffer buffer = new ByteBuffer(DEFAULT_BUFFER_SIZE); // ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE); // while (-1 != (n = inChannel.read(buffer))) { // outChannel.write(buffer); // count += n; // } } return count; }
From source file:com.arc.embeddedcdt.gui.jtag.ConfigJTAGTab.java
public static byte[] read(File file) { try {//www . ja va 2 s.c o m FileInputStream fis = new FileInputStream(file); FileChannel fc = fis.getChannel(); byte[] data = new byte[(int) fc.size()]; // fc.size returns the // size of the file // which backs the // channel ByteBuffer bb = ByteBuffer.wrap(data); fc.read(bb); return data; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:maspack.fileutil.SafeFileUtils.java
/** * Internal copy file method./*from ww w . j av a 2 s. c o m*/ * * @param srcFile * the validated source file, must not be {@code null} * @param destFile * the validated destination file, must not be {@code null} * @param options * determine whether to use a file lock, and preserve date information * @throws IOException * if an error occurs */ private static void doCopyFile(File srcFile, File destFile, int options) throws IOException { if (destFile.exists() && destFile.isDirectory()) { throw new IOException("Destination '" + destFile + "' exists but is a directory"); } File lockFile = new File(destFile.getAbsolutePath() + LOCK_EXTENSION); FileInputStream fis = null; FileOutputStream fos = null; FileChannel input = null; FileChannel output = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); input = fis.getChannel(); output = fos.getChannel(); long size = input.size(); long pos = 0; long count = 0; // Create lock before starting transfer // NOTE: we are purposely not using the Java NIO FileLock, because that // is automatically removed when the JVM exits. We want this file to // persist to inform the system the transfer was never completed. if ((options & LOCK_FILE) != 0) { if (lockFile.exists()) { // if we are not cleaning old locks, throw error if ((options & CLEAN_LOCK) == 0) { throw new IOException( "Lock file exists, preventing a write to " + destFile.getAbsolutePath() + ". Delete " + lockFile.getName() + " or set the CLEAN_LOCK option flag"); } } else { lockFile.createNewFile(); // will always return true or throw // error } } while (pos < size) { count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos; pos += output.transferFrom(input, pos, count); } } finally { closeQuietly(output); closeQuietly(fos); closeQuietly(input); closeQuietly(fis); } if (srcFile.length() != destFile.length()) { throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'"); } if ((options & PRESERVE_DATE) != 0) { destFile.setLastModified(srcFile.lastModified()); } // successful copy, delete lock file deleteQuietly(lockFile); }