public static void copyFile(File sourceFile, File destFile) throws IOException { 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(); } } }
/** * Closes an open channel. If the channel is already closed then this method * has no effect, otherwise it closes the receiver via the * {@code implCloseChannel} method. * <p> * If an attempt is made to perform an operation on a closed channel then a * {@link java.nio.channels.ClosedChannelException} is thrown. * <p> * If multiple threads attempt to simultaneously close a channel, then only * one thread will run the closure code and the others will be blocked until * the first one completes. * * @throws IOException * if a problem occurs while closing this channel. * @see java.nio.channels.Channel#close() */ @Override public final void close() throws IOException { if (!closed) { synchronized (this) { if (!closed) { closed = true; implCloseChannel(); } } } }
void doBegin() { super.begin(); } }
public static void copyFile(File src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
final FileInputStream inputStream = new FileInputStream(src); final FileOutputStream outputStream = new FileOutputStream(dest); final FileChannel inChannel = inputStream.getChannel(); final FileChannel outChannel = outputStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inChannel.close(); outChannel.close(); inputStream.close(); outputStream.close();
final FileChannel channel = new FileInputStream(fileName).getChannel(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); // when finished channel.close();
// create the file so we have something to read. final String fileName = "1.2.fasta"; FileOutputStream fos = new FileOutputStream(fileName); fos.write(new byte[54 * 1024 * 1024]); fos.close(); // read the file in one hit. long start = System.nanoTime(); FileChannel fc = new FileInputStream(fileName).getChannel(); ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); while (bb.remaining() > 0) bb.getLong(); long time = System.nanoTime() - start; System.out.printf("Took %.3f seconds to read %.1f MB%n", time / 1e9, fc.size() / 1e6); fc.close(); ((DirectBuffer) bb).cleaner().clean();
// 4k buffer size. static final int SIZE = 4 * 1024; static byte[] buffer = new byte[SIZE]; // Fastest because a FileInputStream has an associated channel. private static void ScanDataFile(Hunter p, FileInputStream f) throws FileNotFoundException, IOException { // Use a mapped and buffered stream for best speed. // See: http://nadeausoftware.com/articles/2008/02/java_tip_how_read_files_quickly final FileChannel ch = f.getChannel(); long red = 0L; do { final long read = Math.min(Integer.MAX_VALUE, ch.size() - red); final MappedByteBuffer mb = ch.map(FileChannel.MapMode.READ_ONLY, red, read); int nGet; while (mb.hasRemaining() && p.ok()) { nGet = Math.min(mb.remaining(), SIZE); mb.get(buffer, 0, nGet); for (int i = 0; i < nGet && p.ok(); i++) { p.check(buffer[i]); //size += 1; } } red += read; } while (red < ch.size() && p.ok()); // Finish off. p.close(); ch.close(); f.close(); }
FileChannel sourceChannel = null; try { sourceChannel = new FileInputStream(new File(source)).getChannel(); ReadableByteChannel rbc = new CallbackByteChannel(sourceChannel, Files.size(Paths.get(source)), callBack); fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (Exception e) { e.printStackTrace(); } finally { if(sourceChannel.isOpen()){ sourceChannel.close(); fos.close();
int blockSize = 32 * 1024; ByteBuffer bb = ByteBuffer.allocateDirect(blockSize); FileChannel out = new FileOutputStream("deleteme.dat").getChannel(); for (int i = 0; i < (1024 << 20); i += blockSize) { bb.clear(); while (bb.remaining() > 0) if (out.write(bb) < 1) throw new AssertionError(); } out.close(); long start = System.nanoTime(); FileChannel in = new FileInputStream("deleteme.dat").getChannel(); MappedByteBuffer map = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); in.close(); long end = System.nanoTime(); System.out.printf("Mapped file at a rate of %.1f MB/s%n", 1024 * 1e9 / (end - start));
int[] ints = new int[10 * 1000 * 1000]; long start = System.nanoTime(); ByteBuffer byteBuffer = ByteBuffer.allocateDirect(ints.length*4+4); byteBuffer.putInt(ints.length); IntBuffer intBuffer = byteBuffer.asIntBuffer(); intBuffer.put(ints); byteBuffer.position(0); FileChannel fc = new FileOutputStream("main.dat").getChannel(); fc.write(byteBuffer); fc.force(false); fc.close(); long time = System.nanoTime() - start; System.out.println("Write time " + time / 1000 / 1000 + " ms."); long start2 = System.nanoTime(); FileChannel fc2 = new FileInputStream("main.dat").getChannel(); ByteBuffer lengthBuffer = ByteBuffer.allocate(4); while(lengthBuffer.remaining()>0) fc2.read(lengthBuffer); int length = lengthBuffer.getInt(0); int[] ints2 = new int[length]; ByteBuffer buffer2 = ByteBuffer.allocateDirect(length*4); while(buffer2.remaining()>0 && fc2.read(buffer2) > 0); buffer2.flip(); buffer2.asIntBuffer().get(ints2); long time2 = System.nanoTime() - start2; System.out.println("Read time " + time2 / 1000 / 1000 + " ms.");
public static void main(String... args) throws IOException { for (int i = 512; i <= 2 * 1024 * 1024; i *= 2) readWrite(i); } private static void readWrite(int blockSize) throws IOException { ByteBuffer bb = ByteBuffer.allocateDirect(blockSize); long start = System.nanoTime(); FileChannel out = new FileOutputStream("deleteme.dat").getChannel(); for (int i = 0; i < (1024 << 20); i += blockSize) { bb.clear(); while (bb.remaining() > 0) if (out.write(bb) < 1) throw new AssertionError(); } out.close(); long mid = System.nanoTime(); FileChannel in = new FileInputStream("deleteme.dat").getChannel(); for (int i = 0; i < (1024 << 20); i += blockSize) { bb.clear(); while (bb.remaining() > 0) if (in.read(bb) < 1) throw new AssertionError(); } in.close(); long end = System.nanoTime(); System.out.printf("With %.1f KB block size write speed %.1f MB/s, read speed %.1f MB/s%n", blockSize / 1024.0, 1024 * 1e9 / (mid - start), 1024 * 1e9 / (end - mid)); }
FileChannel fc = new FileInputStream(file).getChannel(); ByteBuffer bb = ByteBuffer.allocateDirect((int) fc.size()); while (bb.remaining() > 0) fc.read(bb); fc.close(); bb.flip(); // choose the right endianness ShortBuffer sb = bb.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
public static void main(String[] args) throws IOException { // reads in bytes from a file (args[0]) into input stream (inFile) FileInputStream inFile = new FileInputStream(args[0]); // creates an output stream (outFile) to write bytes to. FileOutputStream outFile = new FileOutputStream(args[1]); // get the unique channel object of the input file FileChannel inChannel = inFile.getChannel(); // get the unique channel object of the output file. FileChannel outChannel = outFile.getChannel(); /* create a new byte buffer and pre-allocate 1MB of space in memory and continue to read 1mb of data from the file into the buffer until the entire file has been read. */ for (ByteBuffer buffer = ByteBuffer.allocate(1024*1024); inChannel.read(buffer) != 1; buffer.clear()) { // set the starting position of the buffer to be the current position (1Mb of data in from the last position) buffer.flip(); // write the data from the buffer into the output stream while (buffer.hasRemaining()) outChannel.write(buffer); } // close the file streams. inChannel.close(); outChannel.close(); }
FileInputStream in = new FileInputStream(src); FileChannel srcChannel = in.getChannel(); FileChannel destChannel = null; FileLock destLock = null; destChannel.close(); srcChannel.close(); in.close(); final long size = src.size(); for (long pos = 0; pos < size; ) { long count = return false; aBuffer.clear(); aCh = a.read(aBuffer); bBuffer.clear(); return s_eof == b.read(bBuffer);
FileChannel inChannel = new FileInputStream(fileName).getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY); while(inChannel.read(buffer) > 0) buffer.clear(); // do something with the data and clear/compact it. inChannel.close();
Integer I; String file_name="C:/Users/User/test.txt"; RandomAccessFile input_file = new RandomAccessFile(file_name,"r"); FileInputStream in = new FileInputStream(file_name); FileChannel ch = in.getChannel(); FileChannel inChannel = input_file.getChannel(); long file_size = inChannel.size(); ByteBuffer buffer = ByteBuffer.allocate((int) file_size); while ( (rd = ch.read( buffer )) != -1 ) buffer.rewind(); buffer.clear(); inChannel.close(); input_file.close();
import java.io.*; import java.nio.channels.*; import javax.xml.bind.DatatypeConverter; public class EncodeDecode { public static void main(String[] args) throws Exception { File file = new File("/bin/ls"); byte[] bytes = loadFile(file, new ByteArrayOutputStream()).toByteArray(); String encoded = DatatypeConverter.printBase64Binary(bytes); System.out.println(encoded); byte[] decoded = DatatypeConverter.parseBase64Binary(encoded); // check for (int i = 0; i < bytes.length; i++) { assert bytes[i] == decoded[i]; } } private static <T extends OutputStream> T loadFile(File file, T out) throws IOException { FileChannel in = new FileInputStream(file).getChannel(); try { assert in.size() == in.transferTo(0, in.size(), Channels.newChannel(out)); return out; } finally { in.close(); } } }
final BufferedInputStream inputStream1 = new BufferedInputStream( new FileInputStream( new File(fileName1)), myBufferSize); first = new FileInputStream(fileName1).getChannel(); seconde = new FileInputStream(fileName2).getChannel(); if (first.size() != seconde.size()) { return false; ByteBuffer secondBuffer = ByteBuffer.allocateDirect(myBufferSize); int firstRead, secondRead; while (first.position() < first.size()) { firstRead = first.read(firstBuffer); totalbytes += firstRead; } finally { if (first != null) { first.close(); seconde.close(); if (first.limit() != second.limit() || length > first.limit()) { return false;
FileChannel outChan = new FileOutputStream(f, true).getChannel(); outChan.truncate(newSize); outChan.close();