Reading data from multiple text files [closed]

折月煮酒 提交于 2019-12-01 02:02:19

You need to prepend the name of the directory to the filename before you attempt to open it. It's currently trying to open nokia.txt/2b.txt/etc. from the current directory.

Try:

File textFile = new File(folder.getAbsolutePath() + File.separator + files);

One of the first things a programmer needs to learn is how to figure out for oneself what is happening when one gets an error. So we wouldn't be helping you much by just giving the answer (though I see that we already have).

Another core skill all programmers need to acquire is breaking up complexity into manageable chunks. Even for someone who's been programming in Java for years, it's an effort to figure out what's going on in the code you posted.

A further skill that's very important as you progress further is knowing the standard library and using it when you can. This can often save you from reinventing the wheel.

In light of the above 3 points, here's my advice:

  • Split your code into one method that takes a File (or a String representing a filename), and counts word frequency in that file. And another method that finds and loops through all the files you want to read, calling the first method on each.
  • Test your two methods separately, which will help you track down the bug.
  • Look into the FileFilter interface.
  • Carefully read the Javadoc of the File.name() method.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!