我有两种方法: saveSubscribedFeeds()
以及 loadSubscribedFeeds()
. 在save方法中,我将一些数据保存到文件中 feedsFile
在如下参数中:
@Override
public void saveSubscribedFeeds(List<Feed> feeds, File feedsFile) {
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(feedsFile));
for (Feed feed : feeds) {
pw.println(feed.getTitle());
pw.println(feed.getDescription());
pw.println(feed.getEntries());
pw.println(feed.getUrl());
pw.println(feed.getPublishedDateString());
}
pw.close();
}
catch(IOException i) {
i.printStackTrace();
}
}
在load方法中,我尝试加载完全相同的文件 feedsFile
作为列表返回,为此我使用了扫描仪。
@Override
public List<Feed> loadSubscribedFeeds(File feedsFile) throws FileNotFoundException {
Scanner s = new Scanner(feedsFile.getAbsoluteFile());
List<Feed> listFeed = new ArrayList<>();
while (s.hasNextLine()) {
listFeed.add(new Feed(s.nextLine()));
}
s.close();
return listFeed;
}
但是,junit测试告诉我,我的列表与读取对象不匹配:
if (!feedsTemp.get(0).getUrl().equals(TEST_FEED_URL)
|| !feedsTemp.get(0).getTitle().equals(TEST_FEED_TITLE)
|| !feedsTemp.get(0).getDescription().equals(TEST_FEED_DESC)) {
System.err.println("The data read from save-file '" + FEEDS_FILE.getName() + "' doesn't match the test input data!");
fail("feed sample data (" + FEEDS_FILE.getAbsolutePath() + ") doesn't match read object!");
}
我做错什么了?下面是feed类:
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import de.uk.java.feader.utils.FeaderUtils;
public class Feed implements Serializable, Comparable<Feed> {
private static final long serialVersionUID = 1L;
private String url;
private String title;
private String description;
private String publishedDateString;
private List<Entry> entries;
public Feed() {
}
public Feed(String url) {
super();
this.url = url;
this.entries = new ArrayList<Entry>();
this.title = "";
this.description = "";
this.publishedDateString = "";
}
/**
* Creates an instance of a Feed and transfers the feed
* data form a SyndFeed object to the new instance.
* @param url The URL string of this feed
* @param sourceFeed The SyndFeed object holding the data for this feed instance
*/
public Feed(String url, SyndFeed sourceFeed) {
this(url);
setTitle(sourceFeed.getTitle());
setDescription(sourceFeed.getDescription());
if (sourceFeed.getPublishedDate() != null)
setPublishedDateString(FeaderUtils.DATE_FORMAT.format(sourceFeed.getPublishedDate()));
for (SyndEntry entryTemp : sourceFeed.getEntries()) {
Entry entry = new Entry(entryTemp.getTitle());
entry.setContent(entryTemp.getDescription().getValue());
entry.setLinkUrl(entryTemp.getLink());
entry.setParentFeedTitle(getTitle());
if (entryTemp.getPublishedDate() != null) {
entry.setPublishedDateString(FeaderUtils.DATE_FORMAT.format(entryTemp.getPublishedDate()));
}
addEntry(entry);
}
}
public String getUrl() {
return url;
}
public void setTitle(String title) {
this.title = title != null ? title : "";
}
public String getTitle() {
return title;
}
public void setDescription(String description) {
this.description = description != null ? description : "";
}
public String getDescription() {
return description;
}
public void setPublishedDateString(String publishedDateString) {
this.publishedDateString = publishedDateString != null ? publishedDateString : "";
}
public String getPublishedDateString() {
return publishedDateString;
}
/**
* Returns a short string containing a combination of meta data for this feed
* @return info string
*/
public String getShortFeedInfo() {
return getTitle() + " [" +
getEntriesCount() + " entries]: " +
getDescription() +
(getPublishedDateString() != null && getPublishedDateString().length() > 0
? " (updated " + getPublishedDateString() + ")"
: "");
}
public void addEntry(Entry entry) {
if (entry != null) entries.add(entry);
}
public List<Entry> getEntries() {
return entries;
}
public int getEntriesCount() {
return entries.size();
}
@Override
public boolean equals(Object obj) {
return (obj instanceof Feed)
&& ((Feed)obj).getUrl().equals(url);
}
@Override
public int hashCode() {
return url.hashCode();
}
@Override
public String toString() {
return getTitle();
}
@Override
public int compareTo(Feed o) {
return getPublishedDateString().compareTo(o.getPublishedDateString());
}
}
1条答案
按热度按时间q7solyqu1#
首先:当你遇到这样的问题时,试着使用调试器来看看发生了什么。您可以在代码中逐行前进,还可以查看变量及其内容。第二:你在那里面临的问题,正如图萨提到的,你正在写标题,描述,。。。每次都换一行。然后,当你尝试读取对象时,你只读取一行,然后尝试创建一个新的feed对象,在其中填充字段title,description。。。构造函数中的“”和给定参数的url。那么文件中保存描述的下一行会发生什么呢?它将生成一个feed对象,其描述设置为url。当然,junit说,它不是同一个对象。您现在可以做什么:
用一个常量字符分隔字段,并且只为新的提要对象换行。然后从单独的行中解析字段来填充对象。例如:url;头衔;描述;发布日期\努尔2;标题2;说明2;发布日期2\n而\n明显地标记了换行符
而是创建一个xml并使用适当的解析库。您已经可以使用java内置lib了。以下是其中一个教程的链接:https://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm