Java 将文本文件内容保存到链接列表
我正在尝试将word文件的内容逐行保存到LinkedList中。 我做错了什么?控制台显示它确实正在读取文件,但没有保存其内容Java 将文本文件内容保存到链接列表,java,Java,我正在尝试将word文件的内容逐行保存到LinkedList中。 我做错了什么?控制台显示它确实正在读取文件,但没有保存其内容 public class SpellCheck { LinkedList<String> lines = new LinkedList(); boolean suggestWord ; public static void main(String[] args) throws java.io.IOException{ System.out.pr
public class SpellCheck {
LinkedList<String> lines = new LinkedList();
boolean suggestWord ;
public static void main(String[] args) throws java.io.IOException{
System.out.println("Welcome to the spellchecker");
LinkedList<String> list = new LinkedList<String>();
try {
File f = new File("input/dictionary.txt");
FileReader r = new FileReader(f);
BufferedReader reader = new BufferedReader(r);
String line = null;
String word = new String();
while((line = reader.readLine()) != null)
{
list.add(word);
word = new String();
}
reader.close();
}catch(IOException e) {
e.printStackTrace();
}
for(int i = 0; i<list.size();i++){
System.out.println(list.get(i));
}
}
}
公共类拼写检查{
LinkedList lines=新建LinkedList();
布尔暗示性词;
公共静态void main(字符串[]args)引发java.io.IOException{
System.out.println(“欢迎使用拼写检查器”);
LinkedList=新建LinkedList();
试一试{
文件f=新文件(“input/dictionary.txt”);
FileReader=新的FileReader(f);
BufferedReader读取器=新的BufferedReader(r);
字符串行=null;
字符串字=新字符串();
而((line=reader.readLine())!=null)
{
列表。添加(word);
word=新字符串();
}
reader.close();
}捕获(IOE异常){
e、 printStackTrace();
}
对于(int i=0;i您正在添加word
,这是一个空字符串,而不是添加从文件中读取的行:
String word = new String();
while((line = reader.readLine()) != null)
{
list.add(word);
^^^^^
word = new String();
}
应该是:
while((line = reader.readLine()) != null) {
list.add(line);
}