无法从RandomAccessFile java获取正确的输出

无法从RandomAccessFile java获取正确的输出,java,system.out,randomaccessfile,Java,System.out,Randomaccessfile,这是我运行的代码 import java.io.*; public class SETBQ1 { public static void main(String[] args) throws Exception { RandomAccessFile item = new RandomAccessFile("item.dat", "rw"); String s; while ((s = item.readLine()) != null) { System.ou

这是我运行的代码

import java.io.*;
public class SETBQ1 {
  public static void main(String[] args) throws Exception {
    RandomAccessFile item = new RandomAccessFile("item.dat", "rw");
    String s;
    while ((s = item.readLine()) != null) {
      System.out.println(s);
    }
    System.out.println(s);
    item.close();
  }
}
顺便说一下,item.dat的内容是

1 pencil 100 10
2 pen 200 5
3 eraser 5 1000
4 book 500 12
然而,我得到的结果是

PS F:\tymalik\Malik\SEM 1\JAVA\ASS5> java SETBQ1
1 pencil 100 10
2 pen 200 5
3 eraser 5 1000
4 book 500 12
null
我想知道为什么最后一个值打印为null而不是值? 在while循环之外处理字符串变量的解决方案是什么?如果您能在这方面提供帮助,我将不胜感激。

最后一句话

System.out.println(s);
显示
null
,因为它不在
while
循环的范围内。您可以在循环中指定另一个变量

String lastLine = null;
while ((s = item.readLine()) != null) {
    System.out.println(s);
    lastLine = s;
}
System.out.println(lastLine);

但我想在白圈之外进一步处理它。那我该怎么办?@malikbagwala你“想进一步处理”什么?此时,变量
s
始终保持值
null
,因为这是离开循环的条件。我想搜索其中最昂贵的项目。这与您最初的问题大不相同。Hi@malik bagwala,您想使用您用于阅读的行吗?