集册 Java实例教程 使用旧的ReadableByteChannel接口读取文件

使用旧的ReadableByteChannel接口读取文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

550
使用旧的ReadableByteChannel接口读取文件

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.ReadableByteChannel;

import java.nio.charset.Charset;/*来 自 时代Java公众号 - nowjava.com*/

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;


public class Main {


  public static void main(String[] args) {


    Path path = Paths.get("C:/folder1/folder0/folder8", "story.txt");//from N o w J a v a . c o m - 时代Java


    // read a file using ReadableByteChannel

    try (ReadableByteChannel readableByteChannel = Files.newByteChannel(path)) {

      ByteBuffer buffer = ByteBuffer.allocate(12);

      buffer.clear();

      String encoding = System.getProperty("file.encoding");


      while (readableByteChannel.read(buffer) > 0) {

        buf
展开阅读全文