1

所以基本上我正在阅读一个包含一堆行的文本文件。我需要从文本文件中提取某些行并将这些特定行添加到字符串数组中。我一直在尝试将每个新行拆分为:“\n”、“\r”。这没有用。我也不断收到此错误:

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1 at A19010.main(A19010.java:47)

这是代码:

Path objPath = Paths.get("dirsize.txt");
    if (Files.exists(objPath)){

     File objFile = objPath.toFile();
     try(BufferedReader in = new BufferedReader(
                             new FileReader(objFile))){
          String line = in.readLine();

           while(line != null){

            String[] linesFile = line.split("\n");
            String line0 = linesFile[0];
            String line1 = linesFile[1];
            String line2 = linesFile[2];



            System.out.println(line0 + "" + line1);
            line = in.readLine();
           }

        }
         catch(IOException e){

             System.out.println(e);
         }

    }
    else
    {
      System.out.println(
              objPath.toAbsolutePath() + " doesn't exist");
    }
4

6 回答 6

3
String[] linesFile = new String[] {line}; // this array is initialized with a single element
String line0 = linesFile[0]; // fine
String line1 = linesFile[1]; // not fine, the array has size 1, so no element at second index
String line2 = linesFile[2];

You're creating a String[] linesFile with one element, line, but then trying to access elements at index 1 and 2. This will give you an ArrayIndexOutOfBoundsException

You're not actually splitting anything here. in.readLine();, as the method says, reads a full line from the file.

Edit: You can add lines (Strings) dynamically to a list instead of an array, since you don't know the size.

List<String> lines = new LinkedList<String>(); // create a new list
String line = in.readLine(); // read a line at a time
while(line != null){ // loop till you have no more lines
    lines.add(line) // add the line to your list
    line = in.readLine(); // try to read another line
}
于 2013-04-19T18:44:25.897 回答
3

readLine() method reads a entire line from the input but removes the newLine characters from it. When you split the line on \n character, you will not find one in the String. Hence, you get the exception.

Please, refer the answer in this link for more clarity.

于 2013-04-19T18:46:10.953 回答
0

You are initializing your String array with 1 element, namely line. linesFile[0] is therefore line and the rest of your array is out of bounds.

于 2013-04-19T18:44:35.143 回答
0

尝试这个:

String[] linesFile = line.split("SPLIT-CHAR-HERE");
if(linesFile.length >= 3)
{
            String line0 = linesFile[0];
            String line1 = linesFile[1];
            String line2 = linesFile[2];
// further logic here
}else
{
//handle invalid lines here
}
于 2013-04-19T18:46:52.557 回答
0

您正在使用数组来存储字符串。而是ArrayList从 Java 中使用,因为 ArrayList 是动态增长的。在您的阅读操作完成后将其转换为数组。

    String line = in.readLine();
      ArrayList<String> str_list = new ArrayList<String>();
      String[] strArr = new String[str_list.size()];

           while(line != null){
             str_list.add(line);  
             line = in.readLine();
           }
    // at the end of the operation convert Arraylist to array
return   str_list.toArray(strArr);
于 2013-04-19T18:50:27.277 回答
0

String这里的问题是,每次解析器读取新行时,您都在创建一个新数组。然后,您只使用正在读入的元素填充该String数组中的第一个元素:line

String[] linesFile = new String[] {line};

String[]由于每次while循环从顶部运行时都会使用一个元素创建一个新元素,因此您会丢失它从上一次迭代中存储的值。

解决方案是new String[];在进入 while 循环之前使用。如果您不知道如何使用ArrayList,那么我建议您使用这样的 while 循环:

int numberOfLine = 0;
while (in.readLine() != null)
{
    numberOfLine++;
}
String linesFile = new String[numberOfLine];

这将让您避免使用动态调整大小ArrayList,因为您从上面的 while 循环中知道文件包含多少行。然后,您将保留一个额外的计数器(或重新numberOfLine使用,因为我们不再使用它),以便您可以填充此数组:

numberOfLine = 0;
in = new BufferedReader(new FileReader(objFile)); // reset the buffer
while ((String line = in.readLine()) != null)
{
    linesFile[numberOfLine] = line;
    numberOfLine++;
}

此时linesFile应该使用文件中的行正确填充,以便linesFile[i]可用于访问文件中的第 i 行。

于 2013-04-19T18:54:20.450 回答