How do I use Java NIO to Copy File?
Category: java.io, viewed: 1786 time(s).
package org.kodejava.sample.java.nio; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; import java.nio.ByteBuffer; public class CopyFileExample { // This program is an example how we use the New I/O API in Java // programming. This API is located in the java.nio.*, this requires // at least Java 1.4 because this API was first included in this // version. // The java.nio is a block based io processing, instead of a stream // base io which is the old version io processing in Java. public static void main(String[] args) throws Exception { // The source file name where data will be copied from String source = "medical-report.txt"; // The destination file name where data will be copied to String destination = "medical-report-final.txt"; // Create an instance of FileInputStream to read the source // content FileInputStream fis = new FileInputStream(source); // Create an instance of FileOutputStream to write the data // into destination file FileOutputStream fos = new FileOutputStream(destination); // Get the FileChannel of FileInputStream instance FileChannel fci = fis.getChannel(); // Get the FileChannel of FileOutputStream instance FileChannel fco = fos.getChannel(); // Create a buffer with 1024 size for buffering data // while copying from source file to destination file. // To create the buffer here we used a static method // ByteBuffer.allocate() ByteBuffer buffer = ByteBuffer.allocate(1024); // Here we start to read the source file and write it // to the destionation file. We repeat this process // until the read method of input stream channel return // nothing (-1). while(true) { // read a block of data and put it in the buffer int read = fci.read(buffer); // did we reach the end of the channel? if yes // jump out the while-loop if (read == -1) break; // flip the buffer buffer.flip(); // write to the destination channel fco.write(buffer); // clear the buffer and user it for the next read // process buffer.clear(); } } } |
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I store properties as XML file?
- How do I load properties from XML file?
- How do I convert InputStream to String?
- How do I convert string into InputStream?
- How do I get an exception stack trace message?
- How do I use Console class to read user input?
- How do I store objects in file?
- How do I use DataInputStream and DataOutputStream?
- How do I use RandomAccessFile class?
- How do I create a directories recursively?
- How can I get current working directory?
- How can I change a file attribute to writable?
- How can I change a file attribute to read only?
- How do I append data to a text file?
- How do I use LineNumberReader class to read file?
- How do I check for a directory?
- How do I get total space and free space of my disk?
- How do I rename a file or directory?
- How do I check if a file is hidden?
- How do I get the content of a directory?
Most Viewed Examples
Latest Code Examples
Categories
100 Top & Latest
Latest Jobs
Blog Entries
Forums Entries
- Google Chrome
- Re: Importing and scanning the text
- Re: What Java books have you read?
- Re: How to create exe file
- How to create exe file
- Re: What Java books have you read?
- Infix expression
- Importing and scanning the text
- Re: I want to reduce the size of a JPG image
- MOVED: I want to reduce the size of a JPG image
