/*
* 案例 1-7: 利用 RandomAccessFile 向文件追加内容
* 目标: 掌握 RandomAccessfile 的用法
*/
import java.io.*;
public class AppendFile {
public static void main(String[] args) {
String toAppend = args[0];
try {
int i = 0;
String record = new String();
String toCn = null;
// 处理中文问题
toCn = new String(toAppend.getBytes("GBK"), "ISO8859_1");
RandomAccessFile rf = new RandomAccessFile("c://aaa.txt", "rw");
rf.seek(rf.length());
rf.writeBytes(toCn + "/n");
rf.close();
RandomAccessFile rf2 = new RandomAccessFile("c:/aaa.txt", "r");
String outCn = null;
while ((record = rf2.readLine()) != null) {
i++;
// 处理中文问题
outCn = new String(record.getBytes("ISO8859_1"), "GBK");
System.out.println("Line " + i + ":" + outCn);
}
rf2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 按如下命令方式运行这个程序 java AppendFile [内容] 将向文件中写入从控制台接收的字符串内容
*/
}