java.nio.channels.FileChannel 用于读取、写入、映射和操作文件的通道,类的定义:public abstract class FileChannel 。
文件通道在其文件中有一个当前 position,可对其进行查询和修改。该文件本身包含一个可读写的长度可变的字节序列,并且可以查询该文件的当前#size大小。写入的字节超出文件的当前大小时,则增加文件的大小;截取 该文件时,则减小文件的大小。文件可能还有某个相关联的元数据,如访问权限、内容类型和最后的修改时间;此类未定义访问元数据的方法。
下面举几个例子:
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileChannelTest1 {
public static void main(String[] args) throws Exception {
File file = new File(”d:\\note.txt”); // 创建新的文件对象
FileInputStream in = new FileInputStream(file); // 创建新的文件输入字节流
FileOutputStream out = new FileOutputStream(”d:\\1.txt”); // 创建新的文件输出字节流
FileChannel fc = in.getChannel(); // 创建一个新的文件输入通道
FileChannel fout = out.getChannel(); // 创建新的文件输出通道
ByteBuffer buf = ByteBuffer.allocate(1024); // 创建新的字节缓冲区
int c = 0;
while((c = fc.read(buf))!=-1){ // 将字节序列从此通道读入给定的缓冲区
buf.flip();
fout.write(buf); // 将字节序列从给定的缓冲区写入此通道。
buf.clear();
}
fout.close();
fc.close();
in.close();
out.close();
}
}
=========================================================
//文件锁定
import java.io.File;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class FileLockTest1 {
public static void main(String[] args) throws Exception {
File file = new File(”d://note.txt”);
FileOutputStream out = new FileOutputStream(file,true);
FileChannel fout = out.getChannel();
FileLock lock = fout.lock();//独占锁
if(lock!=null){
System.out.println(”文件被锁定了”);
Thread.sleep(20000);
lock.release();
System.out.println(”文件被释放了”);
}
fout.close();
out.close();
}
}
=========================================================
// MappedByteBuffer类 直接字节缓冲区,其内容是文件的内存映射区域。
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
public class MappedByteBufferTest {
public static void main(String[] args) throws Exception{
File file = new File(”d:\\note.txt”);
FileInputStream in = new FileInputStream(file);
FileChannel fc = in.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_ONLY, 0, file.length());
System.out.println(mbb.position()); // 输出结果: 0
//ByteBuffer buf = ByteBuffer.allocate((int)file.length());
byte[] data = new byte[(int)file.length()];
int index = 0;
while(mbb.hasRemaining()){
data[index] = mbb.get();
index++;
}
System.out.println(new String(data));
//用内存映射的方式读是最快的,但是很危险,建议只读
}
分享到:
相关推荐
经过对Flume FileChannel相关源码的分析,导致FileChannel吞吐率下降的主要原因集中于事务的提交过程——commit
7.1 打开一个FileChannel 7.2 从FileChannel通道中读取数据 7.3 向FileChannel中写入数据: 7.4 关闭FileCha
【IT十八掌徐培成】Java基础第26天-06.FileChannel-RandomAccessFile-CopyFile.zip
java各类IO流的整理demo,其内包含InputStream/OutputStream BufferedReader流 FileOutputStream/FileInputStream流 PrintWriter流 DataOut/DataIn流 Object流 FileChannel类;
H264TrackImpl h264TrackImpl = new H264TrackImpl(new ... FileChannel fChannel = new FileOutputStream(new File("C://a.mp4")).getChannel(); mp4f.writeContainer(fChannel); fChannel.close();
jruby-stdin-channel JRuby Java 扩展 gem,它从 Java System.in stdin 中提取可中断的FileChannel。 使用这个 gem,在阻塞read方法上调用close将解除阻塞,这与普通的 JRuby $stdin 。 使用close转义阻塞读取仅适用...
1.1 阻塞 IO 2.3 FileChannel 介绍和示例 2.4 FileChannel 操作详解
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-...
javaSE 关于IO几种读取方式的性能比较 1.二进制数据 2.字符数据 3.压缩文件
FindFirstFile函数功能描述,及改函数的用法,示例代码
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-...
使用nio byteBuffer 实现按行读取文件(大文件) 在window/linux/macOS上均测试通过 对于中文乱码也已处理成功 完整注释,可随需求更改 有问题请邮件:mly610865580@126.com
两个通道之间传输数据的方式有两种,分别是:FileChannel 的transferFrom()方法可以将数据从源通道传输到FileChannel中(这个方法在
实验题目 编写一个java应用程序,将一个包含多个子目录和文件的目录复制到另外一个指定的目录下。...更进一步的熟悉了static 、getDrives等等与之用法相类似的语法类。增强了自己学好JAVA的信心。
教程内容涵盖:阻塞和非阻塞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);...
IOAGoGo! IOAGoGo 是一个使用核心 Java 中可用的各种不同方法进行基本 IO 基准测试的库。 它是可扩展的,应该可以在 Java 所在的... 这次运行表明,使用直接分配的 ByteBuffer 和 FileChannel 读取至少 32KB,但不