Java IO流->处理流->“随机访问” 的方式:RandomAccessFile
来源:互联网 发布:网络很丑的女人的图片 编辑:程序博客网 时间:2024/05/21 11:10
图一:
图二:
示例代码:
import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;import org.junit.Test;public class TestRandomAccessFile {//进行文件的读写@Testpublic void test1() {RandomAccessFile raf1 = null;RandomAccessFile raf2 = null;try {raf1 = new RandomAccessFile(new File("hello.txt"), "r");raf2 = new RandomAccessFile("hello1.txt", "rw");byte[] b = new byte[20];int len;while((len = raf1.read(b)) != -1) {raf2.write(b, 0, len);}} catch (FileNotFoundException e) {e.printStackTrace();} 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();}}}}//实际上实现的是覆盖操作@Testpublic void test2() {RandomAccessFile raf = null;try {raf = new RandomAccessFile("hello.txt", "rw");//先定位到要插入的地方raf.seek(4);raf.write("xy".getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(raf != null) {try {raf.close();} catch (IOException e) {e.printStackTrace();}}}}//插入操作:通过进行复制操作,再进行覆盖操作,test4()更好的实现了插入操作@Testpublic void test3() {RandomAccessFile raf = null;try {raf = new RandomAccessFile("hello.txt", "rw");raf.seek(4);String str = raf.readLine();raf.seek(4);raf.write("xy".getBytes());raf.write(str.getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(raf != null) {try {raf.close();} catch (IOException e) {e.printStackTrace();}}}}//插入操作:相较于test3(),更通用@Testpublic void test4() {RandomAccessFile raf = null;try {raf = new RandomAccessFile("hello.txt", "rw");raf.seek(4);byte[] b = new byte[20];int len;StringBuffer sb = new StringBuffer();while((len = raf.read(b)) != -1) {sb.append(new String(b, 0, len));}raf.seek(4);raf.write("xy".getBytes());raf.write(sb.toString().getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(raf != null) {try {raf.close();} catch (IOException e) {e.printStackTrace();}}}}}
0 0
- Java IO流->处理流->“随机访问” 的方式:RandomAccessFile
- IO流--随机访问(RandomAccessFile)
- [Java核心API]IO流--随机访问文件RandomAccessFile
- java: 随机访问流RandomAccessFile
- IO流(RandomAccessFile随机访问流)
- IO流--RandomAccessFile--随机访问文件
- JAVA IO流RandomAccessFile随机读取
- 随机访问流RandomAccessFile
- 随机访问流RandomAccessFile的使用
- java 随机访问 RandomAccessFile
- Java IO流之随机读写流RandomAccessFile
- java IO流:RandomAccessFile
- java io 笔记一:RandomAccessFile(随机访问) 类
- IO之随机读写流RandomAccessFile
- java RandomAccessFile随机读写流
- 【Java IO流】RandomAccessFile类的使用
- Java IO流(三)-- 文件的处理与随机访问
- Java RandomAccessFile随机访问类
- centos6.5在VMware和Oracle VM VirtualBox下安装的区别
- JS数组
- Tomcat集群Cluster实现原理剖析
- HDU 2031 进制转换(进制转换)
- android 遇到TextView 没到末尾 自动换行 可能是 全角半角混合
- Java IO流->处理流->“随机访问” 的方式:RandomAccessFile
- Java单例你所不知道的事,与Volatile关键字有染
- crazyflie2_nrf51822程序分析--MBS
- B、淘汰汽车-------(计蒜之道 测试赛)
- android播放音频文件(MediaPlayer)和录音(MediaRecorder)--录音
- C#中的delegate与event简单讲解
- HDU 2032 杨辉三角(dp)
- ServletContext
- HDU 2033 人见人爱A+B(水~)