【发布时间】:2020-03-23 07:13:52
【问题描述】:
我正在使用 BufferedReader 从文件中读取 Word 对象,但事实证明 BufferedReader 被设计为只能读取字符串,而不是用户定义的对象。
在这种情况下,Word 对象是一个字符串,只是没有定义为字符串
换句话说,如何将 str2[i] 从 String 转换为 Word 对象以便将其入队?
这是因为 enqueue 方法接受 Word 对象。
public static void main(String[] args) {
WordPriorityQueue x = new WordPriorityQueue();
File file = new File("file goes here");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while((st = br.readLine()) != null) {
String str1 = br.readLine();
String str2[] = str1.split(" ", 500);
int i = 0;
while(str2[i] != null) {
//How can I convert the string str2[i] to a Word object?
x.enqueue(str2[i]); //Doesn't work for Strings
i++;
}
}
}
【问题讨论】:
-
您没有使用
BufferedReader。我不确定什么是“Word”对象,但要读取二进制数据,您使用InputStream,要读取Java 序列化对象,您可以使用ObjectInputStream包装它们。但是请edit这个问题并解释一下Word对象是什么。 -
或者你只需要从一个字符串构造一个单词。像
new Word(string)这样的东西。但由于我们不知道 Word 类是什么,所以很难说。
标签: java arrays oop object bufferedreader