用java读取文本文件

用java读取文本文件,java,string,parsing,arraylist,Java,String,Parsing,Arraylist,我正在编写一个“移动到前端”编码器,它读取给定的文件,然后将该文件解析为一个列表。它可以很好地使用编码,但是它只适用于只有一行的文件,我认为问题在于while循环 代码如下: while ((line = br.readLine()) != null) // While the line file is not empty after reading a line from teh text file split the line into strings { splitArr

我正在编写一个“移动到前端”编码器,它读取给定的文件,然后将该文件解析为一个列表。它可以很好地使用编码,但是它只适用于只有一行的文件,我认为问题在于while循环

代码如下:

while ((line = br.readLine()) !=
       null) // While the line file is not empty after reading a line from teh text file split the line into strings
{
  splitArray = line.split(" ");
}

for (int i = 0; i <= splitArray.length - 1;
     i++) // for every string in the array test if it exists already then output data accordinly
{
  if (FirstPass.contains(splitArray[i])) {
    System.out.println(FirstPass.lastIndexOf(splitArray[i]));
    FirstPass.addFirst(splitArray[i]);
    FirstPass.removeLastOccurrence(splitArray[i]);
  } else if (!FirstPass.contains(splitArray[i])) {
    FirstPass.addFirst(splitArray[i]);
    System.out.println("0 " + splitArray[i]);
  }
}

System.out.println(" ");
for (String S : FirstPass) {
  System.out.println(S);
}
while((line=br.readLine())=
null)//当从文本文件中读取一行后,行文件不为空时,将该行拆分为字符串
{
splitArray=line.split(“”);
}

对于(int i=0;i解析splitArray的代码在while循环之外..因此只处理最后一行

要处理每一行,请将整个
for()
块放入while循环中

while((line = br.readLine()) != null) // While the line file is not empty after reading a line from teh text file split the line into strings
{
    splitArray = line.split(" ");


    for(int i = 0; i <= splitArray.length - 1; i++)     // for every string in the array test if it exists already then output data accordinly
    {
        //..........
    } // end for
} // end while 
while((line=br.readLine())!=null)//当从文本文件中读取一行后,行文件不为空时,将行拆分为字符串
{
splitArray=line.split(“”);
对于(int i=0;i位于错误位置的大括号:

while((line = br.readLine()) != null) 
{
    splitArray = line.split(" ");
}  // This } shouuldn't be here...

抱歉,br是缓冲读取器请编辑您的文章并修复缩进。用适当数量的空格替换制表符。您的第一个while循环读取整个文件并丢弃除最后一行之外的所有数据。@JimGarrison格式很好。他只是使用了一个丑陋的大括号样式。:-)非常感谢Kal,我知道这将是一个小而愚蠢的问题。感谢你花时间回答我的问题。删除大括号只会使代码编译时出错。@MathSquared11235 100%正确。我只是指出问题的一般方面,而不是给出这个本地化问题的完整代码。