- 浏览: 950778 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
前面我们看过socket通道,datagram通道,以管道Pipe,从今天起,我们来看一下file通道,先从一个示例开始:
测试主类1:
按一下三步执行:
1.先屏蔽writeBytes,执行readBytes,从文件中读取数据
2.再屏蔽readBytes,执行writeBytes,向文件中写数据
3.最后屏蔽writeBytes,执行readBytes,从文件中读取数据
相应的控制台输出为:
1.=====Read byte length:0
2.===已经写完数据到文件
3.=====Read byte length:44
new String to write to file....1494407798722
测试主类2:
控制输出:
===将源通道的数据传输的本通道完毕
===将本通道数据传输到目的通道完毕
打开文件E:/nio_data_to.txt和E:/nio_data_to2.txt,文件内容如下:
new String to write to file....1494407798722
测试主类1:
package nio.filechannel; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /** * 测试FileChannel * @author donald * 2017年4月9日 * 下午4:16:35 */ public class testFileChannel { public static void main(String[] args) throws IOException { RandomAccessFile aFile = new RandomAccessFile("E:/nio_data.txt", "rw"); FileChannel inChannel = aFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); /* * 1.先屏蔽writeBytes,执行readBytes,从文件中读取数据 * 2.再屏蔽readBytes,执行writeBytes,向文件中写数据 * 3.最后屏蔽writeBytes,执行readBytes,从文件中读取数据 */ // writeBytes(buf, inChannel); readBytes(buf, inChannel); inChannel.close(); aFile.close(); } private static void writeBytes(ByteBuffer buf, FileChannel fileChannel) throws IOException{ String newData = "new String to write to file...."+System.currentTimeMillis(); buf.put(newData.getBytes("UTF-8")); buf.flip(); while(buf.hasRemaining()) fileChannel.write(buf); System.out.println("===已经写完数据到文件"); } private static void readBytes(ByteBuffer buf, FileChannel fileChannel) throws IOException{ buf.clear(); //从file通道读取数据到缓存区,即写入缓冲区 int bytesRead = fileChannel.read(buf); while (bytesRead != -1) { // buf.compact();//将未读完的数据移到缓冲的前面,新写入的数据,将会append旧数据的后面 bytesRead = fileChannel.read(buf); } //转换缓冲区模式 buf.flip();// swith the mode write or read System.out.println("=====Read byte length:" + buf.limit()); while (buf.hasRemaining()) { System.out.print((char) buf.get()); } System.out.println(); } }
按一下三步执行:
1.先屏蔽writeBytes,执行readBytes,从文件中读取数据
2.再屏蔽readBytes,执行writeBytes,向文件中写数据
3.最后屏蔽writeBytes,执行readBytes,从文件中读取数据
相应的控制台输出为:
1.=====Read byte length:0
2.===已经写完数据到文件
3.=====Read byte length:44
new String to write to file....1494407798722
测试主类2:
package nio.filechannel; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; /** * 测试通道间传输 * @author donald * 2017年4月9日 * 下午10:27:16 */ public class testTransferChannel { public static void main(String[] args) throws IOException { RandomAccessFile fromFile = new RandomAccessFile("E:/nio_data.txt", "rw"); FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("E:/nio_data_to.txt", "rw"); FileChannel toChannel = toFile.getChannel(); RandomAccessFile to2File = new RandomAccessFile("E:/nio_data_to2.txt", "rw"); FileChannel to2Channel = to2File.getChannel(); long position = 0; long count = fromChannel.size(); //将源通道的数据传输的本通道 toChannel.transferFrom(fromChannel, position, count); System.out.println("===将源通道的数据传输的本通道完毕"); //将本通道数据传输到目的通道 fromChannel.transferTo(position, count, to2Channel); System.out.println("===将本通道数据传输到目的通道完毕"); fromChannel.close(); fromFile.close(); toChannel.close(); toFile.close(); to2Channel.close(); to2File.close(); } }
控制输出:
===将源通道的数据传输的本通道完毕
===将本通道数据传输到目的通道完毕
打开文件E:/nio_data_to.txt和E:/nio_data_to2.txt,文件内容如下:
new String to write to file....1494407798722
发表评论
-
文件通道解析二(文件锁,关闭通道)
2017-05-16 23:17 992文件通道解析一(读写操作,通道数据传输等):http://do ... -
文件通道解析一(读写操作,通道数据传输等)
2017-05-16 10:04 1101Reference定义(PhantomRefere ... -
文件通道创建方式综述
2017-05-15 17:39 931Reference定义(PhantomReference,Cl ... -
文件读写方式简单综述后续(文件,流构造)
2017-05-14 23:04 1399Java Socket通信实例:http://donald-d ... -
文件读写方式简单综述
2017-05-14 11:13 1068Java Socket通信实例:http://donald-d ... -
FileChanne定义
2017-05-12 23:28 869文件读写方式简单综述:http://donald-draper ... -
SeekableByteChannel接口定义
2017-05-11 08:43 1120ByteChannel,分散聚集通道接口的定义(SocketC ... -
PipeImpl解析
2017-05-11 08:41 837ServerSocketChannel定义:http://do ... -
Pipe定义
2017-05-10 09:07 839Channel接口定义:http://donald-drape ... -
NIO-Pipe示例
2017-05-10 08:47 846PipeImpl解析:http://donald-draper ... -
DatagramChannelImpl 解析四(地址绑定,关闭通道等)
2017-05-10 08:27 690DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析三(多播)
2017-05-10 08:20 1687DatagramChannelImpl 解析一(初始化):ht ... -
NIO-UDP实例
2017-05-09 12:32 1516DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析二(报文发送与接收)
2017-05-09 09:03 1341DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析一(初始化)
2017-05-08 21:52 1329Channel接口定义:http://donald-drape ... -
MembershipKeyImpl 简介
2017-05-08 09:11 871MembershipKey定义:http://donald-d ... -
DatagramChannel定义
2017-05-07 23:13 1182Channel接口定义:http://donald-drape ... -
MulticastChanne接口定义
2017-05-07 13:45 1067NetworkChannel接口定义:ht ... -
MembershipKey定义
2017-05-06 16:20 829package java.nio.channels; i ... -
SocketChannelImpl 解析四(关闭通道等)
2017-05-05 08:38 2412SocketChannelImpl 解析一(通道连接,发送数据 ...
相关推荐
经过对Flume FileChannel相关源码的分析,导致FileChannel吞吐率下降的主要原因集中于事务的提交过程——commit
7.1 打开一个FileChannel 7.2 从FileChannel通道中读取数据 7.3 向FileChannel中写入数据: 7.4 关闭FileCha
1.1 阻塞 IO 2.3 FileChannel 介绍和示例 2.4 FileChannel 操作详解
【IT十八掌徐培成】Java基础第26天-06.FileChannel-RandomAccessFile-CopyFile.zip
04-Java NIO-Channel-FileChannel(介绍和示例).mp4 05-Java NIO-Channel-FileChannel详解(一).mp4 06-Java NIO-Channel-FileChannel详解(二).mp4 07-Java NIO-Channel-Socket通道-概述.mp4 08-Java NIO-Channel-...
FindFirstFile函数功能描述,及改函数的用法,示例代码
01-Java NIO-课程简介.mp4 05-Java NIO-Channel-FileChannel详解(一).mp4 06-Java NIO-Channel-FileChannel详解(二).mp4 08-Java NIO-Channel-...23-Java NIO-Selector-示例代码(客户端).mp4 24
jruby-stdin-channel JRuby Java 扩展 gem,它从 Java System.in stdin 中提取可中断的FileChannel。 使用这个 gem,在阻塞read方法上调用close将解除阻塞,这与普通的 JRuby $stdin 。 使用close转义阻塞读取仅适用...
h264转码MP4 支持html5播放格式 示例代码: ... FileChannel fChannel = new FileOutputStream(new File("C://a.mp4")).getChannel(); mp4f.writeContainer(fChannel); fChannel.close();
javaSE 关于IO几种读取方式的性能比较 1.二进制数据 2.字符数据 3.压缩文件
使用nio byteBuffer 实现按行读取文件(大文件) 在window/linux/macOS上均测试通过 对于中文乱码也已处理成功 完整注释,可随需求更改 有问题请邮件:mly610865580@126.com
两个通道之间传输数据的方式有两种,分别是:FileChannel 的transferFrom()方法可以将数据从源通道传输到FileChannel中(这个方法在
教程内容涵盖:阻塞和非阻塞IO、Channel通道、Buffer缓冲区、Selector选择器、Pipe管道、FileLock文件锁,以及Path、Files、异步FileChannel和Charset字符编码等,并通过一个多人聊天室的综合案例,把所有的NIO知识...
Java NIO系列教程(七) FileChannel Java NIO系列教程(八) SocketChannel Java NIO系列教程(九) ServerSocketChannel Java NIO系列教程(十) Java NIO DatagramChannel Java NIO系列教程(十一) Pipe Java ...
FileChannel outChannel = null; try { outChannel = new FileOutputStream(outFile).getChannel(); for(String f : files){ ByteBuffer wrap = ByteBuffer.wrap("\r\n".getBytes()); outChannel.write(wrap);...
FileChannel Buffer URLConnection NIO Process HashMap LinkedHashMap TreeMap ConcurrentHashMap ConcurrentLinkedQueue ThreadPool ThreadLocal Reflection ScheduledThreadPool ...
java各类IO流的整理demo,其内包含InputStream/OutputStream BufferedReader流 FileOutputStream/FileInputStream流 PrintWriter流 DataOut/DataIn流 Object流 FileChannel类;
import java.nio.channels.FileChannel; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.filechooser.FileFilter; 实验...
大数据 Flume 框架高频面试题 1、Flume组成,Put事务,Take事务 2、Flume拦截器 3、Flume采集数据会丢失吗?(防止数据丢失的机制) ...5、FileChannel优化 6、Flume Channel容量 7、HDFS Sink小文件处理 等等
Kafka还强调减少数据的序列化和拷贝开销,它会将一些消息组织成Message Set做批量存储和发送,并且客户端在pull数据的时候,尽量以zero-copy的方式传输,利用sendfile(对应java里的 FileChannel.transferTo/...