Mega Code Archive

 
Categories / Java / File Input Output
 

Use FileChannel and ByteBuffer

import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main {   static public void main(String args[]) throws Exception {     FileInputStream fin = new FileInputStream("infile.txt");     FileOutputStream fout = new FileOutputStream("outfile.txt");     FileChannel inc = fin.getChannel();     FileChannel outc = fout.getChannel();     ByteBuffer bb = ByteBuffer.allocate(1024);     while (true) {       int ret = inc.read(bb);       if (ret == -1)          break;       bb.flip();       outc.write(bb);       bb.clear();      }   } }