深入理解Java I/O:NIO与NIO.2详解

需积分: 10 4 下载量 27 浏览量 更新于2024-07-19 收藏 2.75MB PDF 举报
"Java IO, NIO 和 NIO.2 是Java平台中处理输入/输出操作的核心技术。这本书涵盖了从Java 8更新51时的I/O能力,旨在帮助开发者理解并有效地利用这些机制来构建复杂的系统。作者Jef Friesen通过详细讲解,使得相对枯燥的I/O主题变得生动易懂。" Java I/O(输入/输出)库是Java编程语言中不可或缺的一部分,它提供了处理文件、网络通信和数据流的基本工具。在Java中,I/O操作主要由java.io包中的类和接口支持,包括InputStream、OutputStream、Reader、Writer等。这些类提供了读写文件、标准输入输出流以及字符和字节流的处理。 NIO(Non-blocking Input/Output)是Java 1.4引入的新特性,它的核心概念是通道(Channel)和缓冲区(Buffer)。与传统的I/O模型不同,NIO允许进行非阻塞I/O操作,这意味着当数据不可用时,程序不会被挂起,而是继续执行其他任务。NIO还引入了选择器(Selector),允许单个线程处理多个通道的事件,提高了并发性能。 NIO.2,又称为New I/O 2或Java NIO 2,是在Java 7中添加的一组新API,进一步增强了Java的I/O功能。NIO.2引入了文件系统相关的API,如Path、Files、Paths,以及异步I/O操作。AsynchronousFileChannel允许进行异步读写操作,而CompletableFuture可以用来处理这些操作的完成状态。此外,NIO.2还引入了对文件属性、符号链接、文件系统事件以及文件通道映射等更全面的支持。 本书《Java I/O, NIO and NIO.2》深入探讨了这些主题,不仅涵盖了基本的I/O流操作,还详细讲解了NIO和NIO.2的高级特性。通过实例代码和详细解释,读者可以学习如何在实际项目中应用这些技术,以提高应用程序的效率和可扩展性。 版权信息提示,该书的复制和分发需要遵循版权法的规定,对于学术研究和评论使用,需得到 Springer 或版权方的许可。对于希望在计算机系统上运行的特定代码,可能需要购买该作品的使用权。如果需要获取使用权限,可以通过RightsLink在版权清算中心获得。
2018-04-28 上传
Input/output (I/O) is not a sexy subject, but it’s an important part of non-trivial applications. This book introduces you to most of Java’s I/O capabilities as of Java 8 update 51. Chapter 1 presents a broad overview of I/O in terms of Java’s classic I/O, 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 cover classic I/O APIs. You learn about the File and RandomAccessFile classes along with streams (including object serialization and externalization) and writers/readers. Chapters 6 through 11 focus on NIO. You explore buffers, channels, selectors, regular expressions, charsets, and formatters. (Formatters were not introduced with the other NIO types in Java 1.4 because they depend on the variable arguments capability that was introduced in Java 5.) NIO is missing several features, which were subsequently provided by NIO.2. Chapters 12 through 14 cover NIO.2’s improved file system interface, asynchronous I/O, and the completion of socket channel functionality. Each chapter ends with assorted exercises that are designed to help you master its content. Along with long answers and true/false questions, you are often confronted with programming exercises. Appendix A provides the answers and solutions. Appendix B provides a tutorial on sockets and network interfaces. Although not directly related to classic I/O, NIO, and NIO.2, they leverage I/O capabilities and are mentioned elsewhere in this book.

com.fmall58.wechatsub.business.bizs.UException at com.fmall58.wechatsub.business.service.PubService.isBindWeChatSub(PubService.java:76) at com.fmall58.wechatsub.business.bizs.PubHelper.isBindWeChatSub(PubHelper.java:125) at com.fmall58.wechatsub.business.bizs.PubHelper.execute(PubHelper.java:38) at com.fmall58.wechatsub.server.handlers.ServerHandler.channelRead(ServerHandler.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:323) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:297) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:656) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:591) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:508) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:470) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:909) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)

2023-06-11 上传

Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: 拒绝连接: node01/192.168.72.110:7077 Caused by: java.net.ConnectException: 拒绝连接 at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:715) at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:702) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748) 23/07/07 22:44:06 ERROR StandaloneSchedulerBackend: Application has been killed. Reason: All masters are unresponsive! Giving up. 23/07/07 22:44:06 WARN StandaloneSchedulerBackend: Application ID is not initialized yet. 23/07/07 22:44:06 INFO Utils: Successfully started service 'org.apache.spark.network.netty.NettyBlockTransferService' on port 45096. 23/07/07 22:44:06 INFO NettyBlockTransferService: Server created on node01:45096 23/07/07 22:44:06 INFO BlockManager: Using org.apache.spark.storage.RandomBlockReplicationPolicy for block replication policy 23/07/07 22:44:06 INFO SparkUI: Stopped Spark web UI at http://node01:4040 23/07/07 22:44:06 INFO BlockManagerMaster: Registering BlockManager BlockManagerId(driver, node01, 45096, None) 23/07/07 22:44:06 INFO StandaloneSchedulerBackend: Shutting down all executors

2023-07-11 上传