这段代码有什么问题吗?
我正在从目录的文件中读取内容。我必须根据文件名称将文件分开,然后读取它们的内容。当我简单地运行代码而不读取内容时,所有文件都会以特定文件名列出,但是当我尝试读取内容时,它只从几个文件中读取内容,实际上只有 10 个文件。但该目录大约有 1000 个特定名称的文件。我在这里发布代码。
for (i = 0; i <= filenames.length; i++) {
read = new FileReader("trainfiles/"+filenames[i]);
br = new BufferedReader(read);
if (filenames[i].matches(".*ham.*")) {
System.out.println("ham:" + filenames[i]);
while ((lines = br.readLine()) != null) {
st = new StringTokenizer(lines);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
br.close();
}
}
谁能告诉我我哪里做错了!?
谢谢
编辑#1我做了一些修改,我在这里被告知,但问题仍然存在,这是代码。
for(i=0;i<=filenames.length;i++){
read = new FileReader("trainfiles/"+filenames[i]);
br = new BufferedReader(read);
if(filenames[i].matches(".*ham.*")){
System.out.println("ham:"+filenames[i]);
while((lines = br.readLine())!= null){
st = new StringTokenizer(lines);
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
}
}
br.close();
read.close();
}
编辑 #2 现在代码看起来像这样,但同样......它没有给我我想要的结果。
for (i = 0; i < filenames.length; i++) {
try {
if (filenames[i].matches(".*ham.*")) {
read = new FileReader("trainfiles/"+filenames[i]);
br = new BufferedReader(read);
System.out.println("ham:" + filenames[i]);
while ((lines = br.readLine()) != null) {
st = new StringTokenizer(lines);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
} finally {
read.close();
br.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会像这样重写您的代码,然后看看您得到什么输出:
对原始代码的一些一般注释:
如果您确实需要数组索引,则仅使用
for
循环。首选 for-each 循环(即for (filename : filenames) ...
)。在尽可能小的范围内声明变量。在这种情况下,您应该声明您的
read
和br
变量,我将它们初始化为null
。除非要使用文件,否则切勿打开它。在这里,这意味着在条件块内打开它。
由于打开文件可能会引发异常,因此
br
可能无法初始化,在这种情况下您无法关闭
它。您需要首先检查null
。I would re-write your code like this, and see what output you get:
Some general comments on your original code:
Only use a
for
loop if you actually need the array index. Prefer a for-each loop (i.e.for (filename : filenames) ...
).Declare variables in the narrowest scope possible. In this case, you should declare your
read
andbr
variables where I initialize them tonull
.Never open a file unless you're going to use it. Here, that means opening it inside the conditional block.
Since opening a file can throw an exception,
br
may not get initialized, in which case you can'tclose
it. You need to check fornull
first.首先,您应该使用
i。其次,
matches
需要正则表达式,而不是*
-glob。您使用的表达式是[something]ham[something]
的有效正则表达式 - 这就是您的意思吗?我认为您不需要关闭 Filereader - 我认为 BR 的
close
会向上传播。但这值得检查。 编辑 正如前面提到的,您需要始终在 if 之外关闭文件。First of all you should use
i<filenames.length
. Second,matches
expects a regular expression, not*
-globs. The expression you used is a valid regular expression for[something]ham[something]
- is that what you meant?I don't think you need to close the Filereader - I think BR's
close
propagates up. But that's worth checking. EDIT as was mentioned, you need to always close the file, outside the if.您还应该关闭您的
FileReader
对象read
。除非这是家庭作业,否则我还建议您看看 commons-io。
编辑#1:我建议在finally块中执行两个关闭操作。
编辑#2:你尝试过这个吗?
You should close your
FileReader
objectread
as well.Unless this is homework, I would also suggest you take a look at commons-io.
EDIT #1: I would suggest doing both close operations in a finally block.
EDIT #2: Did you try this?
1000 多个文件需要读取的文件量很大。如果它无法读取文件,它应该抛出异常(具体来说是 IOException)。也许打印 catch 块中的异常消息并将其粘贴到此处。
我不知道 StringTokenizer 类,但是当您仅打印没有 StringTokenizer 的行时,代码是否会出错?
另一种选择是使用线程。您拥有文件数组,然后启动一些读取文件的线程(生产者/消费者问题)。
顺便说一句,您可以使用 FileFilter 类来过滤文件。
http ://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#listFiles%28java.io.FileFilter%29
1000+ files are a lot of files to read. If it can't read a file it should throw an exception (IOException to be specific). Maybe print the exception message in the catch block and paste it here.
I don't know the StringTokenizer class but does the code give errors when you just print the line without the StringTokenizer?
An other option is to use threads. You have the array of files and then you start some threads who reads a file (producer/consumer problem).
By the way, you can filter files with the class FileFilter.
http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#listFiles%28java.io.FileFilter%29