Mega Code Archive

 
Categories / Java Tutorial / File
 

Using FileChannel to read text file

import java.io.File; import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class MainClass {   public static void main(String[] args) throws Exception {     File aFile = new File("charData.xml");     FileInputStream inFile = null;     inFile = new FileInputStream(aFile);     FileChannel inChannel = inFile.getChannel();     ByteBuffer buf = ByteBuffer.allocate(48);     while (inChannel.read(buf) != -1) {       System.out.println("String read: " + ((ByteBuffer) (buf.flip())).asCharBuffer().get(0));       buf.clear();     }     inFile.close();   } }