public class TestFileInputStream {
/**
* 使用ByteBuffer和FileChannel读取数据
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//创建文件的输入流
File file = new File("/Users/wangzeyu/Downloads/study/sql/1/a.txt");
FileInputStream fileInputStream = new FileInputStream(file);
//通过对应的fileInputStream获取对应的FileChannel,实际类型为FileChannelImpl
FileChannel channel = fileInputStream.getChannel();
//创建缓冲区
ByteBuffer allocate = ByteBuffer.allocate((int) file.length());
//将通道的数据读入到Buffer
channel.read(allocate);
System.out.println(new String(allocate.array()));
}
}