RandomAccessFile:

  翻译过来就是任意修改文件,可以从文件的任意位置进行修改,迅雷的下载就是通过多个线程同时读取下载文件。例如,把一个文件分为四

部分,四个线程同时下载,最后进行内容拼接

public class RandomAccessFile implements DataOutput, DataInput, Closeable {
public RandomAccessFile(String name, String mode);
public RandomAccessFile(File file, String mode);
}

RandomAccessFile实现了DataOutput和DataInput接口,说明可以对文件进行读写

有两种构造方法,一般使用第二种方式

第二个参数mode,有四种模式

代码实例:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student { private int id;
private String name;
private int sex;
}
public static void main(String[] args) throws Exception{
String filePath = "D:" + File.separator + "a.txt";
RandomAccessFile accessFile = new RandomAccessFile(new File(filePath), "rw");
Student student = new Student(1004, "sam", 1);
accessFile.writeInt(student.getId());
accessFile.write(student.getName().getBytes());
accessFile.writeInt(student.getSex());
}

打开a.txt:

  发现内容为乱码,这是因为系统只识别ANSI格式的写入,其他格式都是乱码。当然如果你在软件、IDE书写txt文件,打开没有乱码,是因为

已经替我们转格式了。

writeUTF():

public static void main(String[] args) throws Exception{
String filePath = "D:" + File.separator + "a.txt";
RandomAccessFile accessFile = new RandomAccessFile(new File(filePath), "rw");
Student student = new Student(1004, "sam", 1);
// accessFile.writeInt(student.getId());
// accessFile.write(student.getName().getBytes());
// accessFile.writeInt(student.getSex());
accessFile.writeUTF(student.toString());
}

writeUTF()以与系统无关的方式写入,而且编码为utf-8,打开文件:

文件读取:

public static void main(String[] args) throws Exception{
String filePath = "D:" + File.separator + "a.txt";
RandomAccessFile accessFile = new RandomAccessFile(new File(filePath), "rw");
Student student = new Student();
String s = accessFile.readUTF();
System.out.println(s);
}

输出结果:

Student(id=1004, name=sam, sex=1)

这里需要注意,如果是先写文件,然后立刻读取,需要调用accessFile.seek(0);把指针指向首位,因为文件写入最终指针指向末尾了。=

追加内容到末尾:

public static void main(String[] args) throws Exception{
String filePath = "D:" + File.separator + "a.txt";
RandomAccessFile accessFile = new RandomAccessFile(new File(filePath), "rw");
accessFile.seek(accessFile.length());
accessFile.write("最佳内容".getBytes());
}

我们首先把指针移动到文件内容末尾

Java IO(二)--RandomAccessFile基本使用的更多相关文章

  1. 图解 Java IO : 二、FilenameFilter源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  2. Java IO的RandomAccessFile的使用(转)

    现有如下的一个需求,向已存在1G数据的txt文本里末尾追加一行文字,内容如下“Lucene是一款非常优秀的全文检索库”.可能大多数朋友会觉得这个需求很easy,说实话,确实easy,然后XXX君开始实 ...

  3. Java—IO流 RandomAccessFile类

    RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件. 支持随机访问文件,可以访问文件的任意位置. java文件模型,在硬盘上的文件是byte byte byt ...

  4. Java IO 之 RandomAccessFile 操作文件内容

    RandomAccessFile类实现对文件内容的随机读写 文件内容的随机操作,重难点在于字符操作,具体查看API package org.zln.io.file; import java.io.IO ...

  5. Java IO(二)

    字节流 字符流: FileReader FileWriter BufferedReader BufferedWriter 字节流: FileInputStream FileOutputStream B ...

  6. java IO(二):字节流

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  7. Java IO(二) 之 InputStream

    源代码均以JDK1.8作为參考 前言: InputStream实现了两个接口Closeable和AutoCloseable: Closeable:JDK1.5中引入,Closeable接口中仅仅有一个 ...

  8. Java IO学习--RandomAccessFile

    1.什么是 随机访问文件流 RandomAccessFile 这个类在很多资料上翻译成中文都是:随机访问文件,在中文里,随机是具有不确定的含义,指一会访问这里,一会访问那里的意思.如果以这种语义来解释 ...

  9. 系统学习 Java IO (二)----IO 异常处理

    目录:系统学习 Java IO---- 目录,概览 我们使用流后,需要正确关闭 Streams 和 Readers / Writers . 这是通过调用 close() 方法完成的,看看下面这段代码: ...

随机推荐

  1. 解耦与分离 —— 面向切面编程(AOP)

    家里的电表总结起来有两大特性: 电视机需要(电量管理),空调需要(电量管理),热水器也需要电量管理,即一组对象都需要某一功能特性: 电视机根据信号输出画面,空调吹出冷风,热水器将水加热,这些业务功能的 ...

  2. I.MX6 查找占用UART进程

    /**************************************************************************** * I.MX6 查找占用UART进程 * 说 ...

  3. yum和apt-get 安装方式

    rpm包和deb包是两种Linux系统下最常见的安装包格式,在安装一些软件或服务的时候免不了要和它们打交道.rpm包主要应用在RedHat系列包括 Fedora等发行版的Linux系统上,deb包主要 ...

  4. Are you sure you want to continue connecting etc ssh ssh_config StrictHostKeyChecking no

    Are you sure you want to continue connecting (yes/no) 每次ssh 进入一台新机器都会跳出如下的提示: The authenticity of ho ...

  5. git合并相关问题(copy)

    [说明:资料来自http://gitbook.liuhui998.com/3_3.html] 一个Git仓库可以维护很多开发分支.现在我们来创建一个新的叫”experimental”的分支: $ gi ...

  6. bzoj 2792: [Poi2012]Well【二分+贪心】

    #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const ...

  7. bzoj 4385: [POI2015]Wilcze doły【单调栈】

    对于每个i,以它为左端点的最优右端点一定是单增的,所以用单调栈维护 具体的,单调栈里放的是和单调的长为d的子段,然后枚举右端点,如果这段的和-当前长为d子段最大的和大于p的话,左端点右移同时注意单调栈 ...

  8. P3161 [CQOI2012]模拟工厂

    传送门 先枚举选择哪些订单,然后转为判定是否可行 在能完成的情况下肯定是花越多时间提高生产力越优 我们设可以有\(x\)单位时间来提高生产力,那么如果当前离下一个订单的时间为\(T\)时,这个订单要\ ...

  9. (11)用css设计电子相册 {上}

    本篇学习资料讲解:       通过css对电子相册进行排版 和 侧面强调“盒子模型.标准流.浮动和定位”的重要性. 先来看看"双向联动模式"的电子相册图: {鼠标指针经过某张图片 ...

  10. Akka源码分析-CircuitBreaker(熔断器)

    熔断器,在很多技术栈中都会出现的一种技术.它是在分布式系统中提供一个稳定的阻止嵌套失败的机制. 该怎么理解呢?简单来说,在分布式环境中,如果某个计算节点出现问题,很容易出现失败的逆向传到或整个系统的雪 ...