FileChannel应用实例——本地文件读数据(假定文件已存在)
生活随笔
收集整理的這篇文章主要介紹了
FileChannel应用实例——本地文件读数据(假定文件已存在)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package com.atguigu.nio;import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;public class NIOFileChannel02 {public static void main(String[] args) throws Exception {//創(chuàng)建文件的輸入流File file = new File("d:\\file01.txt");FileInputStream fileInputStream = new FileInputStream(file);//通過fileInputStream 獲取對應的FileChannel -> 實際類型 FileChannelImplFileChannel fileChannel = fileInputStream.getChannel();//創(chuàng)建緩沖區(qū)ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());//將 通道的數(shù)據讀入到BufferfileChannel.read(byteBuffer);//將byteBuffer 的 字節(jié)數(shù)據 轉成StringSystem.out.println(new String(byteBuffer.array()));fileInputStream.close();}
}
總結
以上是生活随笔為你收集整理的FileChannel应用实例——本地文件读数据(假定文件已存在)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FileChannel应用实例——本地文
- 下一篇: FileChannel应用实例——拷贝文