List of usage examples for java.nio.channels FileChannel size
public abstract long size() throws IOException;
From source file:MainClass.java
private static void createFile() throws Exception { long[] primes = new long[] { 1, 2, 3, 5, 7 }; File aFile = new File("C:/primes.bin"); FileOutputStream outputFile = new FileOutputStream(aFile); FileChannel file = outputFile.getChannel(); final int BUFFERSIZE = 100; ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); LongBuffer longBuf = buf.asLongBuffer(); int primesWritten = 0; while (primesWritten < primes.length) { longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten)); buf.limit(8 * longBuf.position()); file.write(buf);/*from ww w . j a va2s. c om*/ primesWritten += longBuf.position(); longBuf.clear(); buf.clear(); } System.out.println("File written is " + file.size() + "bytes."); outputFile.close(); }
From source file:Main.java
/** * Effective way to copy files by file channel. * @return Return the number of copied files. *///from ww w . ja v a 2s.c o m public static int fileChannelCopy(File[] sources, File[] targets) { int result = 0; if (sources == null || targets == null) { return result; } FileInputStream fis = null; FileOutputStream fos = null; FileChannel fc_in = null; FileChannel fc_out = null; try { for (int i = 0, len_s = sources.length, len_t = targets.length; i < len_s && i < len_t; ++i) { if (sources[i] == null || targets[i] == null) { continue; } fis = new FileInputStream(sources[i]); fos = new FileOutputStream(targets[i]); fc_in = fis.getChannel(); fc_out = fos.getChannel(); fc_in.transferTo(0, fc_in.size(), fc_out); ++result; } } catch (IOException e) { e.printStackTrace(); } finally { if (fc_out != null) { try { fc_out.close(); } catch (IOException e) { } } if (fos != null) { try { fos.close(); } catch (IOException e) { } } if (fc_in != null) { try { fc_in.close(); } catch (IOException e) { } } if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return result; }
From source file:och.util.FileUtil.java
public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.getParentFile().mkdirs(); 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()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:de.ingrid.portal.portlets.admin.AdminPortalProfilePortlet.java
/** * Copy Files in file system./* w ww. j a va2 s . com*/ * * @param source * @param dest * @throws IOException */ private static void copy(File source, File dest) throws IOException { FileChannel in = null, out = null; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); out.transferFrom(in, 0, in.size()); } catch (Exception e) { log.error("Error copy files ('" + source.getAbsolutePath() + "' -> '" + dest.getAbsolutePath() + "')", e); } finally { if (in != null) in.close(); if (out != null) out.close(); } }
From source file:com.github.fritaly.dualcommander.Utils.java
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"); }//from w ww .j a v a2 s.c om 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; while (pos < size) { count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos; pos += output.transferFrom(input, pos, count); } } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(input); IOUtils.closeQuietly(fis); } 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:dsd.controller.ParserControler.java
@SuppressWarnings("resource") private static boolean TransferImageToSite(File inFile, File outFile) { if (!outFile.exists()) { try {//from w w w . j a v a2 s.c o m outFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); return false; } } FileChannel inChannel = null; FileChannel outChannel = null; try { inChannel = new FileInputStream(inFile).getChannel(); outChannel = new FileOutputStream(outFile).getChannel(); int maxCount = (64 * 1024 * 1024) - (32 * 1024); long size = inChannel.size(); long position = 0; while (position < size) { position += inChannel.transferTo(position, maxCount, outChannel); } return true; } catch (Exception e) { e.printStackTrace(); } finally { try { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } catch (IOException ex) { ex.printStackTrace(); } } return false; }
From source file:com.weihuoya.bboo._G.java
public static boolean copyFile(File src, File dst) { FileInputStream instream = null; FileOutputStream outstream = null; FileChannel inchannel = null; FileChannel outchannel = null; try {//from w w w .ja va 2s . c o m // channel instream = new FileInputStream(src); outstream = new FileOutputStream(dst); inchannel = instream.getChannel(); outchannel = outstream.getChannel(); // transfer inchannel.transferTo(0, inchannel.size(), outchannel); // close inchannel.close(); outchannel.close(); instream.close(); outstream.close(); // return true; } catch (IOException e) { _G.log(e.toString()); } return false; }
From source file:org.bibsonomy.lucene.util.generator.LuceneGenerateResourceIndex.java
/** * Fast & simple file copy./*from www .j a v a2s . co m*/ * * @param source * @param dest * @throws IOException */ public static void copyFile(final File source, final File dest) throws IOException { FileChannel in = null, out = null; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); final long size = in.size(); final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); out.write(buf); } finally { if (in != null) in.close(); if (out != null) out.close(); } }
From source file:org.jboss.as.forge.util.Files.java
/** * Copies the source file to the destination file. * * @param srcFile the file to copy//ww w. ja v a2 s.com * @param targetFile the target file * * @return {@code true} if the file was successfully copied, {@code false} if the copy failed or was incomplete * * @throws IOException if an IO error occurs copying the file */ public static boolean copyFile(final File srcFile, final File targetFile) throws IOException { FileInputStream in = null; FileOutputStream out = null; FileChannel inChannel = null; FileChannel outChannel = null; try { in = new FileInputStream(srcFile); inChannel = in.getChannel(); out = new FileOutputStream(targetFile); outChannel = out.getChannel(); long bytesTransferred = 0; while (bytesTransferred < inChannel.size()) { bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel); } } finally { Streams.safeClose(outChannel); Streams.safeClose(out); Streams.safeClose(inChannel); Streams.safeClose(in); } return srcFile.length() == targetFile.length(); }
From source file:org.sakaiproject.emailtemplateservice.service.impl.EmailTemplateServiceImpl.java
private static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try {/*from w w w . j a va 2 s.c om*/ FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }