最佳答案
import java.io.File;
import java.io.RandomAccessFile;
public class AddCharacter {
private static RandomAccessFile accessFile;// 定义RandomAccessFile对象对文件进行操作
private static String dir = "D:\\out.txt";// 文件路径
/**
* 往文件追加新的字符
*
* @param newChar
* 字符
* @return 追加成功返回true,否则返回false
*/
public static boolean addChar(char newChar) {
try {
File file = new File(dir);
accessFile = new RandomAccessFile(file, "rw");
long length = file.length();// 获取文件长度
accessFile.seek(length);// 末尾处添加内容
accessFile.writeChars(String.valueOf(newChar));// 写入操作
accessFile.close();
return true;
} catch (Exception e) {// 异常捕获
// TODO: handle exception
e.printStackTrace();
}
return false;</