如何在 Java 中存储 URL 的内容?
URL 类别的 openStream()方法打开一个连接并返回一个 InputStream。通过使用这个流,您可以从该连接读取内容。
示例
import java.io.InputStream; import java.net.URL; import java.util.Scanner; public class ReadingURL { public static void main(String args[]) throws Exception { URL url = new URL("https://tutorialspoint.com/"); InputStream inputStream = url.openStream(); Scanner s = new Scanner(inputStream); while(s.hasNext()) { System.out.println(s.nextLine()); } } }
输出
<li><a href="/couchdb/index.htm" title="Learn CouchDB">Learn CouchDB</a></li> <li><a href="/db2/index.htm" title="Learn DB2">Learn DB2</a></li> <li><a href="/documentdb_sql/index.htm" title="Learn DocumentDB SQL"> Learn DocumentDB SQL</a></li> <li><a href="/documentdb/index.htm" title="Learn DocumentDB">Learn DocumentDB</a></li> <li><a href="/dynamodb/index.htm" title="Learn DynamoDB">Learn DynamoDB</a></li> <li><a href="/h2_database/index.htm" title="Learn H2 Database"> Learn H2 Database</a></li> <li><a href="/hsqldb/index.htm" title="Learn HSQLDB">Learn HSQLDB</a></li> <li><a href="/ims_db/index.htm" title="Learn IMS DB">Learn IMS DB</a></li> <li>…………………………………………………………………………………………. ………………………………………………………………………………………………… ………………………………………………………………………………………………
广告