Mega Code Archive

 
Categories / Java / File Input Output
 

Using FileChannels to Access a File

import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main {   public static void main(String args[]) throws IOException {     FileInputStream fis = new FileInputStream("FileChannelExample.java");     FileChannel fc = fis.getChannel();     ByteBuffer bb = ByteBuffer.allocate((int) fc.size());     fc.read(bb);     bb.flip();     String fileContent = new String(bb.array());     fc.close();     fc = null;     System.out.println("fileContent = " + fileContent);   } }