Java FileReader未读取整个文本文件

Java FileReader未读取整个文本文件,java,input,inputstream,filereader,txt,Java,Input,Inputstream,Filereader,Txt,我希望我的代码读取一个txt文件并打印出每一行,但几乎有一半的行被跳过,看起来是随机的。如何确保读取整个文件 BufferedInputStream readIt = new BufferedInputStream(new FileInputStream(pBase)); //pBase is txt File object Scanner actualRead = new Scanner(readIt); while(

我希望我的代码读取一个txt文件并打印出每一行,但几乎有一半的行被跳过,看起来是随机的。如何确保读取整个文件

        BufferedInputStream readIt = new BufferedInputStream(new FileInputStream(pBase));
        //pBase is txt File object
        Scanner actualRead = new Scanner(readIt);

    
        while(actualRead.hasNextLine()){
            System.out.println("Line is : " + actualRead.nextLine());
            
        }

最简单的方法是使用nio实用程序方法之一,例如

对于不希望同时加载到内存中的大型文件,可以使用如下方法:

导入java.nio.file.Files;
导入java.nio.file.Path;
...
try(var lines=Files.lines(path.get(pBase))){
行。forEach(l->{
系统输出打印LN(l);
});
}

或者..扫描仪可以将文件参数作为输入


扫描仪实际读取=新扫描仪(pBase)//直接将文件作为参数传递。。。
//尽管这可能会引发IOException..在try catch或add to throws中捕获它。。。
试一试{
扫描仪实际读取=新扫描仪(pBase);
while(actualRead.hasNextLine()){
System.out.println(“行为:+actualRead.nextLine());
}
捕获(IOE异常){
System.out.println(“err…”);