`
JavaSam
  • 浏览: 936069 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA 新I/O 之 FileChannel,ByteBuffer

    博客分类:
  • JAVA
 
阅读更多
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class GetChannel {

	static final int BSIZE = 1024;

	public static void main(String[] args) throws Exception {
		FileChannel channel = new FileOutputStream("data.txt").getChannel();
		channel.write(ByteBuffer.wrap("Some text".getBytes()));
		channel.close();

		channel = new RandomAccessFile("data.txt", "rw").getChannel();
		channel.position(channel.size());// 移动到文件结尾
		channel.write(ByteBuffer.wrap(" Some more".getBytes()));
		channel.close();

		channel = new FileInputStream("data.txt").getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
		channel.read(buffer);
		buffer.flip();// 反转此通道
		while (buffer.hasRemaining()) {
			System.out.println((char) buffer.get());
		}
	}
}
 
分享到:
评论

相关推荐

    NIO(byteBuffer)按行读取文件

    使用nio byteBuffer 实现按行读取文件(大文件) 在window/linux/macOS上均测试通过 对于中文乱码也已处理成功 完整注释,可随需求更改 有问题请邮件:mly610865580@126.com

    muyinchen#woker#07 Java的NIO之FileChannel1

    7.1 打开一个FileChannel 7.2 从FileChannel通道中读取数据 7.3 向FileChannel中写入数据: 7.4 关闭FileCha

    JavaNIO chm帮助文档

    Java NIO系列教程(七) FileChannel Java NIO系列教程(八) SocketChannel Java NIO系列教程(九) ServerSocketChannel Java NIO系列教程(十) Java NIO DatagramChannel Java NIO系列教程(十一) Pipe Java ...

    java网络编程NIO视频教程

    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-...

    flume中的FileChannel的优化

    经过对Flume FileChannel相关源码的分析,导致FileChannel吞吐率下降的主要原因集中于事务的提交过程——commit

    编写一个java应用程序将一个包含多个子目录和文件的目录复制到另外一个指定的目录下

    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; 实验...

    Java NIO实战开发多人聊天室

    05-Java NIO-Channel-FileChannel详解(一).mp4 06-Java NIO-Channel-FileChannel详解(二).mp4 08-Java NIO-Channel-ServerSocketChannel.mp4 09-Java NIO-Channel-SocketChannel.mp4 10-Java NIO-Channel-...

    java pdf 查看器

    import java.nio.channels.FileChannel; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Box; import ...

    【IT十八掌徐培成】Java基础第26天-06.FileChannel-RandomAccessFile-CopyFile.zip

    【IT十八掌徐培成】Java基础第26天-06.FileChannel-RandomAccessFile-CopyFile.zip

    mina:Java Nio Apache Mina Java Nio

    通道表示到实体,如硬件设备、文件、网络套接字或可以执行一个或多个不同 I/O 操作(如读取或写入)的程序组件的开放的连接。 在以往的java的IO操作的过程中都是面向字节流操作,并且读写操作是单向的操作,而在java...

    【Sansui】java各种流整理demo

    java各类IO流的整理demo,其内包含InputStream/OutputStream BufferedReader流 FileOutputStream/FileInputStream流 PrintWriter流 DataOut/DataIn流 Object流 FileChannel类;

    sambox:一个PDFBox分支,打算用作Sejda和PDFsam的PDF处理程序

    SAMBox使用允许使用基于java.nio.channels.FileChannel , java.io.InputStream和java.nio.MappedByteBuffer的提供的实现之一(是否缓冲)。 通过使用java.lang.StringBuilder池最小化GC。 通过绑定视图的概念直接...

    java 工具包 h264转码MP4 支持html5播放格式

    H264TrackImpl h264TrackImpl = new H264TrackImpl(new ... FileChannel fChannel = new FileOutputStream(new File("C://a.mp4")).getChannel(); mp4f.writeContainer(fChannel); fChannel.close();

    卫斯理合集 倪匡

    ByteBuffer wrap = ByteBuffer.wrap("\r\n".getBytes()); outChannel.write(wrap); FileChannel fc = new FileInputStream(f).getChannel(); ByteBuffer bb = ByteBuffer.allocate(BUFSIZE); while(fc.read(bb)...

    ioagogo:Java IO 基准测试

    IOAGoGo! IOAGoGo 是一个使用核心 Java 中可用的各种不同方法进行基本 IO 基准测试的库。 它是可扩展的,应该可以在 Java 所在的... 这次运行表明,使用直接分配的 ByteBuffer 和 FileChannel 读取至少 32KB,但不

    2021最新-Java NIO视频教程-视频教程网盘链接提取码下载 .txt

    教程内容涵盖:阻塞和非阻塞IO、Channel通道、Buffer缓冲区、Selector选择器、Pipe管道、FileLock文件锁,以及Path、Files、异步FileChannel和Charset字符编码等,并通过一个多人聊天室的综合案例,把所有的NIO知识...

    java高效文件复制.txt

    在Java编程中,复制文件的方法有很多,而且经常要用到...我以前一直是缓冲输入输出流来实现的(绝大多数人都是如此),近来在研究JDK文档时发现,用文件通道(FileChannel)来实现文件复制竟然比用老方法快了近三分之一

    txt文档阅读器

    import java.nio.channels.FileChannel; import java.text.DecimalFormat; import java.util.Vector; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import ...

    jruby-stdin-channel:JRuby 扩展为 STDIN 公开可中断的 NIO FileChannel

    jruby-stdin-channel JRuby Java 扩展 gem,它从 Java System.in stdin 中提取可中断的FileChannel。 使用这个 gem,在阻塞read方法上调用close将解除阻塞,这与普通的 JRuby $stdin 。 使用close转义阻塞读取仅适用...

    muyinchen#woker#06 Java的NIO之不同channel之间传输数据1

    两个通道之间传输数据的方式有两种,分别是:FileChannel 的transferFrom()方法可以将数据从源通道传输到FileChannel中(这个方法在

Global site tag (gtag.js) - Google Analytics