Java IO(二)--RandomAccessFile基本使用
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基本使用的更多相关文章
- 图解 Java IO : 二、FilenameFilter源码
Writer :BYSocket(泥沙砖瓦浆木匠) 微 博:BYSocket 豆 瓣:BYSocket FaceBook:BYSocket Twitter ...
- Java IO的RandomAccessFile的使用(转)
现有如下的一个需求,向已存在1G数据的txt文本里末尾追加一行文字,内容如下“Lucene是一款非常优秀的全文检索库”.可能大多数朋友会觉得这个需求很easy,说实话,确实easy,然后XXX君开始实 ...
- Java—IO流 RandomAccessFile类
RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件. 支持随机访问文件,可以访问文件的任意位置. java文件模型,在硬盘上的文件是byte byte byt ...
- Java IO 之 RandomAccessFile 操作文件内容
RandomAccessFile类实现对文件内容的随机读写 文件内容的随机操作,重难点在于字符操作,具体查看API package org.zln.io.file; import java.io.IO ...
- Java IO(二)
字节流 字符流: FileReader FileWriter BufferedReader BufferedWriter 字节流: FileInputStream FileOutputStream B ...
- java IO(二):字节流
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- Java IO(二) 之 InputStream
源代码均以JDK1.8作为參考 前言: InputStream实现了两个接口Closeable和AutoCloseable: Closeable:JDK1.5中引入,Closeable接口中仅仅有一个 ...
- Java IO学习--RandomAccessFile
1.什么是 随机访问文件流 RandomAccessFile 这个类在很多资料上翻译成中文都是:随机访问文件,在中文里,随机是具有不确定的含义,指一会访问这里,一会访问那里的意思.如果以这种语义来解释 ...
- 系统学习 Java IO (二)----IO 异常处理
目录:系统学习 Java IO---- 目录,概览 我们使用流后,需要正确关闭 Streams 和 Readers / Writers . 这是通过调用 close() 方法完成的,看看下面这段代码: ...
随机推荐
- lucene DocValues——没有看懂
前言: 在Lucene4.x之后,出现一个重大的特性,就是索引支持DocValues,这对于广大的solr和elasticsearch用户,无疑来说是一个福音,这玩意的出现通过牺牲一定的磁盘空间带来的 ...
- bzoj 5457 城市
题目大意: 树上每个点有种类$a_i$和数量$b_i$,求每个点的子树内数量最多的种类的数量和这个数量 思路: 显然是线段树合并裸题 学习一下$dsu \space on \space tree$ 主 ...
- 说说Java观察者模式
观察者模式是对象行为模式中的一种,在平时项目中经常被用来解耦对象之间的依赖. 观察者模式定义了对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都得到通知并自动更新.别名:De ...
- hdu4417(离线操作 + 树状数组)
题意: 给定一个长度为n的数组,有m次的查询,每次查询[a,b]区间里比H小的数有多少个? 由于n和m的取值范围为0到10的5次方,所以直接回答会超时,所以考虑先读入所有的查询操作,然后依次回答比H小 ...
- CodeForces 723D Lakes in Berland (dfs搜索)
题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来, ...
- CodeForces E. Goods transportation【最大流+dp最小割】
妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...
- P2093 [国家集训队]JZPFAR(KDTree)
传送门 类似于p4357 不过因为距离相等的时候要优先选择序号小的,所以要重载一下运算符 //minamoto #include<bits/stdc++.h> #define R regi ...
- JAVA值传递和引用传递 以及 实参是否改变
八大数据类型和String 在进行传递的时候 不会改变. 八大数据类型 public class parameterValue { //值传递 public static void main(Str ...
- Android偏好设置(7)自定义Preference,和PreferenceDialog
Building a Custom Preference The Android framework includes a variety of Preference subclasses that ...
- 1272 最大距离 只想到了dp
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 离散化后,用dp[i]表示向右,大于等于i这个数字的最大位置 dp ...