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. 【繁琐工作自动化】pandas 处理 excel 文件

    0. 一般处理 读取 excel 格式文件:df = pd.read_excel('xx.xlsx'),下面是一些简单查看文件内容的函数: df.head():展示前五行: df.columns:展示 ...

  2. JavaScript对象模型-执行模型

    数据类型基本数据类型基本数据类型是JS语言最底层的实现.简单数值类型: 有Undefined, Null, Boolean, Number和String.注意,描述中的英文单词在这里仅指数据类型的名称 ...

  3. C++中continue

    /* C++中continue使用 Author:盗了一个你 */ #include<iostream> using namespace std; int main() { int val ...

  4. SDK介绍

    软件开发工具包(外语首字母缩写:SDK.外语全称:Software Development Kit)一般都是一些软件工程师为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件时的开发工具的集合. ...

  5. Thinkpad x230i安装Ubuntu10.04发生no network devices available

    这个是由于10.04版本没有集成x230i的网卡驱动导致,需要到http://sourceforge.net/projects/e1000/f ... %20stable/下载最新版本驱动,并安装,之 ...

  6. zoj 2532 Internship【最小割】

    就是求哪些边在最大流上满流,也就是找割边.把0作为t点,s向所有的1~n连流量为inf的边,其他的边按照流量连.跑一遍最大流,从s顺着有残余流量的正向边dfs打标记fr,从t顺着正向边有残余流量的反向 ...

  7. P5068 [Ynoi2015]我回来了

    传送门 解锁成就:ynoi的题目都做到过原题 因为\(n\)很小,我们可以用\(sss[u][i]\)表示到点\(u\)的距离不超过\(i\)的点的集合,这个可以用bitset存,然后先一遍bfs,再 ...

  8. 使用gitee(码云)创建项目

    注册登录 https://gitee.com/ 也可以直接用oschina的帐号. 创建项目 点击"+"号,创建项目. 执行git命令 本机创建一个你的这个项目目录,init后不要 ...

  9. 标准块CP功能实现

    #include<stdio.h> int main(int argc,char *argv[]) { FILE *src_fp,*des_fp; int src_ret; ]={}; ) ...

  10. Dexposed:android免Root无侵入Aop框架

    在网上看到了阿里推出的一个android开源项目,名为Dexposed, 是一个Android平台下的无侵入运行期AOP框架.旨在解决像性能监控.在线热补丁等移动开发常见难题,典型使用场景为: AOP ...