- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
众所周知,这两个代码会产生相同的结果
public class MainApp {
public static void main(String[] args) throws IOException {
URL google = new URL("http://www.google.com");
google.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(google.openStream()));
reader.lines().forEach(System.out::println);
}
}
和
public class MainApp {
public static void main(String[] args) throws IOException {
URL google = new URL("http://www.google.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(google.openStream()));
reader.lines().forEach(System.out::println);
}
}
那么使用 google.openConnection() 有什么意义呢?
最佳答案
可能是此方法的 javadoc 帮助:
public java.net.URLConnection openConnection() throws java.io.IOException
Returns a
URLConnection
instance that represents a connection to the remote object referred to by theURL
. A new instance ofURLConnection
is created every time when invoking theURLStreamHandler.openConnection(URL)
method of the protocol handler for this URL.It should be noted that a
URLConnection
instance does not establish the actual network connection on creation. This will happen only when callingURLConnection.connect()
.If for the
URL
's protocol (such as HTTP or JAR), there exists a public, specializedURLConnection
subclass belonging to one of the following packages or one of their subpackages:java.lang
,java.io
,java.util
,java.net
, the connection returned will be of that subclass. For example, for HTTP anHttpURLConnection
will be returned, and for JAR aJarURLConnection
will be returned.
如果您想为您的连接添加一些特定的连接属性,请使用它。
例如:
URLConnection urlConnection = google.openConnection();
urlConnection.setReadTimeout(1000);
urlConnection.setConnectTimeout(1000);
关于java - 真的有必要使用 url.openConnection() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38655323/
我正在使用 this solution在二进制矩阵中找到与图像边界对齐的矩形。假设现在我想找到一个不与图像边框对齐的矩形,并且我不知道它的方向;找到它的最快方法是什么? 为了示例,让我们寻找一个仅包含
else: 行在这个 Python 程序中是否正确/必要? from random import randrange for n in range(10): r = randrange(0,1
在 TDPL 7.1.5.1 中讨论了将 Widget w2 分配给 w1 并且作者指出“将 w2 逐个字段分配给 w1 会将 w2.array 分配给 w1.array——一个简单的数组边界分配,而
我是一名优秀的程序员,十分优秀!