List of usage examples for java.nio.channels FileChannel close
public final void close() throws IOException
From source file:org.infai.amor.test.ModelUtil.java
public static void copyFile(final File in, final File out) throws IOException { final FileChannel inChannel = new FileInputStream(in).getChannel(); final FileChannel outChannel = new FileOutputStream(out).getChannel(); try {// ww w . j a v a 2 s .c om inChannel.transferTo(0, inChannel.size(), outChannel); } catch (final IOException e) { throw e; } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.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 2 s . com*/ } 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:yui.classes.utils.IOUtils.java
/** * * good for Large Files >2Mb/*w w w .j av a 2 s . c o m*/ * @param filename * @return */ private static byte[] largeFileReader(String filename) { byte[] bytes = null; FileChannel fc = null; try { fc = new FileInputStream(filename).getChannel(); MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); int size = byteBuffer.capacity(); if (size > 0) { // Retrieve all bytes in the buffer byteBuffer.clear(); bytes = new byte[size]; byteBuffer.get(bytes, 0, bytes.length); } fc.close(); } catch (FileNotFoundException fnf) { System.err.println("" + fnf); } catch (IOException io) { System.err.println("" + io); } finally { if (fc != null) { try { fc.close(); } catch (IOException ioe) { // ignore } } } return bytes; }
From source file:com.aliyun.oss.integrationtests.TestUtils.java
public static String genRandomLengthFile() throws IOException { ensureDirExist(TestBase.UPLOAD_DIR); String filePath = TestBase.UPLOAD_DIR + System.currentTimeMillis(); RandomAccessFile raf = new RandomAccessFile(filePath, "rw"); FileChannel fc = raf.getChannel(); long fileLength = rand.nextInt(MAX_RANDOM_LENGTH); MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, fileLength); try {/*from ww w. j av a2s . c om*/ for (int i = 0; i < fileLength; i++) { mbb.put(pickupAlphabet()); } return filePath; } finally { if (fc != null) { fc.close(); } if (raf != null) { raf.close(); } } }
From source file:cn.com.sunjiesh.wechat.handler.WechatBaseHandler.java
/** * ?//from w w w . j a v a2s . c om * * @param httpResponse httpResponse * @param fileTpye fileTpye * @return File * @throws ServiceException ServiceException */ public static File getFileResponseFromHttpResponse(HttpResponse httpResponse, String fileTpye) throws ServiceException { File file = null; try { StatusLine httpStatusLine = httpResponse.getStatusLine(); HttpEntity httpEntity = httpResponse.getEntity(); int statusCode = httpStatusLine.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { LOGGER.error(DOWNLOAD_FILE_ERROR); throw new ServiceException(DOWNLOAD_FILE_ERROR); } String fileName = UUID.randomUUID().toString(); fileName = fileName + "." + fileTpye; InputStream is = httpEntity.getContent(); if (!StringUtils.isEmpty(fileName)) { //?? file = new File("/var/tmp", fileName); if (file.exists()) { file.delete(); } FileOutputStream fos = new FileOutputStream(file, false); FileChannel fileChannel = fos.getChannel(); ByteBuffer byteBuffer = null; byte[] tempBytes = new byte[4096]; while (is.read(tempBytes) > 0) { byteBuffer = ByteBuffer.wrap(tempBytes); fileChannel.write(byteBuffer); } if (fos != null) { fos.close(); } fileChannel.close(); } } catch (IllegalStateException e) { LOGGER.error(DOWNLOAD_FILE_ERROR, e); throw new ServiceException(DOWNLOAD_FILE_ERROR, e); } catch (FileNotFoundException e) { LOGGER.error(DOWNLOAD_FILE_ERROR, e); throw new ServiceException(DOWNLOAD_FILE_ERROR, e); } catch (IOException e) { LOGGER.error(DOWNLOAD_FILE_ERROR, e); throw new ServiceException(DOWNLOAD_FILE_ERROR, e); } return file; }
From source file:net.filterlogic.util.imaging.ToTIFF.java
private static String bigEndian2LittleEndian(String fileName) throws FileNotFoundException, IOException { //import java.nio.ByteBuffer; //import java.nio.ByteOrder; //import java.nio.channels.FileChannel; //import java.io.FileOutputStream; byte[] imgData = loadFileToByteArray(fileName); String newFileName = fileName + ".be2le.tif"; ByteBuffer buffer = ByteBuffer.allocate(imgData.length); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put(imgData);//from ww w .j av a2 s . c om FileChannel out = new FileOutputStream(newFileName).getChannel(); out.write(buffer); out.close(); return newFileName; }
From source file:org.eclipse.thym.core.internal.util.FileUtils.java
private static void createFileFromZipFile(File file, ZipFile zipFile, ZipEntry zipEntry) throws IOException { if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { throw new IOException("Can not create parent directory for " + file.toString()); }/*from www. j a v a 2s .c o m*/ file.createNewFile(); FileOutputStream fout = null; FileChannel out = null; InputStream in = null; try { fout = new FileOutputStream(file); out = fout.getChannel(); in = zipFile.getInputStream(zipEntry); out.transferFrom(Channels.newChannel(in), 0, Integer.MAX_VALUE); } finally { if (out != null) out.close(); if (in != null) in.close(); if (fout != null) fout.close(); } }
From source file:Main.java
/** * Effective way to copy files by file channel. * @return Return the number of copied files. *///from w w w . j a v a 2 s.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:ru.adios.budgeter.BundleProvider.java
private static void transferFile(String source, String destination, String errMsg) { final File sourceFile = new File(source); final File destFile = new File(destination); FileChannel fcSource = null; FileChannel fcDest = null;//from www. jav a 2 s . co m try { fcSource = new FileInputStream(sourceFile).getChannel(); fcDest = new FileOutputStream(destFile).getChannel(); fcDest.transferFrom(fcSource, 0, fcSource.size()); fcSource.close(); fcDest.close(); } catch (IOException e) { logger.error(errMsg, e); throw new DataAccessResourceFailureException(errMsg, e); } finally { try { if (fcSource != null) { fcSource.close(); } if (fcDest != null) { fcDest.close(); } } catch (IOException ignore) { logger.warn("Exception while closing IO channels", ignore); } } }
From source file:com.aurel.track.lucene.util.FileUtil.java
/** * Copies a source file to a destination file * @param source/*from ww w .j a va 2 s . c o m*/ * @param destination */ public static void copyFile(File source, File destination) { if (!source.exists()) { return; } if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) { destination.getParentFile().mkdirs(); } FileChannel srcChannel = null; FileChannel dstChannel = null; try { srcChannel = new FileInputStream(source).getChannel(); dstChannel = new FileOutputStream(destination).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); } catch (IOException ioe) { LOGGER.error(ExceptionUtils.getStackTrace(ioe)); } finally { if (srcChannel != null) try { srcChannel.close(); } catch (IOException ioe) { } ; if (dstChannel != null) try { dstChannel.close(); } catch (IOException ioe) { } ; } }