从源码包加载文件
我正在尝试从 src 包加载文本文件。我不想将其作为输入流加载。我想将它作为 FileReader 加载。
看看 netbeans 如何加载图标,我尝试使用代码:
getClass().getResourcesAsStream("/getresources/test.txt");
但是,我找不到任何方法将输入流转换为文件读取器。无论如何,请执行此操作,以便我可以使用 FileReader。我知道我可以自己将输入流分成几行,但这似乎需要很多工作。
谢谢!
恩特
I am trying to load a text file from the src package. I don't want to load it as an input stream. I would like to load it as an FileReader.
Looking at how netbeans loads icons, I tried using the code:
getClass().getResourcesAsStream("/getresources/test.txt");
However, I can't find any way to convert an inputstream into a filereader. Is there anyway do do this so that I can use a FileReader. I know I could break the inputstream up into lines myself but that seems like to much work.
Thanks!
nt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想改用 BufferedReader。
它具有与 Filereader 相同的优点,可以逐行读取等,并接受一个接受 InputStream 的 InputStreamReader。
这些 IO 类实际上是众所周知的装饰模式的实现。
如果您阅读该模式,您可能会更多地了解所有这些 IO 类。
FileReader 需要一个文件。
You would probably want to use an BufferedReader instead.
It has the same benefits of a Filereader, can read line by line etc, and accepts a InputStreamReader which accepts an InputStream.
These IO classes are actually an implementation of the well known Decorator Pattern.
If you read up on that pattern you will probably understand all these IO classes more.
A FileReader expects a File.
请注意,可能无法从文件系统访问类路径资源。另外,我假设任何像样的 API 都会接受
Reader
而不是FileReader
。你可以这样做:Note that a classpath resource might not be accessible from the file system. Also, I'd assume any decent API would accept a
Reader
rather than aFileReader
. You can do:由于您有流式资源(可能来自 URL)而不是文件,因此我建议使用 InputStreamReader。
Since you have a streamed resource (which could come from a URL) and not a file, i would suggest using an InputStreamReader.