List of usage examples for java.nio.channels FileChannel close
public final void close() throws IOException
From source file:org.apache.solr.util.FileUtils.java
public static void copyFile(File src, File destination) throws IOException { FileChannel in = null; FileChannel out = null;//www . j a va 2 s . c o m try { in = new FileInputStream(src).getChannel(); out = new FileOutputStream(destination).getChannel(); in.transferTo(0, in.size(), out); } finally { try { if (in != null) in.close(); } catch (IOException e) { } try { if (out != null) out.close(); } catch (IOException e) { } } }
From source file:maspack.fileutil.SafeFileUtils.java
public static boolean closeQuietly(FileChannel stream) { try {//from w ww. ja v a 2s . com stream.close(); } catch (Exception e) { return false; } return true; }
From source file:Main.java
public static void copyFile(File sourceFile, File destFile) throws IOException { new File(destFile.getParent()).mkdirs(); if (!destFile.exists()) { destFile.createNewFile();//from w w w .jav a2 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()); } catch (Exception e) { e.printStackTrace(); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:com.intuit.tank.service.impl.v1.report.FileReader.java
/** * Gets a StreamingOutput from the passedin file from start to end or from beginning to end if start is greater than * end. if a negative number is passed, it will get the last n lines of the file. If 0 is passed it will return the * entire file./* w w w . j a v a 2s . co m*/ * * @param total * * @return a StreamingOutput */ public static StreamingOutput getFileStreamingOutput(final File f, long total, String start) { long l = 0; if (start != null) { try { l = Long.parseLong(start); // num lines to get from end if (l < 0) { l = getStartChar(f, Math.abs(l), total); } } catch (Exception e) { LOG.error("Error parsing start " + start + ": " + e); } } final long to = l > total ? 0 : total; final long from = l; StreamingOutput streamer = new StreamingOutput() { @SuppressWarnings("resource") @Override public void write(final OutputStream output) throws IOException, WebApplicationException { final FileChannel inputChannel = new FileInputStream(f).getChannel(); final WritableByteChannel outputChannel = Channels.newChannel(output); try { inputChannel.transferTo(from, to, outputChannel); } finally { // closing the channels inputChannel.close(); outputChannel.close(); } } }; LOG.debug("returning data from " + from + " - " + to + " of total " + total); return streamer; }
From source file:edu.clemson.cs.nestbed.server.util.FileUtils.java
public static void copyFile(File in, File out) throws FileNotFoundException, IOException { FileChannel sourceChannel = null; FileChannel destinationChannel = null; try {// ww w. ja v a 2 s.c o m sourceChannel = new FileInputStream(in).getChannel(); destinationChannel = new FileOutputStream(out).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); } finally { try { sourceChannel.close(); } catch (Exception ex) { } try { destinationChannel.close(); } catch (Exception ex) { } } }
From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java
public static void channelTest(File source, File target) throws Exception { FileInputStream fis = null;//from w ww . ja v a 2 s . c o m FileOutputStream fos = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(target); FileChannel sChannel = fis.getChannel(); FileChannel tChannel = fos.getChannel(); target.createNewFile(); ByteBuffer buffer = ByteBuffer.allocate(16 * 1024); while (sChannel.read(buffer) > 0) { buffer.flip(); tChannel.write(buffer); buffer.clear(); } tChannel.close(); sChannel.close(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } }
From source file:org.neo4j.server.webadmin.AbstractWebadminTest.java
private static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();//from w w w. jav a 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:Grep.java
public static void setFile(File f) throws IOException { FileInputStream fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory int sz = (int) fc.size(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); // Decode the file into a char buffer indexFile = decoder.decode(bb);//from www. ja v a 2s . c o m fc.close(); }
From source file:GrepSun.java
private static void grep(File f) throws IOException { // Open the file and then get a channel from the stream FileInputStream fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory int sz = (int) fc.size(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); // Decode the file into a char buffer CharBuffer cb = decoder.decode(bb); // Perform the search grep(f, cb);// w ww .j ava 2s. c o m // Close the channel and the stream fc.close(); }
From source file:Main.java
/** * /* w w w. j a v a2s . co m*/ * copy file * * @param src * source file * @param dest * target file * @throws IOException */ public static void copyFile(File src, File dest) throws IOException { FileChannel inChannel = null; FileChannel outChannel = null; try { if (!dest.exists()) { dest.createNewFile(); } inChannel = new FileInputStream(src).getChannel(); outChannel = new FileOutputStream(dest).getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } }