Java 日看一类(50)之IO包中的RandomAccessFile

Java IO(六、随机存取文件流RandomAccessFile) java IO 知识梳理 随机存取文件流 RandomAccessFile 阅读详情

该类完成了DataInput,DataOutput,Closeable接口

引入了如下包

import java.nio.channels.FileChannel;
import sun.nio.ch.FileChannelImpl;




该类的类头注释如下:

/**
 * Instances of this class support both reading and writing to a
 * random access file. A random access file behaves like a large
 * array of bytes stored in the file system. There is a kind of cursor,
 * or index into the implied array, called the <em>file pointer</em>;
 * input operations read bytes starting at the file pointer and advance
 * the file pointer past the bytes read. If the random access file is
 * created in read/write mode, then output operations are also available;
 * output operations write bytes starting at the file pointer and advance
 * the file pointer past the bytes written. Output operations that write
 * past the current end of the implied array cause the array to be
 * extended. The file pointer can be read by the
 * {@code getFilePointer} method and set by the {@code seek}
 * method.
 * <p>
 * It is generally true of all the reading routines in this class that
 * if end-of-file is reached before the desired number of bytes has been
 * read, an {@code EOFException} (which is a kind of
 * {@code IOException}) is thrown. If any byte cannot be read for
 * any reason other than end-of-file, an {@code IOException} other
 * than {@code EOFException} is thrown. In particular, an
 * {@code IOException} may be thrown if the stream has been closed.
 *
 * @author  unascribed
 * @since   JDK1.0
 */

大意如下:

该类实例支持读取和写入随机访问文件

随机访问文件的操作类似于储存在文件系统中的大型字节数组

存在一种光标或索引指向这个数组,该类光标或者索引叫做文件指针

输入操作从文件指针出开始读取数据并且会修改文件指针到刚刚读取过的位置

如果随机访问文件在读或者写模式下创建,输出操作均是有效的。

输出操作从文件指针出开始写出数据,并且会修改文件指针到刚刚写出的位置

写出到该数组当前末尾后的数据会导致该数组延长

文件指针可以通过getFilePointer获取,通过seek方法设置

一般情况下,常规的读取操作都是正确的

如果在读取够要求读取的数据量前读取到文件末尾,则会抛出EOFException

如果一些 byte因为文件结束以外的原因而无法读取的话,会抛出IOException

尤其是当流关闭后要求读取情况下会抛出IOException




该类含有如下的成员变量:

文件描述符

private FileDescriptor fd;

文件操作通道

private FileChannel channel = null;

读写权限

private boolean rw;

文件路径

private final String path;

关闭锁

private Object closeLock = new Object();

流关闭情况

private volatile boolean closed = false;

常量声明

private static final int O_RDONLY = 1;//只读
private static final int O_RDWR =   2;//读写
private static final int O_SYNC =   4;//读写且同步更新底层存储设备和元数据
private static final int O_DSYNC =  8;//读写且同步更新底层存储设备




该类含有如下的成员方法:

构造函数(给出路径和操作类型

public RandomAccessFile(String name, String mode)
    throws FileNotFoundException
{
    this(name != null ? new File(name) : null, mode);
}

构造函数(给出文件和操作类型

public RandomAccessFile(File file, String mode)
    throws FileNotFoundException
{
    String name = (file != null ? file.getPath() : null);
    int imode = -1;
    if (mode.equals("r"))
        imode = O_RDONLY;
    else if (mode.startsWith("rw")) {
        imode = O_RDWR;
        rw = true;
        if (mode.length() > 2) {
            if (mode.equals("rws"))
                imode |= O_SYNC;
            else if (mode.equals("rwd"))
                imode |= O_DSYNC;
            else
                imode = -1;//无有效操作类型
        }
    }
    if (imode < 0)
        throw new IllegalArgumentException("Illegal mode \"" + mode
                                           + "\" must be one of "
                                           + "\"r\", \"rw\", \"rws\","
                                           + " or \"rwd\"");//异常参数
    SecurityManager security = System.getSecurityManager();//获得安全检测器
    if (security != null) {
        security.checkRead(name);//检测文件是否可读取
        if (rw) {
            security.checkWrite(name);//检测文件是否可写入
        }
    }
    if (name == null) {
        throw new NullPointerException();
    }
    if (file.isInvalid()) {//文件无效
        throw new FileNotFoundException("Invalid file path");
    }
    fd = new FileDescriptor();
    fd.attach(this);//文件描述符绑定自身
    path = name;
    open(name, imode);//以特定模式打开文件
}

获得当前类的文件描述符

public final FileDescriptor getFD() throws IOException {
    if (fd != null) {
        return fd;
    }
    throw new IOException();
}

获得该文件的文件操作管道(如果多个操作打开同一文件,获得文件管道是相同的

public final FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, path, true, rw, this);
        }
        return channel;
    }
}

打开文件(native很尴尬

private native void open0(String name, int mode)
    throws FileNotFoundException;

打开文件(调用open0

private void open(String name, int mode)
    throws FileNotFoundException {
    open0(name, mode);
}

从文件中读取出一个byte

public int read() throws IOException {
    return read0();
}

从文件中读取出一个byte

private native int read0() throws IOException;

从文件中读出一个byte数组

private native int readBytes(byte b[], int off, int len) throws IOException;

从文件中读出byte数组

public int read(byte b[], int off, int len) throws IOException {
    return readBytes(b, off, len);
}

从文件中读取数据将数组填满

public int read(byte b[]) throws IOException {
    return readBytes(b, 0, b.length);
}

从文件中读取数据将数组填满

public final void readFully(byte b[]) throws IOException {
    readFully(b, 0, b.length);
}

从文件中读取数据将数组部分填充

public final void readFully(byte b[], int off, int len) throws IOException {
    int n = 0;
    do {
        int count = this.read(b, off + n, len - n);//count为实际读入的数据量
        if (count < 0)
            throw new EOFException();
        n += count;
    } while (n < len);
}

跳过n个数据进行读取

public int skipBytes(int n) throws IOException {
    long pos;//标志位,文件指针
    long len;//文件长度
    long newpos;//偏移后指针

    if (n <= 0) {
        return 0;
    }
    pos = getFilePointer();
    len = length();
    newpos = pos + n;
    if (newpos > len) {//如果偏移后指针超出文件长度,则设置为文件末尾
        newpos = len;
    }
    seek(newpos);

    /* return the actual number of bytes skipped */
    return (int) (newpos - pos);//返回实际跳过数据量
}

写出单个字节

public void write(int b) throws IOException {
    write0(b);
}

写出单个字节

private native void write0(int b) throws IOException;

写出字节数组

private native void writeBytes(byte b[], int off, int len) throws IOException;

写出字节数组

public void write(byte b[]) throws IOException {
    writeBytes(b, 0, b.length);
}

写出字节数组

public void write(byte b[], int off, int len) throws IOException {
    writeBytes(b, off, len);
}

获得文件内操作指针

public native long getFilePointer() throws IOException;

设置文件操作指针(总感觉应该在加一个超出文件长度的检测,可能native方法里有

public void seek(long pos) throws IOException {
    if (pos < 0) {
        throw new IOException("Negative seek offset");
    } else {
        seek0(pos);
    }
}

设置文件操作指针

private native void seek0(long pos) throws IOException;

返回文件长度

public native long length() throws IOException;

设置文件长度

public native void setLength(long newLength) throws IOException;

关闭文件流(并释放相应资源

public void close() throws IOException {
    synchronized (closeLock) {
        if (closed) {
            return;
        }
        closed = true;
    }
    if (channel != null) {
        channel.close();
    }

    fd.closeAll(new Closeable() {//这个写法很独特,创建了接口的实例并直接完成方法(虽然这个传入参数目前没看出来有啥用
        public void close() throws IOException {
           close0();
       }
    });
}

读取一个bool类型(读取int,返回bool

public final boolean readBoolean() throws IOException {
    int ch = this.read();
    if (ch < 0)
        throw new EOFException();
    return (ch != 0);
}

读取一个字节(读取int,返回byte

public final byte readByte() throws IOException {
    int ch = this.read();
    if (ch < 0)
        throw new EOFException();
    return (byte)(ch);
}

读取无符号字节型(-128~127变成0~255)

public final int readUnsignedByte() throws IOException {
    int ch = this.read();
    if (ch < 0)
        throw new EOFException();
    return ch;
}

读取短整形(2byte,需要移位

public final short readShort() throws IOException {
    int ch1 = this.read();
    int ch2 = this.read();
    if ((ch1 | ch2) < 0)
        throw new EOFException();
    return (short)((ch1 << 8) + (ch2 << 0));
}

读取无符号短整形

public final int readUnsignedShort() throws IOException {
    int ch1 = this.read();
    int ch2 = this.read();
    if ((ch1 | ch2) < 0)
        throw new EOFException();
    return (ch1 << 8) + (ch2 << 0);
}

读取字符型

public final char readChar() throws IOException {
    int ch1 = this.read();
    int ch2 = this.read();
    if ((ch1 | ch2) < 0)
        throw new EOFException();
    return (char)((ch1 << 8) + (ch2 << 0));
}

读取整形

public final int readInt() throws IOException {
    int ch1 = this.read();
    int ch2 = this.read();
    int ch3 = this.read();
    int ch4 = this.read();
    if ((ch1 | ch2 | ch3 | ch4) < 0)
        throw new EOFException();
    return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}

读取长整型

public final long readLong() throws IOException {
    return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
}

读取浮点型

public final float readFloat() throws IOException {
    return Float.intBitsToFloat(readInt());
}

读取双精度

public final double readDouble() throws IOException {
    return Double.longBitsToDouble(readLong());
}

读取一行字符串(读取到无数据、换行符或者新行符结束

public final String readLine() throws IOException {
    StringBuffer input = new StringBuffer();
    int c = -1;
    boolean eol = false;

    while (!eol) {
        switch (c = read()) {
        case -1:
        case '\n':
            eol = true;
            break;
        case '\r':
            eol = true;
            long cur = getFilePointer();
            if ((read()) != '\n') {
                seek(cur);
            }
            break;
        default:
            input.append((char)c);
            break;
        }
    }

    if ((c == -1) && (input.length() == 0)) {
        return null;
    }
    return input.toString();
}

读取出的字节进行UTF转码

public final String readUTF() throws IOException {
    return DataInputStream.readUTF(this);
}

写入bool

public final void writeBoolean(boolean v) throws IOException {
    write(v ? 1 : 0);
    //written++;
}

写入byte

public final void writeByte(int v) throws IOException {
    write(v);
    //written++;
}

写入短整型

public final void writeShort(int v) throws IOException {
    write((v >>> 8) & 0xFF);
    write((v >>> 0) & 0xFF);
    //written += 2;
}

写入字符型

public final void writeChar(int v) throws IOException {
    write((v >>> 8) & 0xFF);
    write((v >>> 0) & 0xFF);
    //written += 2;
}

写入整型

public final void writeInt(int v) throws IOException {
    write((v >>> 24) & 0xFF);
    write((v >>> 16) & 0xFF);
    write((v >>>  8) & 0xFF);
    write((v >>>  0) & 0xFF);
    //written += 4;
}

写入长整型

public final void writeLong(long v) throws IOException {
    write((int)(v >>> 56) & 0xFF);
    write((int)(v >>> 48) & 0xFF);
    write((int)(v >>> 40) & 0xFF);
    write((int)(v >>> 32) & 0xFF);
    write((int)(v >>> 24) & 0xFF);
    write((int)(v >>> 16) & 0xFF);
    write((int)(v >>>  8) & 0xFF);
    write((int)(v >>>  0) & 0xFF);
    //written += 8;
}

写入浮点型

public final void writeFloat(float v) throws IOException {
    writeInt(Float.floatToIntBits(v));
}

写入双精度

public final void writeDouble(double v) throws IOException {
    writeLong(Double.doubleToLongBits(v));
}
写入字符串(字节写入
public final void writeBytes(String s) throws IOException {
    int len = s.length();
    byte[] b = new byte[len];
    s.getBytes(0, len, b, 0);
    writeBytes(b, 0, len);
}

写入字符串(字符写入

public final void writeChars(String s) throws IOException {
    int clen = s.length();
    int blen = 2*clen;
    byte[] b = new byte[blen];
    char[] c = new char[clen];
    s.getChars(0, clen, c, 0);
    for (int i = 0, j = 0; i < clen; i++) {
        b[j++] = (byte)(c[i] >>> 8);
        b[j++] = (byte)(c[i] >>> 0);
    }
    writeBytes(b, 0, blen);
}

写入UTF格式的字符串

public final void writeUTF(String str) throws IOException {
    DataOutputStream.writeUTF(str, this);
}

private static native void initIDs();

private native void close0() throws IOException;

static {
    initIDs();
}




读取方式读取的元数据都是字节,读取出的不同数据类型都是对字节进行移位以及其他操作转换而成的,写入操作也是同理,写入的数据类型也有很多,但是最终都是转化成字节再写入到文件中的。该类的常量声明主要针对于FileChannel,在此不过多赘述。

Codex App 远程 SSH 连接 autodl 配置全攻略 SSH 远程连接是开发中常用的技术,通过加密通道实现本地与远程服务器的安全通信。其原理基于非对称加密和密钥对认证,确保数据传输安全。在 AI 开发场景中,远程连接能充分利用云端 GPU 算力,同时保留本地编辑器的流畅体验。autodl 作为主流算力云平台,支持通过 SSH 接入实例,但 Codex App 等工具需额外配置。本文详细讲解如何获取 autodl 的 SSH 接入信息、配置密钥登录、填写 Codex App 远程连接参数,并解决连接拒绝、权限报错等常见问题,帮助开发者高效搭建远程开发环境。 阅读详情

相关推荐

文华财经公式编程:麦语言简介及基础语法

文华财经公式语言(麦语言)是一种专门用于金融领域的编程语言,它是由文华财经公司开发的。提供了丰富的金融计算函数和数据处理功能,可以用于金融市场的数据分析、模型建立和交易策略的开发。例1:A:CLOSE;// 定义变量A为收盘价(盘中k线没有走完的时候A为最新价)。例2:MA5:=MA(C,5);// 定义收盘价的5周期均线(C为CLOSE简写)。例3:A:=REF(C,1);// 取得前一根k线的收盘价。

超哥量化的博客 1万+

Java中I/O包中的——“非流类RandomAccessFile类”——读、写文件数据的功能(只读、可读写等)

说明: 在IO包中,提供了一个RandomAccesseFile类,它不属于流类,但具有读写文件数据的功能,可以随机从文件的任何位置开始并以指定的操作权限(如只读、可读写等)执行读写数据的操作。 RandomAccesseFile类构造方法: RandomAccessFile(String name,String mode)中的参数mode用于指定访问文件的模式,也就是文件的操作权限。 参数mode取值: r:以只读的方式打开文件。如果执行写操作,会报IOException异常。 rw:以“读写”的方式打

hey,欢迎浏览我的博客,希望能给你提供帮助。 1370

【excel+vba】一表拆多表+不同收件人附带不同附件

快捷处理:拆分多个不同表 给不同人发送不同内容且附带附件

weixin_42779327的博客 2968

从Native层看java IO文件读写流程(以调用window API为例)

RandomAccessFile类最终调用的是Windows的四个API:OpenFile,ReadFile,WriteFile,GetFilePointer

lzf的博客 3265

人民网留言数据爬取

起因是要做一个LDA主题分析的实践案例,需要数据来源,人民网的留言数据特别的适合,想着能不能用人民网留言数据来做一个LDA,种种原因还是放弃了,但是第一次弯弯绕绕地通过爬虫拿到了数据,还是想记录一下。值得说明的是,这篇博客写于2025年11月9日,其中的加密逻辑可能会随着时间而变动。

Phoenix1st的博客 1032

YOLO11实战:手把手教你优化钢材表面缺陷识别模型(基于NEU-DET数据集)

本文详细介绍了如何使用YOLO11优化钢材表面缺陷识别模型,基于NEU-DET数据集进行实战训练。从数据增强、模型架构优化到训练过程控制,全面解析提升钢材缺陷检测性能的关键技术,特别针对裂纹、划痕等典型缺陷提出针对性解决方案。

weixin_29081163的博客 381

Java技术篇·IO包·RandomAccessFile类

有关知识及说明,全部在下边程序中体现。 package com.io; import java.io.File; import java.io.RandomAccessFile; /** * RandomAccessFile Java提供的对文件内容的访问,既可以读也可以写。 * 支持随机访问,可以访问任意位置。 * java文件模型:在硬盘上是byte byte byte存储的,是...

筱光的博客 573

如何加载交易策略EA到MT4/MT5

1、打开MT4/MT5软件 2、首先,必须有一个EA,而且必须是mq4/ex4为扩展名。点击左上角“文件”—“打开数据文件夹”,找到安装路径 3、将EA的ex4或mq4文件放到MT4目录\MQL4\Experts里面(本文以名为Demo的EA为例) 4、如果有DLL库文件,复制到experts目录下的libraries子目录中 5、如果有tpl模板文件,复制到templates目录中 6、添加完成后重启MT4,点击允许EA运行图标 7、在导航中找到刚才安装的EA,拖拽到...

weixin_41364817的博客 7362

花1K内存实现高效I/O的RandomAccessFile类

花1K内存实现高效I/O的RandomAccessFile类英文原文内容:在改进之前先做一个基本测试:逐字节COPY一个12兆的文件根据1.3得出的结论,现试着对RandomAccessFile类也加上缓冲读写机制优化BufferedRandomA

鲤鱼商城 3178

vue2/3 - H5 移动端网页中调用手机摄像头扫描二维码 / 条形码功能,在h5手机网页里调起相机摄像头进行二维码或条形码扫描效果详细教程(详细示例组件源码,一键复制开箱即用!)

vue扫描二维码条形码,vue网页调用摄像头扫二维码条形码,vue移动端如何实现调用相机扫描二维码,vue移动端扫一扫功能,vue怎么做移动端h5扫一扫功能,vue识别二维码和条形码,vue实现点击按钮调用摄像头扫码,vue调用本地摄像头实现拍照功能,Vue中如何进行二维码生成与扫描,如何实现vue在手机浏览器中调用摄像头实现扫描二维码功能,VUE项目移动端H5调用摄像头扫码,vue项目中实现H5调取摄像头,网页调用摄像头实现二维码扫描功能,vue调用手机摄像头,vue在调用摄像头扫码,vue自定义打开摄像

✅ 精选推荐文章 1万+

国产数据库全解析:openGauss、OceanBase、TiDB与KingbaseES的核心优势与应用场景指南

本文将深入剖析openGauss、OceanBase、TiDB和KingbaseES这四款主流国产数据库的核心内容,并针对国产化过程中给出使用建议,以帮助企业根据自身需求选择合适的数据库解决方案。国产数据库在性能、扩展性、兼容性、安全性等方面展现出强大实力,能够满足不同行业、不同场景的需求。企业在进行国产化过程中,应充分考虑业务特点、技术栈、团队能力等因素,结合上述分析,选择最适合自身的国产数据库产品。随着国产数据库技术的持续发展与生态完善,它们将成为推动企业数字化转型、保障数据安全的重要力量。

java专栏 8782

Java异常处理

目录RandomAccessFile类概述用RandomAccessFile类实现数据的读写操作非文本文件文本文件使用RandomAccessFile实现数据的插入 RandomAccessFile类概述 RandomAccessFile 声明在java.io包下,但直接继承于java.lang.Object类。并且它实现了DataInput、DataOutput这两个接口,也就意味着这个类既可以读也可以写。 RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意地方来读

十八岁讨厌编程的博客 1517

多物理场光学仿真软件:COMSOL_(10).激光器与谐振腔仿真

定义Nd:YAG晶体的材料属性% 折射率% 吸收系数% 发射截面% 定义光泵浦源% 泵浦光波长% 泵浦光强度% 泵浦光方向% 定义全反射镜% 选择边界% 完美电导体边界条件% 定义部分反射镜% 选择边界% 反射率边界条件% 反射率通过 COMSOL 对激光器和光学谐振腔进行仿真,可以全面评估激光器的性能,包括输出功率、效率、稳定性和模式选择等。这些仿真结果为激光器的设计和优化提供了重要的参考依据。

kkchenjj的博客 1649

Java IO——RandomAccessFile类详解

一、RandomAccessFile介绍 RandomAccessFile支持对文件的读取和写入随机访问(其他的输入流或者输出流只能进行一种操作,要么输入,要么输出)。RandomAccessFile把随机访问的文件对象看作存储在文件系统中的一个大型 byte 数组,然后通过指向该 byte 数组的光标或索引(即:文件指针 FilePointer)在该数组任意位置读取或写入任意数据。输入操作从文件指针开始读取字节(以字节为单位进行读取),并随着对字节的读取而前移此文件指针。如果RandomAccessFi

b15735105314的博客 2万+

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Java IO RandomAccessFile

Java IO RandomAccessFile详解~~~

WYSCODER的博客 4780

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Java中的IO流

Java中的IO流

YIYIYI 782

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Java IO(九):RandomAccessFile使用

Java中的RandomAccessFile可以让我们像操作数组一样操作文件,核心在于seek()方法可以移动文件指针。

欧阳方超的专栏 791

JAVA学习-IO.RandomAccessFile

RandomAccessFile类是Java中用于随机访问文件的独立文件读写类。它支持读写操作,可以在文件的任意位置进行读写,并且可以随机访问文件的任意位置。相比其他IO流,RandomAccessFile类在处理需要指定位置读写的场景下非常有用。(文章为作者在学习java过程中的一些个人体会总结和借鉴,如有不当、错误的地方,请各位大佬批评指正,定当努力改正,如有侵权请联系作者删帖。

weixin_69763181的博客 1081

Java中RandomAccessFile用法

文章目录 介绍 RandomAccessFile 使用 创建对象 写方法 读取方法 介绍 java.io.RandomAccessFile 1、读写文件的工具 2、将文件中的字节数据,当作数组,用下标访问指定位置的字节值 RandomAccessFile 既可以读取文件内容,也可以向文件输出数据。同时,RandomAccessFile 支持“随机访问”的方式,程序快可以直接跳转到文件的任意地方来读写数据。 由于 RandomAccessFile 可以自由访问文件的任意位置,所以如果需要访...

uuqaz的博客 9193

java IO笔记(RandomAccessFile)

本篇讲述的内容是java io中的RandomAccessFile。 RandomAccessFile是java io体系中提供给我们的一种文件访问类,它自己同时包含了读写功能,当然它最大的特点是可以自由访问文件中的位置,无需从头开始访问,下面先贴上RandomAccessFile的源码,通过源码对其进行学习。 package java.io; import java.nio.chann

moonfish0607的博客 482

java中io是什么_深入理解Java中的IO

深入理解Java中的IO转载自:http://blog.youkuaiyun.com/qq_25184739/article/details/51205186本文的目录视图如下:Java IO概要a.Java IO中常用的类b.Java流类的类结构图1.流的概念和作用2.Java IO所采用的模型 :3.IO流的分类4.Java IO流对象1.输入字节流InputStream2.输出字节流OutputStr...

weixin_34638282的博客 8684

Java IO ——RandomAccessFile

Java IO ——RandomAccessFile RandomAccessFile简介 RandomAccessFile 既可以读取文件也可以写入文件。 RandomAccessFile 最大的优势就是随机访问,能够直接跳转到任意位置进行文件的读写操作。 适用场景 如果需要访问文件的部分内容,而不是把文件从头读到尾,RandomAccessFile是最好的选择,因此RandomAccessFi...

梦的起点 468

Java IO流详解——RandomAccessFile

一、RandomAccessFile简介 RandomAccessFile既可以读取文件内容,也可以向文件输出数据。同时,RandomAccessFile支持“随机访问”的方式,程序快可以直接跳转到文件的任意地方来读写数据。 由于RandomAccessFile可以自由访问文件的任意位置,所以如果需要访问文件的部分内容,而不是把文件从头读到尾,使用RandomAccessFile将是更好的选择。 与OutputStream、Writer等输出流不同的是,RandomAccessFile允许自由定义文件记录指

weixin_44336181的博客 544

Java IO流之RandomAccessFile分析

简介 RandomAccessFile是一个随机访问文件即可读又可写的类,包含从文件读取数据的方法,又包含了向文件写入数据的方法.但不在IO流中字符流和字节流继承体系中.是完全独立的一个类.翻看源码可知RandomAccessFile继承了DataOutput和DataInput两个接口,而字节流体系中DataOutputStream和DataInputStream也分别继承了DataOutpu...

梦悠哉的博客 1309

RandomAccessFile readUTF() java.io.EOFException

RandomAccessFile 类实例 使用 readUTF() 方法 时,产生 java.io.EOFException 异常: readUTF() 只能读取由 writeUTF() 所写的文本 若定标至文件头读取,则必须保证所有文本均由 writeUTF() 所写 若遇到非由 writeUTF() 所写的内容,则会产生 java.io.EOFException 异常 代码: //注意!readUTF只能读取由writeUTF所写的文本! //若定标至文件头读取,则必须保证所有文本均由writeUT

weixin_45686201的博客 581

Java IO之RandomAccessFile

本篇介绍在io包中但不属于前面介绍的字节流和字符流体系的RandomAccessFile类,这里的不属于的意思是RandomAccessFile直接继承于Object,与InputStream和OutputStream没有继承关系。 RandomAccessFile有随机读取和写入文件的功能,可以跳到文件的任何位置读取或者写入,原因在于RandomAccessFile类持有一个文件位置指针,通过...

博客 282
上一篇: Java 日看一类(49)之IO包中的PushbackReader
下一篇: Java 日看一类(51)之IO包中的Reader类
wyf95278
博客等级 码龄10年 38粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值