在文件指定位置读取和写入内容RandomAccessFile
2015-02-03 16:41
706 查看
RandomAccessFile是属于随机读取类,是可以对文件本身的内容直接随机进行操作的,可以在文件的指定位置
的读取和写入内容,这在很多时候都是很方便的。
Java代码
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* RandomAccessFile是属于随机读取类,是可以对文件本身的内容直接随机进行操作的,就是说可以指定位置
* 的读取和写入内容
* @author andy
*
*/
public class RandomAccessFileTest {
public static void main(String args[]) throws IOException {
write();
read();
}
public static void write() throws IOException {
//以读写的方式来访问该文件
RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "rw");
raf.writeBytes("Hello World!");
raf.writeBoolean(true);
raf.writeInt(30);
raf.writeDouble(3.56);
raf.close();
}
public static void read() throws IOException {
RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "r");
raf.seek(12);//设置指针的位置
boolean booleanValue = raf.readBoolean();
int intValue = raf.readInt();
double doubleValue = raf.readDouble();
raf.seek(0);//设置指针的位置为文件的开始部分
byte[] bytes = new byte[12];
for (int i=0; i<bytes.length; i++)
bytes[i] = raf.readByte();//每次读一个字节,并把它赋值给字节bytes[i]
String stringValue = new String(bytes);
raf.skipBytes(1);//指针跳过一个字节
int intValue2 = raf.readInt();
raf.close();
System.out.println(booleanValue);
System.out.println(intValue);
System.out.println(doubleValue);
System.out.println(stringValue);
System.out.println(intValue2);
}
}
转载 :http://haohaoxuexi.iteye.com/blog/1235317
的读取和写入内容,这在很多时候都是很方便的。
Java代码
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* RandomAccessFile是属于随机读取类,是可以对文件本身的内容直接随机进行操作的,就是说可以指定位置
* 的读取和写入内容
* @author andy
*
*/
public class RandomAccessFileTest {
public static void main(String args[]) throws IOException {
write();
read();
}
public static void write() throws IOException {
//以读写的方式来访问该文件
RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "rw");
raf.writeBytes("Hello World!");
raf.writeBoolean(true);
raf.writeInt(30);
raf.writeDouble(3.56);
raf.close();
}
public static void read() throws IOException {
RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "r");
raf.seek(12);//设置指针的位置
boolean booleanValue = raf.readBoolean();
int intValue = raf.readInt();
double doubleValue = raf.readDouble();
raf.seek(0);//设置指针的位置为文件的开始部分
byte[] bytes = new byte[12];
for (int i=0; i<bytes.length; i++)
bytes[i] = raf.readByte();//每次读一个字节,并把它赋值给字节bytes[i]
String stringValue = new String(bytes);
raf.skipBytes(1);//指针跳过一个字节
int intValue2 = raf.readInt();
raf.close();
System.out.println(booleanValue);
System.out.println(intValue);
System.out.println(doubleValue);
System.out.println(stringValue);
System.out.println(intValue2);
}
}
转载 :http://haohaoxuexi.iteye.com/blog/1235317
相关文章推荐
- 在文件指定位置读取和写入内容RandomAccessFile
- Java RandomAccessFile 指定位置实现文件读取与写入
- 读取文件任意位置的内容——RandomAccessFile
- 读取文件任意位置的内容——RandomAccessFile
- 随机访问文件的读取和写入对象:RandomAccessFileDemo
- RandomAccessFile 替换文件内容 并写入
- RandomAccessFile随机访问文件的读取和写入
- 使用RandomAccessFile 对日志文件读取,以及读取追加的文件内容。
- C# 从内嵌资源读取文件内容并写入到指定位置
- Java中使用RandomAccessFile向文件最后写入内容
- java:RandomAccessFile随机读取文件内容
- Java按行读取正在被动态写入的大文件实例--使用RandomAccessFile(1)
- java RandomAccessFile读取文件追加内容
- java使用randomaccessfile在文件任意位置写入数据
- RandomAccessFile的读取与写入
- 使用FileReader和FileWriter读取写入文件内容
- java按指定编码写入和读取文件内容的类
- 随机读取文件--RandomAccessFile的应用实例
- 文件的指定位置读取内容
- java RandomAccessFile 向文件中写入数据,怎么样不覆盖原来的数据