PrintWriter 异常:字符串索引超出范围
我正在尝试读取一个文件,然后将一些文本附加到文件中的某个位置(即@ 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
此外,完成此方法后,原始文件现在为空...
I'm trying to read a file then append some text to a certain place in the file (ie. @ offset jabjab). The problem occurs when i'm trying to write to the file at offset jabjab. what's the error?
File contents:
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());
}
Console output including error:
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
Also, after this method is done the original file is now empty...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您误解了 PrintWriter.write(string,offset,length) 的定义。如果我正确地阅读了您的问题,您认为它将在该偏移量处写入输出文件。但是,偏移量指定要写入的字符串中的起始位置,因此您尝试从偏移量 23 处开始写入字符串“ok”。由于该字符串只有 2 个字符,因此会出现异常。
如果您确实想覆盖文件中的特定字节,请查看 java.io.RandomAccessFile。请注意,虽然您可以用其他字节覆盖文件中的特定字节,但如果不将数据读入内存并将新副本写入磁盘,则无法“插入”数据或从文件中删除数据(导致文件长度不同)。
I think you've misunderstood the definition of PrintWriter.write(string,offset,length). If I read your question correctly, you think it will write into the output file at that offset. However, the offset specifies where in the string being written to start, so you're trying to write from the string "ok" starting at offset 23. Since the string only has 2 characters you get the exception.
Take a look at java.io.RandomAccessFile if you really want to overwrite specific bytes in a file. Note that, while you can overwrite specific bytes in a file with other bytes, you cannot "insert" data or delete data from a file (resulting in a file of different length) without reading it into memory and writing a new copy to disk.