您的位置:首页 > 编程语言 > Java开发

Java使用RandomAccessFile读写文本文件

2017-09-24 10:26 393 查看
指定位置写入

RandomAccessFile file = new RandomAccessFile("c:\\k.txt", "rw");

 file.seek(2*2);//跳过俩个字节

file.write("人".getBytes());//防止乱码

指定位置读取

 RandomAccessFile raf = new RandomAccessFile("c:\\k.txt", "r"); 

 raf.seek(2);//设置指针的位置为文件的开始部分  

 byte[] bytes = new byte[12];  

 for (int i=0; i<bytes.length; i++)  

 bytes[i] = raf.readByte();//每次读一个字节,并把它赋值给字节bytes[i]  

 String stringValue = new String(bytes);           

 System.out.println(stringValue); 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: