获取 java.lang.StringIndexOutOfBoundsException :字符串索引超出范围:(无法弄清楚)

发布于 2024-11-03 18:22:42 字数 2148 浏览 4 评论 0原文

我有一段代码 -

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2024-11-10 18:22:42

您仅读取文件的第一...长度不足 252 个字符。看起来您确实想读取整个文件然后对其进行修改。

GuavaFiles 中提供了一些有用的方法,可以一次性读取整个文件。

(总的来说,这感觉像是一种非常脆弱的方法......您对分割点始终是 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?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文