关于RandomAccess
package special;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 随机访问流:
*
* 此类不属于任何一个输入流和输出流
* 直接继承自Object实现的类
* 可以对文件随机的进行读和写!
* @author mzy
*
*
* JDK:
* 此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。
* 存在指向该隐含数组的光标或索引,称为文件指针;
* 输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。
* 如果随机访问文件以读取/写入模式创建,则输出操作也可用;
* 输出操作从文件指针开始写入字节,并随着对字节的写入而前移此文件指针。
* 写入隐含数组的当前末尾之后的输出操作导致该数组扩展。
* 该文件指针可以通过 getFilePointer 方法读取,并通过 seek 方法设置。
*
* 通常,如果此类中的所有读取例程在读取所需数量的字节之前已到达文件末尾,则抛出 EOFException(是一种 IOException)。
* 如果由于某些原因无法读取任何字节,而不是在读取所需数量的字节之前已到达文件末尾,则抛出 IOException,而不是 EOFException。
* 需要特别指出的是,如果流已被关闭,则可能抛出 IOException。
*/
public class RandomAccessFileDemo {
// 第二个参数为mode:一共四种模式:
// 模式常用的 "r" 和 "rw"
// r read
// rw read and write
/* RandomAccessFile(File file, String mode) */
/* RandomAccessFile(String name, String mode) */
public static void main(String[] args) throws IOException {
write();
read();
}
private static void read() throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
byte a = raf.readByte();
System.out.println("byte:" + a);
System.out.println("当前指针位置为:" + raf.getFilePointer());
int b = raf.readInt();
System.out.println("int:" + b);
System.out.println("当前指针位置为:" + raf.getFilePointer());
char c = raf.readChar();
System.out.println("char:" + c);
System.out.println("当前指针位置为:" + raf.getFilePointer());
boolean d = raf.readBoolean();
System.out.println("boolean:" + d);
System.out.println("当前指针位置为:" + raf.getFilePointer());
float e = raf.readFloat();
System.out.println("float:" + e);
System.out.println("当前指针位置为:" + raf.getFilePointer());
double f = raf.readDouble();
System.out.println("double:" + f);
System.out.println("当前指针位置为:" + raf.getFilePointer());
long g = raf.readLong();
System.out.println("long:" + g);
System.out.println("当前指针位置为:" + raf.getFilePointer());
String h = raf.readUTF();
// 2 + 3 + 3
System.out.println("UTF:" + h);
System.out.println("当前的指针位置为:" + raf.getFilePointer());
raf.seek(5);
char ch = raf.readChar();
System.out.println("定位指针为5: ch = " + ch);
}
private static void write() throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
raf.writeByte(100);
raf.writeInt(1000);
raf.writeChar('b');
raf.writeBoolean(true);
raf.writeFloat(12.34F);
raf.writeDouble(12.48);
raf.writeLong(123456789);
raf.writeUTF("中国");
// 不提供flush方法
raf.close();
}
}
关于RandomAccess的更多相关文章
- 256 terabytes random-access memory
Computer Systems A Programmer's Perspective Second Edition As we will discuss, the extension of IA32 ...
- RandomAccess接口的使用
RandomAccess在类Collections的shuffle()方法中的使用:(jdk源码如下) /** * Randomly permute the specified list using ...
- Jdk 接口类RandomAccess了解
1. 接口说明 Marker interface used by List implementations to indicate that they support fast (generally ...
- Java集合类:"随机访问" 的RandomAccess接口
引出RandomAccess接口 如果我们用Java做开发的话,最常用的容器之一就是List集合了,而List集合中用的较多的就是ArrayList 和 LinkedList 两个类,这两者也常被用来 ...
- Java的四个标记接口:Serializable、Cloneable、RandomAccess和Remote接口
一.概述 标记接口是一些没有属性和方法的接口,也是一种设计思想.Java中的一个标记接口表示的的是一种类的特性,实现了该标记接口的类则具有该特性.如实现了Serializable接口的类,表示这个类的 ...
- RandomAccess
在List集合中,我们经常会用到ArrayList以及LinkedList集合,但是通过查看源码,就会发现ArrayList实现RandomAccess接口,但是RandomAccess接口里面是空的 ...
- 分析轮子(七)- RandomAccess.java
1:还是先上一个类的继承关系比较图吧! 2:看一下 RandomAccess.java 的源码,空空如也,什么都没有,那她有什么用处呢? /** * Marker interface used by ...
- Java容器解析系列(3) List AbstractList ListIterator RandomAccess fail-fast机制 详解
做为数据结构学习的常规,肯定是先学习线性表,也就是Java中的List,开始 Java中List相关的类关系图如下: 此篇作为对Java中相关类的开篇.从上图中可以看出,List和AbstractLi ...
- Java Collections Framework 之 RandomAccess接口
在研究Collections类的排序算法时候,看到这样的代码 : public static <T> int binarySearch(List<? extends Comparab ...
- 关于接口 RandomAccess
今天看到java.util.Collections这个工具类中的 public static <T> void fill(List<? super T> list, T obj ...
随机推荐
- Beam Search快速理解及代码解析(下)
Beam Search的问题 先解释一下什么要对Beam Search进行改进.因为Beam Search虽然比贪心强了不少,但还是会生成出空洞.重复.前后矛盾的文本.如果你有文本生成经验,一定对这些 ...
- 微信小程序云开发-数据库-查询满足条件的数据
一.查询价格大于10的商品 1.wxml文件 2.js文件 where条件语句:.where({price:db.command.gt(10)}) 3.查询结果 二.查询价格大于等于10的商品 js文 ...
- 安装Go语言支持及Gogs版本管理工具
安装Go语言支持及Gogs版本管理工具 1. GO 语言: 1.1 介绍 1.1.1 官方介绍: The Go programming language is an open source proje ...
- debian 9安装细节
1.安装KDE桌面 2.开机桌面正常启动,首先在grub启动界面,按"e"键,在linux......quiet后面加上nomodeset,然后进入桌面,在终端输入: su -vi ...
- ML - 常用数学符号
关系运算符: ±:\pm ×:\times ÷:\div ∣:\mid ∤:\nmid ⋅:\cdot ∘:\circ ∗:\ast ⨀:\bigodot ⨂:\bigotimes ⨁:\bigopl ...
- Linux bash命令行常用快捷键(Xshell和secure CRT以及gnome-terminal)
常用的命令行击键操作 ctrl + insert xshell中复制,可以设置选中内容自动复制ctrl shift + c crt中复制shift + insert xshell中粘贴ctrl s ...
- JDK 和 CGLib 实现动态代理和区别
JDK 和 CGLib 实现动态代理和区别 在日常的开发中,Spring AOP 是一个非常常用的功能.谈到 AOP,自然离不开动态代理. 那么,基于 JDK 和 CGLib 如何实现动态代理,他们之 ...
- 百度nlp api接口测试
date:2021/7/8 使用postman测试 网址:https://ai.baidu.com/ 在百度AI首页-开放能力-自然语言处理-语言处理基础技术 点击技术文档 在左侧文档目录选择API参 ...
- Drupal 远程代码执行漏洞(CVE-2019-6339)
影响版本 Drupal core 7.62之前的7.x版本.8.6.6之前的8.6.x版本和8.5.9之前的8.5.x版本 poc https://github.com/thezdi/PoC/blob ...
- Python 统计列表中重复元素的个数并返回其索引值
需求:统计列表list1中元素3的个数,并返回每个元素的索引 list1 = [3, 3, 8, 9, 2, 10, 6, 2, 8, 3, 4, 5, 5, 4, 1, 5, 9, 7, 10, 2 ...