Jo.*_*o.P 1 java text file readline token
我是新来的,只是努力尝试获取文本文件.在每一行上都有一个单词和相应的数字代码.我们的想法是阅读它并将代码和单词放在单独的变量中.我对这个领域了解不多,但我一直在网上寻找并提出以下建议:
try{
FileReader freader=new FileReader(f);
BufferedReader inFile=new BufferedReader(freader);
while (inFile.readLine()!=null){
String s=null;
s=inFile.readLine();
System.out.println(s);
String[] tokens=s.split(" ");
string=tokens[0];
System.out.println(string);
code=tokens[1];
System.out.println(code);
c.insert(string, code);
}//end outer while
}//end try
Run Code Online (Sandbox Code Playgroud)
问题是未读取文本文件的第一行.然后它每次都跳过一条线!(换句话说,只读取第1行,第3行,第5行,第7行等)
正如我上面所说,我是新手,我不太了解我在网上不同网站上看到的所有不同内容(比如为什么所有内容都是bufferedThis或bufferedThat,或者如何正确使用所有令牌化器的东西).我在不同的时间尝试了一些不同的东西,最后得到了这个.
你的while循环吞噬了文件中的一半行.
while (inFile.readLine()!=null)
Run Code Online (Sandbox Code Playgroud)
这会读取一行,但不会将其分配给任何内容.String在循环之前声明一个并以这种方式读取每一行.
String line;
while ((line = inFile.readLine()) != null)
Run Code Online (Sandbox Code Playgroud)
现在变量line将在循环内可用,因此您不需要inFile.readLine()在循环中调用.
| 归档时间: |
|
| 查看次数: |
1536 次 |
| 最近记录: |