`

NIO2 AsynchronousFileChannel

阅读更多
package com.java7developer.chapter2;

import java.nio.*;
import java.nio.channels.*;
import java.nio.file.*;
import java.io.IOException;

public class Nio2ReadLargeLogFile implements
        CompletionHandler<Integer, AsynchronousFileChannel> {

    // need to keep track of the next position.
    int pos = 0;
    AsynchronousFileChannel channel = null;
    ByteBuffer buffer = null;

    public void completed(Integer result, AsynchronousFileChannel attachment) {
//        System.out.println("bytes num " + result);
        // if result is -1 means nothing was read.
        if (result != -1) {
            pos += result; // don't read the same text again.
                            // your output command.
            //这里必须读取0,result直接的buffer,否则会读取到垃圾数据
            System.out.print(new String(buffer.array(),0,result));

//            buffer.clear(); // reset the buffer so you can read more.
            buffer.flip();
            attachment.read(buffer, pos, attachment, this);
        } else {
            return;
        }
        // initiate another asynchronous read, with this.

    }

    public void failed(Throwable exc, AsynchronousFileChannel attachment) {
        System.err.println("Error!");
        exc.printStackTrace();
    }

    public void doit() {
        Path file = Paths.get("c:/a");
        AsynchronousFileChannel channel = null;
        try {
            channel = AsynchronousFileChannel.open(file);
        } catch (IOException e) {
            System.err.println("Could not open file: " + file.toString());
            System.exit(1); // yeah. heh.
        }
        buffer = ByteBuffer.allocate(1000);

        // start off the asynch read.
        channel.read(buffer, pos, channel, this);
        // this method now exits, thread returns to main and waits for user
        // input.
    }

    public static void main(String[] args) {
        Nio2ReadLargeLogFile tn = new Nio2ReadLargeLogFile();
        tn.doit();
        // wait fur user to press a key otherwise java exits because the
        // asynch thread isn't important enough to keep it running.
        try {
            System.in.read();
        } catch (IOException e) {
        }
    }
}

 

分享到:
评论

相关推荐

    Java IO NIO and NIO 2 无水印pdf

    Java IO NIO and NIO 2 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn...

    Java IO NIO and NIO 2 epub

    Java IO NIO and NIO 2 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    NIO2基础介绍

    主要是一些关于NIO2的基本操作和内容,内容是网上下载的收集的,适合没有使用过JDK7以上版本或没有使用过新的文件操作的规范的新手。

    Java-NIO2教程

    比较详尽地介绍了JDK1.7开始支持的NIO2技术。 有兴趣的开发者可以了解一下。

    Java IO, NIO and NIO.2(Apress,2015)

    Java I/O, NIO, and NIO.2 is a power-packed book that accelerates your mastery of Java's various I/O APIs. In this book, you'll learn about classic I/O APIs (File, RandomAccessFile, the stream classes ...

    JDK_7_NIO2

    JDK1.7新特性,NIO2异步学习笔记。

    Pro Java 7 NIO2

    java nio2, Pro Java 7 NIO2

    Java IO, NIO and NIO.2 原版pdf by Friesen

    New I/O (NIO), and NIO.2 categories. You learn what each category offers in terms of its capabilities, and you also learn about concepts such as paths and Direct Memory Access. Chapters 2 through 5 ...

    Java IO, NIO and NIO.2

    这是一本介绍java io以及nio相关知识的书,书中对知识的讲解通俗易懂,是学习java nio以及复习java io相关知识的必备书籍。注意:本书为英文版!!!

    Novation诺维逊 Nio 2&4 用户说明书.pdf

    Novation诺维逊 Nio 2&4 用户说明书.pdf

    Java I-O NIO and NIO2

    完整的讲述了Java的IO,NIO以及NIO2,文档是英文版的,我会陆续翻译其中的NIO章节。

    Java NIO 中英文版 + Pro Java 7 NIO.2

    Java NIO,Ron Hitchens 著,中文版 裴小星 译,Pro Java 7 NIO.2,Anghel Leonard 著,pdf文字版带书签,无安全限制

    JavaNIO chm帮助文档

    Java NIO系列教程(一) Java NIO 概述 Java NIO系列教程(二) Channel Java NIO系列教程(三) Buffer Java NIO系列教程(四) Scatter/Gather Java NIO系列教程(五) 通道之间的数据传输 Java NIO系列教程(六)...

    ProJava7NIO.2PDFBooks.pdf 英文原版

    Pro Java 7 NIO.2 – PDF Books

    pro_java_7_nio.2.pdf

    详细介绍java7中新的nio,英文原版pdf,绝对清晰,非扫描版

    java NIO和java并发编程的书籍

    java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java...

    Java IO,NIO and NIO.2 mobi

    java io nio nio2 java io的百科全书 mobi格式 需要kindle 软件

    NIO 入门.chm,NIO 入门.chm

    NIO入门.chm NIO入门.chm NIO入门.chm

    java nio 包读取超大数据文件

    Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据...

Global site tag (gtag.js) - Google Analytics