(随机流)RandomAccessFile
2017-04-04 21:13
246 查看
/**
* 随机存储文件 RandomAccessFile
* 既可以当输入流 也可以当输出流
* 支持从文件的开头读取、写入
* 支持从文件的任意位置读取、写入
*
* 此方法实现了从文件的任意位置插入内容
*/
public static void suiJiInsert(){
//提供随机访问的流
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello.txt"), "rw");
//调取光标到指定插入的位置,开始读取指针后面的内容
raf.seek(3);
//读取指定位置后面的内容
byte[] b = new byte[10];
int len;
StringBuffer sb = new StringBuffer();
while((len = raf.read(b)) != -1){
//把指定光标后面的内容暂时存储在sb中
sb.append(new String(b, 0, len));
}
//在把指针调回原来的位置开始进行写入操作
raf.seek(3);
//插入xy两个字符
raf.write("xy".getBytes());
//在xy后面添加原有的内容
raf.write(sb.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(raf != null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 用随机流实现复制功能的操作
*/
public static void shuiJi(){
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
//r表示只读
raf1 = new RandomAccessFile(new File("hello.txt"), "r");
//rw表示可读取、可写入
raf2 = new RandomAccessFile(new File("hello1.txt"), "rw");
byte[] b = new byte[1024];
int len;
while((len = raf1.read(b)) != -1){
raf2.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(raf2 != null){
try {
raf2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(raf1 != null){
try {
raf1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
* 随机存储文件 RandomAccessFile
* 既可以当输入流 也可以当输出流
* 支持从文件的开头读取、写入
* 支持从文件的任意位置读取、写入
*
* 此方法实现了从文件的任意位置插入内容
*/
public static void suiJiInsert(){
//提供随机访问的流
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello.txt"), "rw");
//调取光标到指定插入的位置,开始读取指针后面的内容
raf.seek(3);
//读取指定位置后面的内容
byte[] b = new byte[10];
int len;
StringBuffer sb = new StringBuffer();
while((len = raf.read(b)) != -1){
//把指定光标后面的内容暂时存储在sb中
sb.append(new String(b, 0, len));
}
//在把指针调回原来的位置开始进行写入操作
raf.seek(3);
//插入xy两个字符
raf.write("xy".getBytes());
//在xy后面添加原有的内容
raf.write(sb.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(raf != null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 用随机流实现复制功能的操作
*/
public static void shuiJi(){
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
//r表示只读
raf1 = new RandomAccessFile(new File("hello.txt"), "r");
//rw表示可读取、可写入
raf2 = new RandomAccessFile(new File("hello1.txt"), "rw");
byte[] b = new byte[1024];
int len;
while((len = raf1.read(b)) != -1){
raf2.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(raf2 != null){
try {
raf2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(raf1 != null){
try {
raf1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- 随机读写文件RandomAccessFile
- Java学习笔记---实现文件随机读写-RandomAccessFile
- java 管道流PipedInputStream,PipedInputStream和随机访问文件 RandomAccessFile
- RandomAccessFile随机访问文件的读取和写入
- 随机访问文件的读取和写入对象:RandomAccessFileDemo
- IO操作-随机读取文件RandomAccessFile
- java io 笔记一:RandomAccessFile(随机访问) 类
- Java 实现文件随机读写-RandomAccessFile
- 随机访问类(RandomAccessFile)
- IO流--RandomAccessFile--随机访问文件
- RandomAccessFile文件随机读写
- Java文件操作---RandomAccessFile随机读写文件操作
- [Java核心API]IO流--随机访问文件RandomAccessFile
- 7-RandomAccessFile 随机流
- 随机访问文件 自身具备读写 的RandomAccessFile
- java中RandomAccessFile随机文件读写,文件追加和部分读取
- 随机访问类RandomAccessFile-多线程下载
- 2011-09-01--RandomAccessFile 随机读取文件
- JavaIO之RandomAccessFile随机访问文件
- (原创)RandomAccessFile随机读取流