我正在尝试读取文件,然后将一些文本附加到文件中的某个位置(即@offset jabjab)。当我尝试在偏移量jabjab 处写入文件时,就会出现问题。有什么错误?
文件内容:
Mi
<?xml Version="1.0"?>
_
File f = new File("data.dat");
String brstring = null;
String entrystring = null;
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
StringBuilder result = new StringBuilder();
while ((line = br.readLine()) != null) {
result.append(line+"\r\n");
}
br.close();
System.out.print(result);
int jabjab = result.indexOf("?>");
System.out.println(jabjab);
PrintWriter fo = new PrintWriter(f);
fo.write("ok", jabjab, 2);
fo.flush();
fo.close();
} catch (Exception ex) {
System.out.print(ex.getMessage());
}
控制台输出包括错误:
Mi// output of the result string
<?xml Version="1.0"?>//output of the result string
23//output of jabjab
String index out of range: 25String index out of range: 25//output of exception
此外,完成此方法后,原始文件现在为空...