I'm still pretty new to java and I'm trying to learn how to open url's through script. From what I understand, two of the ways to do this are:
1. Directly use URL.getContent()
2. Creating a URLConnection object, then using it to open the connection and subsequently, an input stream from which you can read the information.
The question is, in what scenario would the latter be better than the prior? My first impressions are that method 1 would obviously be much faster (I'm guessing it's the equivalent of a GET command when we attempt to retrieve content via the browser.
Also, if I were to open a connection object to say, www.google.com, is there a way for me to open input streams based on extra url information:
Example www.google.com/herp/derp/hello.html, where the string in bold is dynamic,
without having to close the existing connection object? If so, how is this done?
+Will I experience memory leaks if I don't properly destroy objects that are no longer used? Or does Java handle that automatically?
I cant help you with your URL problem since I never used that API, however...
Quote:
+Will I experience memory leaks if I don't properly destroy objects that are no longer used? Or does Java handle that automatically?
You dont need to destroy objects in java, since java has a garbage collector. Java automatically destroys all objects that dont have any references pointing on them left. However, you should always close streams since otherwise they will build up. (Using the .close() method)
I'm still pretty new to java and I'm trying to learn how to open url's through script. From what I understand, two of the ways to do this are:
The question is, in what scenario would the latter be better than the prior? My first impressions are that method 1 would obviously be much faster (I'm guessing it's the equivalent of a GET command when we attempt to retrieve content via the browser.
Also, if I were to open a connection object to say, www.google.com, is there a way for me to open input streams based on extra url information:
Example www.google.com/herp/derp/hello.html, where the string in bold is dynamic,
without having to close the existing connection object? If so, how is this done?
+Will I experience memory leaks if I don't properly destroy objects that are no longer used? Or does Java handle that automatically?
I cant help you with your URL problem since I never used that API, however...
You dont need to destroy objects in java, since java has a garbage collector. Java automatically destroys all objects that dont have any references pointing on them left. However, you should always close streams since otherwise they will build up. (Using the .close() method)
@Mille25: Go
Thanks for the tip. For clarity, Each stream is one connection between the target url and my terminal right?