RadomAccessFile_java读写文件时如…
来源:互联网 发布:博客导航源码 编辑:程序博客网 时间:2024/05/29 18:43
如果用 流读取文件FileInputStream,FileReader 只能顺序读取。读一个字符指针移动一个位置
如果用 RadomAccessFile,可以用 seek 指定移动到文件的某个位置。
位置定位以字符为单元
要定位到某行,试试 java.io.LineNumberReader 和 java.io.LineNumberInputStream
如果用 RadomAccessFile,可以用 seek 指定移动到文件的某个位置。
位置定位以字符为单元
要定位到某行,试试 java.io.LineNumberReader 和 java.io.LineNumberInputStream
例子: RadomAccessFile
package org.kodejava.example.io;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileExample {
public static void main(String[] args) {
try {
//
// Create a new instance of RandomAccessFile class. We'll do a "r"
// read and "w" write operation to the file. If you want to do a write
// operation you must also allow read opeartion to the RandomAccessFile
// instance.
//
RandomAccessFile raf = new RandomAccessFile("books.dat", "rw");
//
// Let's write some book's title to the end of the file
//
String books[] = new String[5];
books[0] = "Professional JSP";
books[1] = "The Java Application Programming Interface";
books[2] = "Java Security";
books[3] = "Java Security Handbook";
books[4] = "Hacking Exposed J2EE & Java";
for (int i = 0; i < books.length; i++) {
raf.writeUTF(books[i]);
}
//
// Write another data at the end of the file.
//
raf.seek(raf.length());
raf.writeUTF("Servlet & JSP Programming");
//
// Move the file pointer to the beginning of the file
//
raf.seek(0);
//
// While the file pointer is less than the file length, read the
// next strings of data file from the current position of the
// file pointer.
//
while (raf.getFilePointer() < raf.length()) {
System.out.println(raf.readUTF());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
0 0
- RadomAccessFile_java读写文件时如…
- 如何用JSON读写XML文件
- 如何用JSONKit读写JSON文件
- 如何用javascript操作本地文件(读写txt文件)
- 如何用javascript操作本地文件(读写txt文件)
- 如何用QT实现txt文件的读写
- 手把手教你如何用读写plist文件
- 【Java I/O】如何用Java读写文件
- python 中对配置文件(如.ini文件)进行读写等操作ConfigParser模块
- java中如何用使用输入输出流读写文件(事例)
- 如何用c#读写配置文件
- 如何用c#读写配置文件
- 如何用JSP读写Cookie
- 文件读写、剪贴板读写
- 读写文件
- 文件读写
- 读写文件
- 读写文件
- Android一些常用知识和代码(不断…
- Android 解决ListView 和 ScrollVi…
- Android 数字签名学习笔记
- 刷钻骗局
- 牛人博客
- RadomAccessFile_java读写文件时如…
- Cocos2d-x 3.0 动作
- java RandomAccessFile
- [Java]读取文件方法大全zz
- 文件输入输出流 读写测试断点续传
- JAVA中OutStream中方法WRITE用法
- AdapterView was not registered
- Android获取其他包的Context和在任…
- android ListView的美化涉及到的一…