2015-10-07 31 views
0

我需要打开一个文件test.txt这个文件只有一个句子,但它只在一行中。我的工作是分离每个单词并显示所有拼错的单词。需要标记仅包含一行的文件

我使用BufferReader的FileReader试过,但它只是打印出的文件名。我希望它看到第一行,并将所有单词放在数组中。如果任何人可以解释我应该如何使用BufferReaderFileReader会很好。

这是test.txt的:
The warst drought in the United States in neearly a century is expected to drive up the price of milk, beef and pork next yeer, the government said Wednesdaay, as consumers bear some of the bruntt of the sweltering heat that is drivng up the cost of feed corrn.
注:这似乎是在编辑器中一个单行。


这是我的尝试:

FileReader fr = new FileReader("test.txt"); 
BufferReader br = new BufferReader(fr); 
StringBuilder sb = new StringBuilder(); 
String s; 
while((s = br.readLine()) != null){ 
    sb.append(s); 
    sb.toString(); 
} 

感谢您的帮助。

+1

请编辑您的问题,以显示您所描述的示例代码。 – fdsa

+0

我将成为一名互联网通灵者。每个人似乎都已经认为我有一个水晶球,并且可以在没有看到它的情况下调试它们的代码...... – John3136

+0

*“但它只是打印出文件的名称”*您的代码已经打印任何内容。 –

回答

1
for (String line : Files.readAllLines(Paths.get("filepath.txt"))) { 
    // ... 
} 

Java: How to read a text file

+0

http://www.programcreek.com/2011/03/java-read-a-file-line-by-line-code-example/ –

相关问题