扫描程序读取您的文件时出现问题,但是我不确定它是什么。它错误地认为,当文件尚未到达文件末尾时,它可能是由于某些时髦的String编码所致。尝试使用包装了FileReader对象的BufferedReader对象。
例如,
private static Set<String> posible2(String posLoc) { Set<String> result = new TreeSet<String>(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(new File(posLoc))); String availalbe; while((availalbe = br.readLine()) != null) { result.add(availalbe); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }编辑
我尝试将您的问题减少到最低限度,而这足以引发问题:
public static void main(String[] args) { try { Scanner scanner = new Scanner(new File(FILE_POS)); int count = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.printf("%3d: %s %n", count, line ); count++; }我用printf检查了Scanner对象:
System.out.printf("Str: %-35s size%5d; Has next line? %b%n", availalbe, result.size(), s.hasNextLine());并表明它认为文件已结束。我正在逐步从数据到文件中删除行,以查看引起问题的行,但将其留给您。



