获取 java.lang.StringIndexOutOfBoundsException :字符串索引超出范围:(无法弄清楚)
我有一段代码 -
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.eclipse.core.runtime.Platform;
public class RAF {
public static void main(String[] args) {
File file = new File("test.txt");
Scanner scanner;
try {
scanner = new Scanner(file).useDelimiter("\n");
String line = scanner.next();
String newLine = line.substring(0, 252) + "<input type=\"button\" value = \"abhishek\" />" + line.substring(252);
FileWriter writer = new FileWriter(file);
writer.write(newLine);
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
test.txt 文件是 -
> <!DOCTYPE html PUBLIC "-//W3C//DTD
> HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> <html> <head> <meta
> http-equiv="Content-Type"
> content="text/html;
> charset=ISO-8859-1"> <title>Insert
> title here</title> </head> <body>
>
> <form><input></form> </body> </html>
test.txt 的总长度是 285,我想在 252 位置添加内容,以便输出为 -
> "http://www.w3.org/TR/html4/loose.dtd">
> <html> <head> <meta
> http-equiv="Content-Type"
> content="text/html;
> charset=ISO-8859-1"> <title>Insert
> title here</title> </head> <body>
>
> <form>**<input type="button" value =
> "abhishek"/>**<input></form> </body>
> </html>
但我遇到了例外 - 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:252 在 java.lang.String.substring(来源未知) 在 com.lg.palette.elementEditFactory.RAF.main(RAF.java:25)
代码有什么问题 我的主要目标是获取第二个 test.txt 中所示的内容
I have a piece of code -
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.eclipse.core.runtime.Platform;
public class RAF {
public static void main(String[] args) {
File file = new File("test.txt");
Scanner scanner;
try {
scanner = new Scanner(file).useDelimiter("\n");
String line = scanner.next();
String newLine = line.substring(0, 252) + "<input type=\"button\" value = \"abhishek\" />" + line.substring(252);
FileWriter writer = new FileWriter(file);
writer.write(newLine);
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
and the test.txt file is -
> <!DOCTYPE html PUBLIC "-//W3C//DTD
> HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> <html> <head> <meta
> http-equiv="Content-Type"
> content="text/html;
> charset=ISO-8859-1"> <title>Insert
> title here</title> </head> <body>
>
> <form><input></form> </body> </html>
The total length of test.txt is 285 and I want to add the content at 252 location so that the output will be -
> "http://www.w3.org/TR/html4/loose.dtd">
> <html> <head> <meta
> http-equiv="Content-Type"
> content="text/html;
> charset=ISO-8859-1"> <title>Insert
> title here</title> </head> <body>
>
> <form>**<input type="button" value =
> "abhishek"/>**<input></form> </body>
> </html>
But I am getting exception -
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 252
at java.lang.String.substring(Unknown Source)
at com.lg.palette.elementEditFactory.RAF.main(RAF.java:25)
What is the problem with the code
My main objective is to get the content as shown in the 2nd test.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仅读取文件的第一行...长度不足 252 个字符。看起来您确实想读取整个文件然后对其进行修改。
Guava 在
Files
中提供了一些有用的方法,可以一次性读取整个文件。(总的来说,这感觉像是一种非常脆弱的方法......您对分割点始终是 HTML 中的 252 个字符有多大信心?)
You're only reading the first line of the file... which isn't 252 characters long. It looks like you really want to read the whole file in and then modify it.
Guava has some useful methods in
Files
to read an entire file in one go.(In general this feels like a pretty fragile approach though... how confident are you that the split point will always be 252 characters into the HTML?)