在Java中使用Scanner类获取文件的下一行时遇到问题
我正试图接到下一个电话,但我打电话的时候似乎已经收到了整个文件的其余部分在Java中使用Scanner类获取文件的下一行时遇到问题,java,file-io,java.util.scanner,Java,File Io,Java.util.scanner,我正试图接到下一个电话,但我打电话的时候似乎已经收到了整个文件的其余部分 header = getHeaderLine(fname, columns); Scanner inputFile = null; try{ inputFile = new Scanner(new FileInputStream(fname)); } catch (FileNotFoundException e){ System.out.println("File is not found.");
header = getHeaderLine(fname, columns);
Scanner inputFile = null;
try{
inputFile = new Scanner(new FileInputStream(fname));
} catch (FileNotFoundException e){
System.out.println("File is not found.");
System.exit(0);
}
if (hasHeader) inputFile.nextLine();
String line = inputFile.nextLine();
for(int i=0; i<body.length; i++){
body[i] = getEntries(line, columns);
line = inputFile.nextLine();
}
我认为它正在运行文件,并试图抓住下一行,一旦我已经达到了文件的结尾。我需要更改停止条件。问题在于
行尾字符。行尾字符在不同的操作系统linux、mac、windows/DOS等之间有所不同
检查文件是否有您正在运行的操作系统的右行尾字符。在使用它之前,检查您是否有下一行
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at CFR.main(CFR.java:50)