如何将字符串保存在本地缓冲区中以便扫描仪可以重新读取它?
我正在从事一些处理 HTML 页面的项目。 我不想一遍又一遍地下载同一页面,所以我想将其保存在本地。 我经常使用 Scanner
,但是 Scanner
需要一个 InputStreamReader
。 现在我设法将 HTML 页面保存到本地 StringBuffer
,但是我无法使 此StringBuffer
与Scanner
一起使用。
我可以将此 StringBuffer
用作 InputStreamReader
吗?如果不行的话我可以用什么方法呢?
I am working on some project which deals with HTML pages.
I don't want to download the same page over and over again, so I want to save it locally.
I use the Scanner
a lot, however the Scanner
need a InputStreamReader
.
For now I managed to save the HTML page to a local StringBuffer
, however I can't make
this StringBuffer
be used with the Scanner
.
Can I make this StringBuffer
be used as an InputStreamReader
? If not, then what method can I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
扫描器
有构造函数< /a> 采用Readable
。在所有Readable
实现中,CharBuffer
似乎最适合您的目的,因为您希望拥有一个可读可写的源。Scanner
has a constructor taking aReadable
. Of allReadable
implementations, theCharBuffer
seems to suit your purpose the most since you want to have a readable and writeable source.也许是这样的:
如果您想正确执行此操作,则无论如何都应该使用
If-Modified-Since
标头发送HTTP GET
请求,以便正确检测已更改的页面。您是否考虑过使用适当的 HTTP 代理?这将为您省去在应用程序中重新发明轮子的麻烦。 HTTP 代理的性能可能会更好,并且可供多个应用程序使用。
Perhaps something along the lines of:
If you want to do this right, you should send the
HTTP GET
request anyway, using theIf-Modified-Since
header, in order to correctly detect a page that has been changed.Have you considered using a proper HTTP proxy though? That would save you the hassle of re-inventing the wheel in your application. An HTTP proxy would probably perform better and it would be usable by more than a single application.