Java RandomAccessFile的使用(转载的文章,出处http://www.2cto.com/kf/201208/149816.html)
来源:互联网 发布:成都经济发展数据库 编辑:程序博客网 时间:2024/05/03 13:59
RandomAccessFile的对象包含一个记录指针,用于标识当前流的读写位置,这个位置可以向前移动,也可以向后移动。RandomAccessFile包含两个方法来操作文件记录指针。
long getFilePoint():记录文件指针的当前位置。
void seek(long pos):将文件记录指针定位到pos位置。
RandomAccessFile包含InputStream的三个read方法,也包含OutputStream的三个write方法。同时RandomAccessFile还包含一系列的readXxx和writeXxx方法完成输入输出。
mode的值有四个
"r":以只读文方式打开指定文件。如果你写的话会有IOException。
"rw":以读写方式打开指定文件,不存在就创建新文件。
"rws":不介绍了。
"rwd":也不介绍。
下面是从中间开始读取文件
[java]
import java.io.RandomAccessFile;
public class work {
public static void main(String[] args) throws Exception{
RandomAccessFile raf=new RandomAccessFile("work","r");
raf.seek(4);
byte []buffer=new byte[100];
int len=0;
while((len=raf.read(buffer, 0, 100))!=-1)
{
System.out.println(new String(buffer,0,len));
}
}
}
import java.io.RandomAccessFile;
public class work {
public static void main(String[] args) throws Exception{
RandomAccessFile raf=new RandomAccessFile("work","r");
raf.seek(4);
byte []buffer=new byte[100];
int len=0;
while((len=raf.read(buffer, 0, 100))!=-1)
{
System.out.println(new String(buffer,0,len));
}
}
}
下面是在文件最后加入内容
[java]
import java.io.RandomAccessFile;
public class work {
public static void main(String[] args) throws Exception{
RandomAccessFile raf=new RandomAccessFile("work","rw");
raf.seek(raf.length());
raf.write("hello world!".getBytes());
raf.close();
}
}
import java.io.RandomAccessFile;
public class work {
public static void main(String[] args) throws Exception{
RandomAccessFile raf=new RandomAccessFile("work","rw");
raf.seek(raf.length());
raf.write("hello world!".getBytes());
raf.close();
}
}
至于向文件中间添加内容的话,也是很好解决的。先将指针移到指定位置,把后面的保存到临时文件,再将指针移到指定位置,添加内容,然后再将临时文件的类容加到后面就可以了。
- Java RandomAccessFile的使用(转载的文章,出处http://www.2cto.com/kf/201208/149816.html)
- Java生成二维码,转载!感觉不错Mark下(出处:http://www.2cto.com/kf/201108/98471.html)
- 实现多线程的两种方法(转自:http://www.2cto.com/kf/201507/423032.html)
- http://www.2cto.com/kf/201112/115476.html
- http://www.2cto.com/kf/201205/131945.html
- http://www.2cto.com/kf/201402/280576.html
- http://www.2cto.com/kf/201410/344061.html
- http://www.2cto.com/kf/201311/255432.html
- 使用HTTP协议访问网络 原网址https://www.2cto.com/kf/201701/552932.html
- argparse解析器 转自http://www.2cto.com/kf/201208/149418.html
- urllib2库 http://www.2cto.com/kf/201208/145070.html
- 转载一篇搭建svn服务器的文章。出处为http://www.ha97.com/4467.html
- android ContentResolver详解 http://www.2cto.com/kf/201207/144022.html
- ASP.NET MVC HtmlHelper用法大全(转:http://www.2cto.com/kf/201105/90985.html)
- iframe跨域通信方法(转自http://www.2cto.com/kf/201211/166555.html)
- Android开发之MdiaPlayer详解 http://www.2cto.com/kf/201408/325309.html
- inux的安装和启动流程(http://www.2cto.com/os/201208/145165.html)
- googleEarth抄下来的经纬度转换成距离的代码 文章出处:http://www.diybl.com/course/3_program/java/javashl/2008611/124563.html
- LeetCode之Rotate Image
- 绑定本地Service并与之通信
- linux命令行输出彩色打印
- Geotools之“Hello World”——打开本地shp文件并显示
- 怎样用AIDL Service 传递复杂数据
- Java RandomAccessFile的使用(转载的文章,出处http://www.2cto.com/kf/201208/149816.html)
- 平时遇见的错误集合
- Android版多线程下载器核心代码分享
- 显示在标题上的进度条
- Log工具管理
- 文章标题
- android 检测sqlite数据表中字段(列)是否存在
- Qt在Windows下的三种开发环境的搭建
- material与sharedMaterial的区别