The acronym URL stands for Uniform Resource Locator. A URL essentialy points at a resource stored somewhere on the World Wide Web. This resource could be an ordinary file, and this is what it will be for our projects. However, it could also be a directory, an executable program, a database, or some other type of resource. To use the resource, you would have to know what kind of resource it is and then try to use it appropriately.
Yes. The Java API contains a lot of powerful tools, and the URL class is one of them. You can use it to create a connection between your Java program and a resource stored somewhere on the internet. The program below shows an example of how this can be done. The example is fully explained in the paragraphs following the example.
import java.io.*; import java.net.*; import javax.swing.*; public class helloURL { public static void main(String[] x)throws IOException { URL url = null; String s = JOptionPane.showInputDialog("Enter the URL name", "http://thinkdeeply.net/java/mynameis.txt"); boolean gotURL = false; while (! gotURL) { try { url=new URL(s); gotURL = true; } catch (MalformedURLException e) { s=JOptionPane.showInputDialog(null, "Try again - reenter the URL", "Bad URL - Try Again", JOptionPane.ERROR_MESSAGE); } } InputStream instream=url.openStream(); BufferedReader in=new BufferedReader(new InputStreamReader(instream)); String name = in.readLine(); JOptionPane.showMessageDialog(null, "Hello " + name); return; } }
A properly formed URL will contain the name of a protocol (http in the example above), an internet node address (thinkdeeply.net in the example above), a path name (/java in the example above) and a file name (mynameis.txt in the example above). A properly formed URL can be passed as a parameter to the constructor of the URL class, creating an object of this type. A bad URL can cause an exception to be thrown. Note the use of try and catch blocks in the example above. The catch block tries to recover from the error by forcing the interactive user to retry the inputting the name of the web resource.
Once a good URL is inputted, a URL object is created successfully and its openStream() method is invoked. This creates an input stream whose source is the web resource identified by the URL and whose destination is this Java program. It can now be used just like any other input stream you have worked with previously.
Instead of reading byte-by-byte from the input stream, the example above attaches a buffered reader to the input stream, remembering to use an InputStreamReader object as an intermediary (because it knows the resource is a text file … if it were some other type of resource, different tools would have to be used).
Finally, the example above uses the readLine() method to get something from that text file located somewhere on the internet. It shows you what it found by displaying a message on the console.
Yes. If you know the name of the resource, you can create a connection to it exactly as done in the example above. Once you invoke the openStream() method to create a new InputStream in your program, you can deal with it just as you would deal with a local file of the same type. If it is a binary file, attach a DataInputStream to the input stream. If its an object file, attach an ObjectInputStream to the input stream.
Do this the same way you did it for local files.
If you are using an ObjectInputStream, the readObject() method will throw an EOFException when the end of file condition is detected.
The input methods of a DataInputStream object will also throw an EOFException when end of file is encountered.
If you are accessing a text file and are using the read() method of an InputStreamReader or the readLine() method of a BufferedReader, end of file is signalled by the method's return value rather than by the throwing of an exception.
When end of file is signalled, it is customary to close the stream and continue the execution of your program.
| Things to try |
|---|
|
© Wiggen & Associates, 2007