读入文件-缺少输入 [英] Reading in file - input missing
问题描述
I was trying to help this user with an answer, but reading in the file isn't working as I expected.
如果我创建的文件只包含OP的前两行:
If I create a file with just the OPs first two lines:
学习Java-William Lane -12.00-Wiley-0811234561
Learning Java - William Lane -12.00 - Wiley - 0811234561
-威廉·斯托林(William Stalling)-25.00-普伦蒂斯·霍尔(Prentice Hall)-1304086641
- William Stalling - 25.00 - Prentice Hall - 1304086641
输出符合预期:
Learning Java - William Lane -12.00 - Wiley - 0811234561
- William Stalling - 25.00 - Prentice Hall - 1304086641
但是当我添加第三行时:
But when I add the third line:
学习Java-William Lane -12.00-Wiley-0811234561
Learning Java - William Lane -12.00 - Wiley - 0811234561
-威廉·斯托林-25.00-普伦蒂斯·霍尔-1304086641
- William Stalling - 25.00 - Prentice Hall - 1304086641
OOP编程-格雷厄姆·温特-32.50 –奥赖利-0471974555
OOP programming - Graham Winter - 32.50 – O'Reilly - 0471974555
输出为空白.
我的代码很简单,只是为了测试它.我直接从上面的链接问题复制并粘贴了文件内容.该行上是否有特殊字符,如果是,如何找到它?为什么不打印其他行?
My code is straight forward, just to test it. I copied and pasted the file contents directly from the linked question above. Is there a special character on that line, and if so, how to find it? And why would it not print the other lines?
Scanner sc = new Scanner(new File("myfile"));
while (sc.hasNextLine()) {
String nameLine = sc.nextLine();
System.out.println(nameLine);
}
我通过调试器运行它,sc.hasNextLine
返回false
,程序结束.
I ran it through a debugger and sc.hasNextLine
returns false
and the program ends.
我基本上是使用Notepad
进行测试的,并且(在浏览器上)注意到32.50和O'Reilly之间的破折号略有拉长.因此,我删除了该破折号并保存,该程序按预期工作.当我把它放回去并保存并重新运行该程序时,它什么也不显示.我每次都能生产.
I'm using Notepad
basically to test this, and noticed (on the browser) that the dash between 32.50 and O'Reilly is slightly elongated. So I removed this dash and saved and the program works as expected. When I put it back in and save and re-run the program, it doesn't display anything. I can produce this every time.
然后我在Notepad++
中进行了测试,所有工作均顺利进行.快乐的日子.
I then tested in Notepad++
and all worked smoothly. Happy days.
所以我用WinMerge
在两个文件之间进行了比较;它基本上说唯一的区别是破折号,在Notepad++
上是â€
,在Notepad
上是–
.
So I did a comparison between the two files with WinMerge
; it basically says the only difference is that dash, and on Notepad++
, it's â€"
but on Notepad
, it's –
.
有任何想法吗?
推荐答案
可能是因为文件编码/字符集不同.尝试以UTF-8模式读取文件并进行检查.
THis might probably because of difference in file encoding/character set. Try reading the file as UTF-8 mode and check.
这篇关于读入文件-缺少输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!