创建读取连续文件的自定义迭代器
我们有
public class UKWacSentenceIterator implements SentenceIterator
显然是一个 Ttererator,但我没有任何有关 SentenceIterator
中内容的信息。此类具有以下属性:Scanner fileScanner
。
这个想法是,构造函数接受一个文件数组:
public UKWacSentenceIterator() throws IOException {
Properties p = new Properties();
p.load(prop.class.getClassLoader()
.getResourceAsStream("sources/ukwacdump.properties"));
Enumeration<Object> keys = p.elements();
while (keys.hasMoreElements()) {
source.add(keys.nextElement());
}
fileScanner = new Scanner(new File((String) source.get(0)));
}
在 main 方法中,我们可以使用 for 循环:
public static void main(String[] args) throws IOException {
for(String line : new UKWacSentenceIterator()) {
System.out.println(line);
}
}
他目前在使用这个 for 循环时遇到了问题,因为一旦第一个文件是 EOF,for 循环就会出现问题。就停止了。所以他认为重写是个好主意
@Override
public boolean hasNext() {
if(tmp != null) {
return true;
}
if (this.fileScanner.hasNext()) {
try {
this.skipToSequenceStart();
String sent = this.scanSentence();
this.tmp = sent;
return true;
} catch (Exception e) {
return false;
}
} else {
return advanceFileScanner();
}
}
,但他不知道如何构建 advanceFileScanner()
。
我的想法是仅将变量 fileScanner
分配给具有下一个文件名的新 Scanner
,然后复制
this.skipToSequenceStart();
String sent = this.scanSentence();
this.tmp = sent;
return true;
我不知道他是否尝试过。我想知道您是否认为这是一个好主意,以及您是否可以向我推荐一个关于如何创建可迭代对象的好教程。因为现在我只是猜测,除了 hasNext()
之外,我不知道 for 循环还使用什么。
We have
public class UKWacSentenceIterator implements SentenceIterator
which is obviously an Tterator but I don't have any information on what's in SentenceIterator
. This class has this property: Scanner fileScanner
.
The idea is that the constructor takes an array of files:
public UKWacSentenceIterator() throws IOException {
Properties p = new Properties();
p.load(prop.class.getClassLoader()
.getResourceAsStream("sources/ukwacdump.properties"));
Enumeration<Object> keys = p.elements();
while (keys.hasMoreElements()) {
source.add(keys.nextElement());
}
fileScanner = new Scanner(new File((String) source.get(0)));
}
And in the main method we can use a for loop:
public static void main(String[] args) throws IOException {
for(String line : new UKWacSentenceIterator()) {
System.out.println(line);
}
}
He has currently having a problem with this for loop because once the first file is EOF
the for just stops. So he thought would be a good idea to override
@Override
public boolean hasNext() {
if(tmp != null) {
return true;
}
if (this.fileScanner.hasNext()) {
try {
this.skipToSequenceStart();
String sent = this.scanSentence();
this.tmp = sent;
return true;
} catch (Exception e) {
return false;
}
} else {
return advanceFileScanner();
}
}
But he doesn't know how to build advanceFileScanner()
.
My idea is to just to assign the variable fileScanner
to a new Scanner
with the next file name and then just copy
this.skipToSequenceStart();
String sent = this.scanSentence();
this.tmp = sent;
return true;
I don't know if he tried yet. I was wondering if you think is a good idea and if you can suggest me a good tutorial on how to create an iterable object. Because right now I'm just guessing, I don't know what the for loop use other than hasNext()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定,但你的问题不只是你
只包含我解释的 1 个文件
。我过去常常读取许多文件,给定一个包含我必须读取的所有文件的字符串数组。我,我就是这样做的,我只是声明为一个[]。我给你一个我的代码的例子。
它带有缓冲阅读器,但我认为您可以将其应用到您的代码中。你能做类似的事情吗(源是一个数组吗?我假设是,所以我使用长度。也许你的情况是“size()”)。
然后当然你必须重构其余的代码来处理文件扫描仪数组
希望它有帮助
I am not sure but isn't your problem simply that your
only contains 1 file
I explain. I use to read in many file given a string array of all the files I have to read. Me, I do it that way, I simply declare as an []. I give you an exemple of my code.
It is with buffered reader but I think you could apply it to your code. Could you do something like that (is source an array ? i assume yes so i use length. Perhaps it's "size()" in your case).
Then of course you have to refactor the rest of the code to handel the filescanner array
Hope it helps