List of usage examples for java.nio.channels FileChannel size
public abstract long size() throws IOException;
From source file:padl.creator.cppfile.eclipse.plugin.internal.Utils.java
static void copyFile(final File sourceFile, final File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();// w ww . ja v a 2 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:li.barter.utils.Utils.java
public static boolean copyFile(final File src, final File dst) { boolean returnValue = true; FileChannel inChannel = null, outChannel = null; try {//from ww w . j a v a 2 s . co m inChannel = new FileInputStream(src).getChannel(); outChannel = new FileOutputStream(dst).getChannel(); } catch (final FileNotFoundException fnfe) { Logger.d(TAG, "inChannel/outChannel FileNotFoundException"); fnfe.printStackTrace(); return false; } try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (final IllegalArgumentException iae) { Logger.d(TAG, "TransferTo IllegalArgumentException"); iae.printStackTrace(); returnValue = false; } catch (final NonReadableChannelException nrce) { Logger.d(TAG, "TransferTo NonReadableChannelException"); nrce.printStackTrace(); returnValue = false; } catch (final NonWritableChannelException nwce) { Logger.d(TAG, "TransferTo NonWritableChannelException"); nwce.printStackTrace(); returnValue = false; } catch (final ClosedByInterruptException cie) { Logger.d(TAG, "TransferTo ClosedByInterruptException"); cie.printStackTrace(); returnValue = false; } catch (final AsynchronousCloseException ace) { Logger.d(TAG, "TransferTo AsynchronousCloseException"); ace.printStackTrace(); returnValue = false; } catch (final ClosedChannelException cce) { Logger.d(TAG, "TransferTo ClosedChannelException"); cce.printStackTrace(); returnValue = false; } catch (final IOException ioe) { Logger.d(TAG, "TransferTo IOException"); ioe.printStackTrace(); returnValue = false; } finally { if (inChannel != null) { try { inChannel.close(); } catch (final IOException e) { e.printStackTrace(); } } if (outChannel != null) { try { outChannel.close(); } catch (final IOException e) { e.printStackTrace(); } } } return returnValue; }
From source file:HSqlPrimerDesign.java
@Deprecated public static String readFile(String file) throws IOException { FileChannel inChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); char ch;// w w w . j a va2 s. c o m StringBuffer line = new StringBuffer(); for (int i = 0; i < buffer.limit(); i++) { ch = ((char) buffer.get()); line.append(ch); } return line.toString(); }
From source file:bridge.toolkit.commands.S1000DConverter.java
/** * copy file content// w w w .j a v a2 s.co m * * @param from_ * String * @param to_ * String * @throws Exception */ public static void copy(String from_, String to_) throws Exception { try { FileChannel sourceChannel = new FileInputStream(from_).getChannel(); FileChannel destinationChannel = new FileOutputStream(to_).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); sourceChannel.close(); destinationChannel.close(); } catch (Exception ex) { throw (ex); } }
From source file:com.hipu.bdb.util.FileUtils.java
/** * Copy up to extent bytes of the source file to the destination *// ww w . ja v a 2 s .co m * @param src * @param dest * @param extent Maximum number of bytes to copy * @param overwrite If target file already exits, and this parameter is * true, overwrite target file (We do this by first deleting the target * file before we begin the copy). * @return True if the extent was greater than actual bytes copied. * @throws FileNotFoundException * @throws IOException */ public static boolean copyFile(final File src, final File dest, long extent, final boolean overwrite) throws FileNotFoundException, IOException { boolean result = false; if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Copying file " + src + " to " + dest + " extent " + extent + " exists " + dest.exists()); } if (dest.exists()) { if (overwrite) { dest.delete(); LOGGER.finer(dest.getAbsolutePath() + " removed before copy."); } else { // Already in place and we're not to overwrite. Return. return result; } } FileInputStream fis = null; FileOutputStream fos = null; FileChannel fcin = null; FileChannel fcout = null; try { // Get channels fis = new FileInputStream(src); fos = new FileOutputStream(dest); fcin = fis.getChannel(); fcout = fos.getChannel(); if (extent < 0) { extent = fcin.size(); } // Do the file copy long trans = fcin.transferTo(0, extent, fcout); if (trans < extent) { result = false; } result = true; } catch (IOException e) { // Add more info to the exception. Preserve old stacktrace. // We get 'Invalid argument' on some file copies. See // http://intellij.net/forums/thread.jsp?forum=13&thread=63027&message=853123 // for related issue. String message = "Copying " + src.getAbsolutePath() + " to " + dest.getAbsolutePath() + " with extent " + extent + " got IOE: " + e.getMessage(); if ((e instanceof ClosedByInterruptException) || ((e.getMessage() != null) && e.getMessage().equals("Invalid argument"))) { LOGGER.severe("Failed copy, trying workaround: " + message); workaroundCopyFile(src, dest); } else { IOException newE = new IOException(message); newE.initCause(e); throw newE; } } finally { // finish up if (fcin != null) { fcin.close(); } if (fcout != null) { fcout.close(); } if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } return result; }
From source file:org.mule.util.FileUtils.java
/** * Internal copy file method./* ww w . j a v a 2s. c om*/ * * @param srcFile the validated source file, must not be <code>null</code> * @param destFile the validated destination file, must not be <code>null</code> * @param preserveFileDate whether to preserve the file date * @throws IOException if an error occurs */ private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException { if (destFile.exists() && destFile.isDirectory()) { throw new IOException("Destination '" + destFile + "' exists but is a directory"); } FileChannel input = new FileInputStream(srcFile).getChannel(); try { FileChannel output = new FileOutputStream(destFile).getChannel(); try { output.transferFrom(input, 0, input.size()); } finally { closeQuietly(output); } } finally { closeQuietly(input); } if (srcFile.length() != destFile.length()) { throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'"); } if (preserveFileDate) { destFile.setLastModified(srcFile.lastModified()); } }
From source file:dk.clanie.io.IOUtil.java
/** * Creates a copy of a file./* www. jav a2 s . c o m*/ * * If the destination is a directory the file is copied to that directory * with the same name as the source file. * * @param from * - source File * @param to * - destination File * @param overwrite * - allow overwriting existing file * * @throws RuntimeIOException */ @SuppressWarnings("resource") public static void copyFile(File from, File to, boolean overwrite) throws RuntimeIOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { if (!from.exists()) throw new RuntimeIOException.FileNotFound(from); if (to.isDirectory()) { to = new File(to.getPath() + File.separator + from.getName()); } if (to.exists()) { if (!overwrite) throw new RuntimeIOException.FileAlreadyExists(to); } else if (!to.createNewFile()) throw new RuntimeIOException.FailedToCreate(to); inputChannel = new FileInputStream(from).getChannel(); outputChannel = new FileOutputStream(to).getChannel(); long size = inputChannel.size(); long copied = 0L; while (copied < size) copied += outputChannel.transferFrom(inputChannel, copied, size - copied); } catch (java.io.IOException e) { throw new RuntimeIOException(e.getMessage(), e); } finally { closeChannels(inputChannel, outputChannel); } }
From source file:org.grouplens.lenskit.data.dao.packed.BinaryRatingDAO.java
/** * Open a binary rating DAO./*from w ww . j a v a2 s . co m*/ * @param file The file to open. * @return A DAO backed by {@code file}. * @throws IOException If there is */ public static BinaryRatingDAO open(File file) throws IOException { FileInputStream input = new FileInputStream(file); try { FileChannel channel = input.getChannel(); BinaryHeader header = BinaryHeader.read(channel); logger.info("Loading DAO with {} ratings of {} items from {} users", header.getRatingCount(), header.getItemCount(), header.getUserCount()); ByteBuffer data = channel.map(FileChannel.MapMode.READ_ONLY, channel.position(), header.getRatingDataSize()); channel.position(channel.position() + header.getRatingDataSize()); ByteBuffer tableBuffer = channel.map(FileChannel.MapMode.READ_ONLY, channel.position(), channel.size() - channel.position()); BinaryIndexTable utbl = BinaryIndexTable.fromBuffer(header.getUserCount(), tableBuffer); BinaryIndexTable itbl = BinaryIndexTable.fromBuffer(header.getItemCount(), tableBuffer); return new BinaryRatingDAO(file, header, data, utbl, itbl); } finally { input.close(); } }
From source file:org.mycore.common.content.util.MCRServletContentHelper.java
private static long copyFileChannel(final FileChannel src, final WritableByteChannel dest, final int bufferSize) throws IOException { long bytes = 0L; long time = -System.currentTimeMillis(); long size = src.size(); while (bytes < size) { long bytesToTransfer = Math.min(bufferSize, size - bytes); long bytesTransfered = src.transferTo(bytes, bytesToTransfer, dest); bytes += bytesTransfered;//from www . java 2s . c o m if (LOGGER.isDebugEnabled()) { long percentage = Math.round(bytes / ((double) size) * 100.0); LOGGER.debug("overall bytes transfered: " + bytes + " progress " + percentage + "%"); } } if (LOGGER.isDebugEnabled()) { time += System.currentTimeMillis(); double kBps = (bytes / 1024.0) / (time / 1000.0); LOGGER.debug("Transfered: " + bytes + " bytes in: " + (time / 1000.0) + " s -> " + kBps + " kbytes/s"); } return bytes; }
From source file:marytts.util.io.FileUtils.java
public static void copy(File source, File dest) throws IOException { FileChannel in = null, out = null; try {/*w w w . j a va 2s . c o m*/ System.out.println("copying: " + source + "\n --> " + dest); in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); out.write(buf); } catch (Exception e) { System.out.println("Error copying file " + source.getAbsolutePath() + " to " + dest.getAbsolutePath() + " : " + e.getMessage()); throw new IOException(); } finally { FileUtils.close(in, out); } }