`
- 浏览:
300239 次
- 性别:
- 来自:
河南
-
清单一:
- import java.io.*;
- import java.nio.*;
- import java.nio.channels.*;
- public class GetChannel
- {
- private static final int BSIZE = 1024;
- public static void main(String[] args)throws IOException
- {
- FileChannel fc = new FileOutputStream ("data.txt").getChannel();
- fc.write(ByteBuffer.wrap("some txt".getBytes()));
-
- fc.close();
- fc = new RandomAccessFile("data.txt","rw").getChannel();
- fc.position(fc.size());
-
- fc.write(ByteBuffer.wrap("some more".getBytes()));
- fc.close();
- fc =new FileInputStream("data.txt").getChannel();
- ByteBuffer bf = ByteBuffer.allocate(BSIZE);
-
-
-
-
- fc.read(bf);
- bf.flip();
- while(bf.hasRemaining())
- System.out.print((char)bf.get());
- }
- }
清单二:
- import java.io.*;
- import java.nio.*;
- import java.nio.channels.*;
- public class ChannelCopy
- {
- private static final int BSIZE = 1024;
- public static void main(String [] args)throws IOException
- {
- if (args.length!=2)
- {
- System.out.println("argument:sourcefile destfile");
- System.exit(1);
- }
- FileChannel
- in = new FileInputStream (args[0]).getChannel(),
- out = new FileOutputStream (args[1]).getChannel();
- ByteBuffer bb = ByteBuffer.allocate(BSIZE);
- while (in.read(bb)!=-1)
- {
- bb.flip();
- out.write(bb);
- bb.clear();
- }
- }
- }
清单三
- import java.io.*;
- import java.nio.*;
- import java.nio.channels.*;
- public class TransferTo
- {
- public static void main(String [] args) throws IOException
- {
- if(args.length!=2)
- {
- System.out.println("argument: sourcefile destfile");
- System.exit(1);
- }
- FileChannel
- in = new FileInputStream(args[0]).getChannel(),
- out = new FileOutputStream(args[1]).getChannel();
- in.transferTo(0,in.size(),out);
-
-
-
- }
- }
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
经过对Flume FileChannel相关源码的分析,导致FileChannel吞吐率下降的主要原因集中于事务的提交过程——commit
7.1 打开一个FileChannel 7.2 从FileChannel通道中读取数据 7.3 向FileChannel中写入数据: 7.4 关闭FileCha
【IT十八掌徐培成】Java基础第26天-06.FileChannel-RandomAccessFile-CopyFile.zip
jruby-stdin-channel JRuby Java 扩展 gem,它从 Java System.in stdin 中提取可中断的FileChannel。 使用这个 gem,在阻塞read方法上调用close将解除阻塞,这与普通的 JRuby $stdin 。 使用close转义阻塞读取仅适用...
FindFirstFile函数功能描述,及改函数的用法,示例代码
java各类IO流的整理demo,其内包含InputStream/OutputStream BufferedReader流 FileOutputStream/FileInputStream流 PrintWriter流 DataOut/DataIn流 Object流 FileChannel类;
1.1 阻塞 IO 2.3 FileChannel 介绍和示例 2.4 FileChannel 操作详解
01-Java NIO-课程简介.mp4 02-Java NIO-概述.mp4 03-Java NIO-Channel-概述.mp4 ...13-Java NIO-Buffer-基本使用.mp4 14-Java NIO-Buffer-三个属性和类型.mp4 15-Java NIO-Buffer-分配和读写数据.mp4
javaSE 关于IO几种读取方式的性能比较 1.二进制数据 2.字符数据 3.压缩文件
实验题目 编写一个java应用程序,将一个包含多个子目录和文件的目录复制到另外一个指定的目录下。...更进一步的熟悉了static 、getDrives等等与之用法相类似的语法类。增强了自己学好JAVA的信心。
H264TrackImpl h264TrackImpl = new H264TrackImpl(new ... FileChannel fChannel = new FileOutputStream(new File("C://a.mp4")).getChannel(); mp4f.writeContainer(fChannel); fChannel.close();
13-Java NIO-Buffer-基本使用.mp4 14-Java NIO-Buffer-三个属性和类型.mp4 17-Java NIO-Buffer-缓冲区分片.mp4 18-Java NIO-Buffer-只读缓冲区.mp4 19-Java NIO-Buffer-直接缓冲区.mp4 21-Java NIO-Selector-概述....
使用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 ...
非易失性映射的字节缓冲将添加新的 JDK 特定文件映射模式,该模式允许 FileChannel API 用于创建引用非易失性内存(NVM)的 MappedByteBuffer 实例。 358:Helpful NullPointerExceptions 改进 ...
FileChannel Buffer URLConnection NIO Process HashMap LinkedHashMap TreeMap ConcurrentHashMap ConcurrentLinkedQueue ThreadPool ThreadLocal Reflection ScheduledThreadPool ...
FileChannel outChannel = null; try { outChannel = new FileOutputStream(outFile).getChannel(); for(String f : files){ ByteBuffer wrap = ByteBuffer.wrap("\r\n".getBytes()); outChannel.write(wrap);...
源(如java.nio.channels.ReadableByteChannel和java.nio.channels.FileChannel一起使用的方法,它们允许对来自各种源的位和字节进行高效、惰性的访问和操作。 十六进制和二进制字符串字面量是通过所支持的hex和bin...