首页 Java java教程 随机访问文件

随机访问文件

Jul 18, 2024 pm 07:08 PM

Java 提供了 RandomAccessFile 类,允许在文件中的任何位置读取和写入数据。到目前为止,您使用过的所有流都称为只读只写流。这些流称为顺序流。使用顺序流打开的文件称为顺序访问文件。顺序存取文件的内容无法更新。然而,经常需要修改文件。 Java 提供了 RandomAccessFile 类来允许在文件中的任何位置读取和写入数据。使用 RandomAccessFile 类打开的文件称为 随机访问文件

RandomAccessFile 类实现了 DataInputDataOutput 接口,如下图所示。 DataInput 接口定义了读取原始类型值和字符串的方法(例如,readIntreadDoublereadChar readBooleanreadUTF)和 DataOutput 接口定义了写入原始类型值和字符串的方法(例如,writeIntwriteDoublewriteCharwriteBooleanwriteUTF)。

Image description

创建 RandomAccessFile 时,您可以指定以下两种模式之一:rrw。模式 r 表示流是只读的,模式 rw 表示流允许读取和写入。例如,以下语句创建一个新流 raf,该流允许程序读取和写入文件 test.dat:

RandomAccessFile raf = new RandomAccessFile("test.dat", "rw");

如果test.dat已经存在,则创建raf来访问它;如果 test.dat 不存在,则会创建一个名为 test.dat 的新文件,并创建 raf 来访问新文件。方法 raf.length() 返回任意给定时间 test.dat 中的字节数。如果将新数据追加到文件中,raf.length() 会增加。

如果不想修改文件,请使用 r 模式打开它。这可以防止无意中修改文件。

随机访问文件由字节序列组成。称为文件指针的特殊标记位于这些字节之一。读或写操作发生在文件指针的位置。打开文件时,文件指针设置在文件的开头。当您向文件读取或写入数据时,文件指针向前移动到下一个数据项。例如,如果您使用 readInt() 读取 int 值,则 JVM 从文件指针读取 4 字节,现在文件指针为 4 个字节,如下图所示。

Image description

对于

RandomAccessFile raf,可以使用 raf.seek(position) 方法将文件指针移动到指定位置。 raf.seek(0) 将其移动到文件的开头,raf.seek(raf.length()) 将其移动到文件的末尾。下面的代码演示了 RandomAccessFile.

package demo;
import java.io.*;

public class TestRandomAccessFile {

    public static void main(String[] args) throws IOException {
        try( // Create a random access file
            RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");
        ) {
            // Clear the file to destroy the old contents if exists
            inout.setLength(0);

            // Write new integers to the file
            for(int i = 0; i < 200; i++)
                inout.writeInt(i);

            // Display the current length of the file
            System.out.println("Current file length is " + inout.length());

            // Retrieve the first number
            inout.seek(0); // Move the file pointer to the beginning
            System.out.println("The first number is " + inout.readInt());

            // Retrieve the second number
            inout.seek(1 * 4); // Move the file pointer to the second number
            System.out.println("The second number is " + inout.readInt());

            // Retrieve the tenth number
            inout.seek(9 * 4); // Move the file pointer to the tenth number
            System.out.println("The tenth number is " + inout.readInt());

            // Modify the eleventh number
            inout.writeInt(555);

            // Append a new number
            inout.seek(inout.length()); // Move the file pointer to the end
            inout.writeInt(999);

            // Display the new length
            System.out.println("The new length is " + inout.length());

            // Retrieve the new eleventh number
            inout.seek(10 * 4); // Move the file pointer to the eleventh number
            System.out.println("The eleventh number is " + inout.readInt());
        }
    }

}

登录后复制
当前文件长度为 800

第一个数字是 0
第二个数字是 1
第十个数字是 9
新的长度是804
第11个数字是555

为名为

inout.dat 的文件创建一个 RandomAccessFile,模式为 rw,以允许第 8 行中的读取和写入操作。

inout.setLength(0) 将第 11 行中的长度设置为 0。这实际上会破坏文件的旧内容。

for 循环将 200 个 int 值从 0199 写入文件第 14 和 15 行。 🎜>int 值需要 4 个字节,从 inout.length() 返回的文件总长度现在是 800 (第 18 行),如示例输出所示。

第 21 行调用 inout.seek(0) 将文件指针设置为文件的开头。 inout.readInt() 读取第 22 行中的第一个值并将文件指针移动到下一个数字。第二个数字在第 26 行读取。

inout.seek(9 * 4)(第 29 行)将文件指针移动到第十个数字。 inout.readInt() 读取第十个数字,并将文件指针移动到第 30 行的第十一个数字。 inout.write(555) 在当前位置写入新的第十一个数字 (第 33 行)。之前的第十一个数字被销毁。

inout.seek(inout.length()) 将文件指针移动到文件末尾(第 36 行)。 inout.writeInt(999)999 写入文件(第 37 行)。现在文件的长度增加了 4,因此 inout.length() 返回 804(第 40 行)。

inout.seek(10 * 4) 将文件指针移动到第 43 行中的第十一个数字。新的第十一个数字 555 显示在第 44 行中。

以上是随机访问文件的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Java的类负载机制如何起作用,包括不同的类载荷及其委托模型? Java的类负载机制如何起作用,包括不同的类载荷及其委托模型? Mar 17, 2025 pm 05:35 PM

Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?

2025年的前4个JavaScript框架:React,Angular,Vue,Svelte 2025年的前4个JavaScript框架:React,Angular,Vue,Svelte Mar 07, 2025 pm 06:09 PM

2025年的前4个JavaScript框架:React,Angular,Vue,Svelte

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射? 如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射? Mar 17, 2025 pm 05:43 PM

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?

如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案? 如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案? Mar 17, 2025 pm 05:46 PM

如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?

Node.js 20:关键性能提升和新功能 Node.js 20:关键性能提升和新功能 Mar 07, 2025 pm 06:12 PM

Node.js 20:关键性能提升和新功能

冰山:数据湖桌的未来 冰山:数据湖桌的未来 Mar 07, 2025 pm 06:31 PM

冰山:数据湖桌的未来

Spring Boot Snakeyaml 2.0 CVE-2022-1471问题已修复 Spring Boot Snakeyaml 2.0 CVE-2022-1471问题已修复 Mar 07, 2025 pm 05:52 PM

Spring Boot Snakeyaml 2.0 CVE-2022-1471问题已修复

如何在Java中实施功能编程技术? 如何在Java中实施功能编程技术? Mar 11, 2025 pm 05:51 PM

如何在Java中实施功能编程技术?

See all articles