如何使用Java从Internet上读取文件?

时间:2021-07-15 15:26:41

I am trying to make a changelog that gets updated at runtime, now, I would like to download the changelog from my website, and then display it. How would I do this?

我正在尝试制作一个在运行时更新的更改日志,现在,我想从我的网站下载更改日志,然后显示它。我该怎么办?

By the way, I just want to read the file (something like when browsers ask you do you want to save or run this file?, not save it.

顺便说一句,我只是想读取文件(类似于浏览器要求你保存或运行此文件的时候?,而不是保存它。

Thanks.

1 个解决方案

#1


1  

You can open the input stream from the URI in Java as follows. Replace InputStreamReader with other implementations as per your need. Once you have the reader, you should be able to read the data from the stream.

您可以使用Java从URI打开输入流,如下所示。根据需要将InputStreamReader替换为其他实现。一旦有了阅读器,您就应该能够从流中读取数据。

InputStream stream = new URI('http://path-to-file').toURL().openStream();
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
String content = "";
while((content = r.readLine()) != null) {
System.out.println(content); // you can put custom logic here.
}

#1


1  

You can open the input stream from the URI in Java as follows. Replace InputStreamReader with other implementations as per your need. Once you have the reader, you should be able to read the data from the stream.

您可以使用Java从URI打开输入流,如下所示。根据需要将InputStreamReader替换为其他实现。一旦有了阅读器,您就应该能够从流中读取数据。

InputStream stream = new URI('http://path-to-file').toURL().openStream();
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
String content = "";
while((content = r.readLine()) != null) {
System.out.println(content); // you can put custom logic here.
}