我想从文件中读取文本。每行应拆分为 180 个字符,然后应将每一行添加到表中,但我收到错误消息“字符串索引超出范围 -180”。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadAndWrite {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader (new FileReader("xyz.txt"));
String line = read.readLine();
int len = line.length();
System.out.println(len);
int chaCount = 0;
while (chaCount < len) {
String line2 = line.substring(chaCount, 180);
System.out.println(line2);
chaCount += 180;
}
}
}