在Java中使用FileChannel和ByteBuffer对文件进行读写

过去,我讨论过RandomAccessFile以及如何将其用于在Java中进行更快的IO,在本Java NIO教程中,我们将了解如何通过使用FileChannel和ByteBuffer来使用读/写数据。

Channel提供了一种从文件读取数据的替代方法,它提供了比InputStream或OutputStream更好的性能。 也可以在阻止和非阻止模式下打开它。 虽然, FileChannles是读/写通道,并且它们始终处于阻塞状态 ,但不能将其置于非阻塞模式。 RandomAccessFile类将文件视为字节数组。

您可以在数组的任何位置写入数据,也可以从任何位置读取数据。 为此,它使用保存当前位置的指针,并提供诸如seek()几种方法来移动该指针。 一旦定位正确,就可以从RandomAccessFile获取FileChannel并开始从文件中读取数据。 顺便说一下,JDK 7还引入了NIO 2,它使处理文件和目录变得更加容易。 阅读Anghel Leonard 撰写的Pro Java 7 NIO.2以了解更多信息。

如何使用FileChannel和ByteBuffer读取/写入文件

在开始编码之前,让我们修改Java NIO中Channel和Buffer的基本概念。 一言以蔽之,缓冲区与通道一起工作。 通道是传输数据的管道,缓冲区是数据传输的源和目标。 对于写操作,要写入的数据放置在缓冲区中,该缓冲区将传递到通道,而不是通道从缓冲区读取该数据并将其写入文件。

同样,在读取的情况下,通道会将数据放入您从文件 , 网络或任何其他来源提供的缓冲区中。 由于相同的缓冲区用于读取和写入,即您将数据写入缓冲区,而通道将其读取以写入文件,因此,一旦完成写入缓冲区,就必须调用flip()方法。 flip()方法更改指针,并允许您从缓冲区读取数据。 Java中的缓冲区有三种类型, 直接缓冲区,非直接缓冲区和映射缓冲区 。 在此示例中,我们将使用直接字节缓冲区。

使用FileChannel和Buffer读取/写入数据的步骤

这是逐步指南,开始使用RandomAccessFileFileChannelByteBuffer从文件读取数据:

  1. 在读取/写入模式下使用RandomAccessFile打开要读取/写入的文件。
  2. 调用RandomAccessFile的getChannel()方法以获取FileChannel。 返回的通道的位置将始终等于getFilePointer()方法返回的此对象的文件指针偏移量。
  3. 使用ByteBuffer.allocate()方法创建一个ByteBuffer。
  4. 使用各种数据存储到字节缓冲区put()方法例如putInt() putLong()
  5. 翻转缓冲区,以便Channel可以从缓冲区读取数据并写入文件。 flip()方法更改指针,并允许您从缓冲区读取数据。
  6. 调用FileChannel的write()方法。
  7. 关闭文件通道
  8. 关闭RandomAccessFile。

要注意的另一个重要点是,可以使用相同的缓冲区进行读写,但是需要翻转它。 现在,让我们看一个示例Java程序,该程序使用Java中的FileChannel和ByteBuffer从文件读取/写入数据。 在Memory Mapped File之后 ,这是从Java中读取和写入文件的第二快的方法。

ByteBuffer FileChannel Java

Java程序,使用FileChannel和ByteBuffer从文件读取/写入

这是示例程序,以演示如何使用FileChannel和ByteBuffer类从文件(可以是二进制文件或文本文件)读取和写入数据。 我还使用抽象来创建一个名为Persistable的接口,该接口提供了两种方法persist()recover() 。 任何实现此接口的对象都可以保存和加载,但是如何保存和加载它们则留给实现者,即可以像我们一样使用Chanel和Buffer,也可以使用旧方法在Java中读取/写入文件。 。

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;/*** Java Program to read and write on RandomAccessFile in Java* using FileChannle and ByteBuffer.** @author Javin*/
public class FileChannelDemo {public static void main(String args[]) {Tablet ipad = new Tablet("Apple", true, 1000);System.out.println("Writing into RandomAcessFile : " + ipad);write("tablet.store", ipad);Tablet fromStore = new Tablet();read("tablet.store", fromStore);System.out.println("Object read from RandomAcessFile : " + fromStore);}/** Method to write data into File using FileChannel and ByteBuffeer*/public static void write(String filename, Persistable object) {try {// Creating RandomAccessFile for writingRandomAccessFile store = new RandomAccessFile("tablet", "rw");// getting FileChannel from fileFileChannel channel = store.getChannel();// creating and initializing ByteBuffer for reading/writing dataByteBuffer buffer = ByteBuffer.allocate(2048);// an instance of Persistable writing into ByteBufferobject.persist(buffer);// flip the buffer for writing into filebuffer.flip();int numOfBytesWritten = channel.write(buffer); // writing into FileSystem.out.println("number of bytes written : " + numOfBytesWritten);channel.close(); // closing file channelstore.close(); // closing RandomAccess file} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** Method to read data from File using FileChannel and ByteBuffeer*/public static void read(String filename, Persistable object) {try {// Opening RandomAccessFile for reading dataRandomAccessFile store = new RandomAccessFile("tablet", "rw");// getting file channelFileChannel channel = store.getChannel();// preparing buffer to read data from fileByteBuffer buffer = ByteBuffer.allocate(1024);// reading data from file channel into bufferint numOfBytesRead = channel.read(buffer);System.out.println("number of bytes read : " + numOfBytesRead);// You need to filp the byte buffer before readingbuffer.flip();// Recovering objectobject.recover(buffer);channel.close();store.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
}

我们的界面为抽象的读写机制。 这也是提供抽象的接口的实际用法 ,将要做的事情与要做的事情分开。 像这个界面,只说坚持并恢复,不说你怎么做。

interface Persistable {public void persist(ByteBuffer buffer);public void recover(ByteBuffer buffer);
}

实现Persistable使其可读写的具体类:

class Tablet implements Persistable {private String brand;private boolean isCellular;private long cost; // in US Dollarspublic Tablet() {brand = "";}public Tablet(String brand, boolean isCellular, long cost) {this.brand = brand;this.isCellular = isCellular;this.cost = cost;}public final String getBrand() {return brand;}public final boolean isCellular() {return isCellular;}public final long getCost() {return cost;}public final void setBrand(String brand) {this.brand = brand;}public final void setCellular(boolean isCellular) {this.isCellular = isCellular;}public final void setCost(long cost) {this.cost = cost;}@Overridepublic void persist(ByteBuffer buffer) {byte[] strBytes = brand.getBytes();buffer.putInt(strBytes.length);buffer.put(strBytes, 0, strBytes.length);buffer.put(isCellular == true ? (byte) 1 : (byte) 0);buffer.putLong(cost);}@Overridepublic void recover(ByteBuffer buffer) {int size = buffer.getInt();byte[] rawBytes = new byte[size];buffer.get(rawBytes, 0, size);this.brand = new String(rawBytes);this.isCellular = buffer.get() == 1 ? true : false;this.cost = buffer.getLong();}@Overridepublic String toString() {return "Tablet [brand=" + brand + ", isCellular=" + isCellular + ", cost=" + cost + "]";}}Output:
Writing into RandomAcessFile : Tablet [brand=Apple, isCellular=true, cost=1000]
number of bytes written : 18
number of bytes read : 1024
Object read from RandomAcessFile : Tablet [brand=Apple, isCellular=true, cost=1000]

警告

将对象的内容写入其中之后,不要忘记翻转字节缓冲区,因为文件通道需要读取它才能将数据写入RandomAccessFile。 如果忘记在调用FileChannel.write()之前先调用flip()方法,那么最终将什么都没有写入文件中。

同样,在将数据从文件读入缓冲区后,再次翻转它,以便可以从缓冲区读数据到对象的常用内容。 许多Java程序员会犯这样的错误,即写后不会翻转,并最终导致调试时间过长,因为要么什么都没有写到文件,要么什么都看不到。

这就是如何在Java中使用FileChannel和ByteBuffer读取/写入文件的全部内容。 在本例中,我向您展示了如何使用FileChannel和ByteBuffer读写RandomAccessFile,但是您可以应用相同的技术从Java程序读取任何其他文本或二进制文件。

翻译自: https://www.javacodegeeks.com/2016/01/readingwriting-tofrom-files-using-filechannel-bytebuffer-java.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/336587.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

tp5防止sql注入mysql_PHP+Mysql防止SQL注入的方法(life)

这篇文章介绍的内容是关于PHPMysql防止SQL注入的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下我的官方群点击此处。方法一:mysql_real_escape_string -- 转义 SQL 语句中使用的字符串中的特殊字符&#xff0…

neo4j 关系属性_Neo4j:特定关系与一般关系+属性

neo4j 关系属性为了在Neo4j查询中获得最佳的遍历速度,我们应该使关系类型尽可能具体 。 让我们看一下几周前我在Skillsmatter上发表的“ 建模建议引擎建模 ”演讲中的一个例子。 我需要决定如何为成员和事件之间的“ RSVP”关系建模。 一个人可以对某个事件表示“…

(Tree)二叉树基本操作

//二叉树操作#include<iostream> #include<algorithm> using namespace std;//准备数据#define MANLEN 20 typedef char DATA; typedef struct CBT {DATA data;struct CBT*left;struct CBT*right; }CBTT;//初始二叉树,设置一个根节点CBTT* InitTree() {CBTT* node;…

mysql多实例主从_window 下 mysql 单机多实例以及主从同步

主MySQLmy.ini 配置# mysql server 的唯一idserver_id 3306log-binlog# 需要同步的数据库binlog-do-dbfaner# 不需要同的数据库binlog-ignore-dbmysql创建从数据库连接用户grant replication slave on *.* to root10% identified by 123456从MySQLmy.ini 参数配置server_id 3…

jvm ide_通过微调JVM Garbage Collector减少Java IDE滞后

jvm ide有没有想过为什么Eclipse / Netbeans会一直暂停一段时间&#xff1f; 特别是当您想向亲爱的同事展示代码中的内容时&#xff1f; 感到尴尬和尴尬&#xff0c;不是吗&#xff1f; 我发现大多数情况下IDE会由于执行垃圾收集器而暂停。 JVM设计中的微妙元素很少起作用&…

LeetCode 206. 反转链表

解法一:迭代法 class Solution { public:ListNode* reverseList(ListNode* head) {if(!head)return NULL;auto p1 head, p2 p1->next;//定义两个相邻指针while(p2) {auto p3 p2->next; //p3存储p2的后继节点p2->next p1; //后面节点指针指向前面的节点p1 p2;//双…

compareto 和_清洁责任–摆脱均等,compareTo和toString

compareto 和您是否看过Java中Object类的javadoc&#xff1f; 大概。 您倾向于时不时地到达那里&#xff0c;然后沿着继承树进行挖掘。 您可能已经注意到的一件事是&#xff0c;每个类都必须继承许多方法。 实现自己而不是坚持使用原始方法的最喜欢的方法可能是.toString&#…

2008r2配置 iis mysql php_Windows 2008 R2服务器配置文档iis+php+mysql

关闭防火墙关闭防火墙打开桌面远程连接修复系统依赖文件IIS配置添加角色创建一个网站验证iis是否成功删除默认创建的网站添加网站PHP配置安装解压&#xff0c;改名创建一个php.ini&#xff0c;php.ini由php.ini-production改名得到如果输入php -v后出现如下报错解决方法如下(安…

Leetcode 260. 只出现一次的数字 III

原题链接 解法&#xff1a;异或操作 vector<int> findNumsAppearOnce(vector<int>& nums) {int eO 0, eOhasOne 0;for (int curNum : nums) {eO ^ curNum;}int rightOne eO & (~eO 1); //取出最右侧的1for (int cur : nums) {if ((cur & rightO…

接口入口在什么地方_弱电工程施工图审查要点?有哪些地方需要审核?审核要求是什么?...

前言&#xff1a;弱电工程施工图审核标准是什么呢&#xff1f;需要审核哪些方面呢&#xff1f;有没有可以参考的地方&#xff1f;今天就分享一套弱电系统施工图审核技术要求&#xff0c;可以参考正文&#xff1a;1.设计文件设计文件是否完整(包括设计说明、平面图、系统图(单体…

drill apache_Apache Drill 1.4性能增强的简要概述

drill apache今天&#xff0c;我们很高兴地宣布&#xff0c;MapR发行版中现已提供Apache Drill 1.4。 钻1.4是MAPR生产就绪和支持的版本&#xff0c;可以从下载这里 &#xff0c;找到1.4版本说明这里 。 Drill 1.4以其高度灵活和可扩展的体系结构为基础&#xff0c;带来了多种…

Leetcode 237. 删除链表中的节点

原题链接 解法&#xff1a;后面的节点直接覆盖前面的节点 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/void deleteNode(ListNode* node) {ListNode* pnode->ne…

docker java mysql_Docker 搭建 MySQL 服务

安装 Docker请参考我的另一篇文章建立镜像拉取镜像# 拉取最新版本镜像docker pull mysql# 拉取执行版本镜像docker pull mysql:版本号检查拉取是否成功docker images创建数据库容器(不建立数据映射)docker run -d --name mysql --rm -p 3306:3306 -e MYSQL_ROOT_PASSWORD123456…

php cdi_Swing应用程序中的CDI事件将UI和事件处理分离

php cdi在愉快地围绕CDI构建我的代码几年之后&#xff0c;使用它根据众所周知的模式来构造我的代码非常自然。 CDI是一种旨在在Java EE应用程序服务器中使用的依赖项注入机制&#xff0c;这可能被视为不利。 但是&#xff0c;我想证明它可以在Java SE应用程序中使用并且具有巨大…

虽然你没有考入清华大学,但是如果你有对于计算机的向往!!!请看过来

本文分享来自清华大学计科的课程&#xff0c;希望对热爱计算机的人有帮助。 Github清华大学计算机课程

Mysql存储函数实现增删改查_使用存储过程操作数据库(实现增删改查)

使用存储过程操作数据库添加数据&#xff1a;--创建一个存储过程&#xff0c;向表中插入一条数据create proc usp_insertintoTbClassclassName nvarchar(50),classDes nvarchar(100)asbegininsert into TbClass values( ClassName,classDes)endexec usp_insertintoTbClass clas…

计算机实现数论 奇偶排列问题

#include <iostream> using namespace std;int main() {int sub 0;int a[5] {5,4,1,3,2};for (int i 0; i < 5; i){for (int j i 1; j <5; j){if (a[i] > a[j]){sub 1;}}}if (sub % 2 0)cout << "此排列是偶排列" << endl;else{…

java关闭窗口函数_2016年将是Java终于拥有窗口函数的那一年!

java关闭窗口函数你没听错。 到目前为止&#xff0c;出色的窗口功能是SQL独有的功能。 甚至复杂的函数式编程语言似乎仍然缺少这种漂亮的功能&#xff08;如果我错了&#xff0c;请纠正我&#xff0c;Haskell伙计们&#xff09;。 我们撰写了许多有关窗口函数的博客文章&#…

mysql抓包_mysql抓包工具

简介MySQL Sniffer 是一个基于 MySQL 协议的抓包工具&#xff0c;实时抓取 MySQLServer 端或 Client 端请求&#xff0c;并格式化输出。输出内容包括访问时间、访问用户、来源 IP、访问 Database、命令耗时、返回数据行数、执行语句等。有批量抓取多个端口&#xff0c;后台运行…