读取文本文件内容(Java FileChannel)
import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /** * 读取文本文件内容Java FileChannel。 * author Bright Lee */ public class FileChannelTest { public static void main(String[] args) { RandomAccessFile file null; try { file new RandomAccessFile(C:\\JavaNIO\\test.txt, rw); FileChannel channel file.getChannel(); ByteBuffer buffer ByteBuffer.allocate(1024); while (true) { int count channel.read(buffer); if (count -1) { break; } buffer.flip(); while (buffer.hasRemaining()) { char ch (char) buffer.get(); System.out.print(ch); } buffer.compact(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (file ! null) { file.close(); } } catch (IOException e) { e.printStackTrace(); } } } }运行结果Hello world! Java file NIO以下是我们工作室开发的软件《榴芒客服系统》的介绍博客里面有软件的下载地址https://blog.csdn.net/look4liming/article/details/164755808
上一篇/下一篇内容由系统自动关联
返回资讯列表 →