Java RandomAccessFile provides the facility to read and write data to a file. RandomAccessFile works with file as large array of bytes stored in the file system and a cursor using which we can move the file pointer position.

Java RandomAccessFile提供了将数据读取和写入文件的功能。 RandomAccessFile与文件一起使用,作为存储在文件系统中的大字节数组以及一个游标,通过该游标我们可以移动文件指针的位置。

Java RandomAccessFile (Java RandomAccessFile)

RandomAccessFile class is part of Java IO. While creating the instance of RandomAccessFile in java, we need to provide the mode to open the file. For example, to open the file for read only mode we have to use “r” and for read-write operations we have to use “rw”.

RandomAccessFile类是Java IO的一部分。 在Java中创建RandomAccessFile实例时,我们需要提供打开文件的模式。 例如,要以只读模式打开文件,我们必须使用“ r”,而对于读写操作,我们必须使用“ rw”。

Using file pointer, we can read or write data from random access file at any position. To get the current file pointer, you can call getFilePointer() method and to set the file pointer index, you can call seek(int i) method.

使用文件指针,我们可以在任何位置从随机访问文件读取或写入数据。 若要获取当前文件指针,可以调用getFilePointer()方法,并可以设置seek(int i)方法来设置文件指针索引。

If we write data at any index where data is already present, it will override it.

如果我们在已经存在数据的任何索引处写入数据,它将覆盖它。

Java RandomAccessFile阅读示例 (Java RandomAccessFile read example)

We can read byte array from a file using RandomAccessFile in java. Below is the pseudo code to read file using RandomAccessFile.

我们可以使用Java中的RandomAccessFile从文件读取字节数组。 下面是使用RandomAccessFile 读取文件的伪代码。

RandomAccessFile raf = new RandomAccessFile("file.txt", "r");
raf.seek(1);
byte[] bytes = new byte[5];
raf.read(bytes);
raf.close();
System.out.println(new String(bytes));

In the first line, we are creating RandomAccessFile instance for the file in read-only mode.

在第一行中,我们将以只读模式为文件创建RandomAccessFile实例。

Then in the second line, we are moving the file pointer to index 1.

然后在第二行中,将文件指针移动到索引1。

We have created a byte array of length 5, so when we are calling read(bytes) method, then 5 bytes are read from file to the byte array.

我们创建了一个长度为5的字节数组,因此,当我们调用read(bytes)方法时,会将5个字节从文件读取到字节数组。

Finally, we are closing the RandomAccessFile resource and printing the byte array to console.

最后,我们将关闭RandomAccessFile资源,并将字节数组打印到控制台。

Java RandomAccessFile编写示例 (Java RandomAccessFile write example)

Here is a simple example showing how to write data to a file using RandomAccessFile in java.

这是一个简单的示例,显示了如何使用Java中的RandomAccessFile将数据写入文件。

RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
raf.seek(5);
raf.write("Data".getBytes());
raf.close();

Since RandomAccessFile treats the file as a byte array, write operation can override the data as well as it can append to a file. It all depends on the file pointer position. If the pointer is moved beyond the file length and then write operation is called, then there will be junk data written in the file. So you should take care of this while using write operation.

由于RandomAccessFile将文件视为字节数组,因此写操作可以覆盖数据以及可以附加到文件。 这完全取决于文件指针的位置。 如果将指针移到文件长度之外,然后调用写操作,则文件中将写入垃圾数据。 因此,在使用写操作时应注意这一点。

RandomAccessFile附加示例 (RandomAccessFile append example)

All we need is to make sure file pointer is at the end of the file to append to a file. Below is the code for append to file using RandomAccessFile.

我们需要做的就是确保文件指针位于文件末尾以追加到文件中。 以下是使用RandomAccessFile追加到文件的代码。

RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
raf.seek(raf.length());
raf.write("Data".getBytes());
raf.close();

Java RandomAccessFile示例 (Java RandomAccessFile Example)

Here is a complete java RandomAccessFile example with different read and write operations.

这是具有不同读写操作的完整Java RandomAccessFile示例。

package com.journaldev.files;import java.io.IOException;
import java.io.RandomAccessFile;public class RandomAccessFileExample {public static void main(String[] args) {try {// file content is "ABCDEFGH"String filePath = "/Users/pankaj/Downloads/source.txt"; System.out.println(new String(readCharsFromFile(filePath, 1, 5)));writeData(filePath, "Data", 5);//now file content is "ABCDEData"appendData(filePath, "pankaj");//now file content is "ABCDEDatapankaj"} catch (IOException e) {e.printStackTrace();}}private static void appendData(String filePath, String data) throws IOException {RandomAccessFile raFile = new RandomAccessFile(filePath, "rw");raFile.seek(raFile.length());System.out.println("current pointer = "+raFile.getFilePointer());raFile.write(data.getBytes());raFile.close();}private static void writeData(String filePath, String data, int seek) throws IOException {RandomAccessFile file = new RandomAccessFile(filePath, "rw");file.seek(seek);file.write(data.getBytes());file.close();}private static byte[] readCharsFromFile(String filePath, int seek, int chars) throws IOException {RandomAccessFile file = new RandomAccessFile(filePath, "r");file.seek(seek);byte[] bytes = new byte[chars];file.read(bytes);file.close();return bytes;}}

That’s all for RandomAccessFile in java.

Java中的RandomAccessFile就是这样。

翻译自: https://www.journaldev.com/921/java-randomaccessfile-example

Java RandomAccessFile示例相关推荐

  1. Java二进制文件示例

    Java二进制文件示例 通过以下案例具体说明如何将数据存入二进制文件以及如何读取二进制文件. 问题:输入5个学生的信息(包含学号.姓名.3科成绩),统计各学生的总分,然后将学生信息和统计结果存入二进制 ...

  2. 大数据 java 代码示例_功能Java示例 第7部分–将失败也视为数据

    大数据 java 代码示例 这是称为" Functional Java by Example"的系列文章的第7部分. 我在本系列的每个部分中开发的示例是某种"提要处理程序 ...

  3. java方法示例注释 @_Java 8中的功能接口是什么? @功能注释和示例

    java方法示例注释 @ 函数接口是Java 8最重要的概念之一,实际上为lambda表达式提供了动力,但是许多开发人员没有首先了解函数接口在Java 8中的作用就花了很多精力来理解它,并花时间学习l ...

  4. java 方法 示例_Java 9示例–收集的工厂方法–创建不可修改的列表,集合和映射...

    java 方法 示例 大家好,这是我在该博客上发表的有关Java 9功能的第一篇文章,今天您将了解我最喜欢的功能"收集的工厂方法" ,它是JEP 269的一部分.JEP代表JDK增 ...

  5. java 观察者模式示例_观察者设计模式示例

    java 观察者模式示例 本文是我们名为" Java设计模式 "的学院课程的一部分. 在本课程中,您将深入研究大量的设计模式,并了解如何在Java中实现和利用它们. 您将了解模式如 ...

  6. java 泛型示例_使用Java泛型的模板方法模式示例

    java 泛型示例 如果您发现除了某些部分外,您的许多例程完全相同,那么您可能需要考虑使用Template Method来消除容易出错的代码重复 . 这是一个示例:下面是两个做类似事情的类: 实例化并 ...

  7. java 设计模式 示例_Java中的状态设计模式–示例教程

    java 设计模式 示例 状态模式是行为设计模式之一 . 当对象根据其内部状态更改其行为时,将使用状态设计模式. 如果必须根据对象的状态更改其行为,则可以在对象中使用状态变量,并使用if-else条件 ...

  8. java 设计模式 示例_Java中的访问者设计模式–示例教程

    java 设计模式 示例 访客模式是行为设计模式之一 . 当我们必须对一组相似类型的对象执行操作时,将使用访问者模式. 借助访问者模式,我们可以将操作逻辑从对象移动到另一个类. 例如,假设有一个购物车 ...

  9. java 观察者模式示例_Java中的观察者设计模式-示例教程

    java 观察者模式示例 观察者模式是行为设计模式之一 . 当您对对象的状态感兴趣并希望在发生任何更改时得到通知时,观察者设计模式很有用. 在观察者模式中,监视另一个对象状态的对象称为Observer ...

最新文章

  1. maven 编译mybatis项目时xml文件无法编译到target目录下的解决方法
  2. RocketMQ带你快速入门
  3. LwIP之netbuf
  4. 结构设计模式 - 代理设计模式
  5. java适应性自旋锁_深夜!小胖问我,什么是自旋锁?怎么使用?适用场景是啥?...
  6. java 生成Excel开门篇
  7. 拓端tecdat|决策树算法建立电信客户流失模型
  8. css元素居中的几种方式
  9. Atitit 查询优化器的流程attilax总结
  10. EDA课程设计代码汇总(信号发生器、抢答器、频率计、秒表、密码锁、计算器、VGA、PS2)
  11. (二)NIST CSF-框架基础
  12. 大数据综合实验的踩坑总结(林子雨)
  13. 位图的实战场景及源码分析
  14. iPhone模拟器截图
  15. 当居住产业挺进深水区,数字化能衍生出什么机会?
  16. html布局属性,hTML之FLEX布局属性
  17. Redis——底层数据结构原理
  18. 一对一直播源码、一对一聊天app源码前端后台功能说明
  19. python入门技能树评测-积跬步以至千里
  20. DES加密和解密算法

热门文章

  1. PHP正则表达式详解(三)
  2. pcie inbound、outbound及EP、RC间的互相訪问
  3. android 之反编译
  4. 法拉利杀手Koenigsegg CCX
  5. [转载] python 1
  6. Django基础--4
  7. web自动化测试python+selenium学习总结----selenium安装、浏览器驱动下载
  8. Django(6)-orm
  9. 第一次作业+105032014142
  10. 【转】:TCP/IP详解学习笔记系列