Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The class library that's a part of the JDK includes a rich set of classes designed for network communications. Among the tasks that they support, they give you the ability to easily retrieve and read files over the web. I'll show you one approach to doing this, using the classes of the Java class library. I'll mention before I get started, that there are easier ways that require less code if you use the Apache Commons Library, but it's good to know what's available in the core class library first. I'm working in a project called NetworkFiles and a class called Read network file and I'm working with an empty main method.
The first step in getting a network file is knowing its location. You should wrap the location in the file in an instance of a class named URL for Uniform Resource Locator. I'll type in the name of the class which is all uppercase and I'll press Ctrl + Spacebar, and choose the version in the package java.net. I'll name that variable url, all lowercase and then instantiate it, using the URL constructor method, I'll start with a pair of quotes. Now the purpose of the URL class is to wrap a string that indicates the location of the file, so now I need the file.
I'll go to Internet Explorer, I'm going to be retrieving a dynamic XML feed from the web. Here is the URL http://services. explorecalifornia.org/RSS/tours.php. When you view this file in a browser, its format will differ from one browser to the next. In the Internet Explorer, it might be formatted the way you see here on the screen or if you're working in Chrome or Firefox, it might have a hierarchical presentation.
But regardless of how it's presented visually the actual format is XML. You can see this in any browser by right clicking and choosing view source or some similar menu choice. That will open the raw format of the retrieved content and you'll see that it's an XML file and you also see that it's in RSS format. RSS is the format that's used for syndicated content. For example, if somebody has a site that's based on WordPress, typically you'll be able to get an RSS feed of their content from that site.
So now that I've confirmed that this URL provides a valid XML feed, I'll copy the URL to the clipboard and return to Eclipse. I'll place the cursor inside double quotes that I use to pass in the value to the URL and I'll paste in the URL string. I'll maximize my editor so we can see all the code. You'll see an error occur over on the left, it's telling you about a potential unhandled exception, we'll deal with that later. The next step is to create an instance of a class named InputStream.
I worked with this class in an earlier video, when I opened a file from the local file system. It's the same data type but this time I am going to create the reference to the object by calling a method of the URL class using URL.openStream. So now I have my connection to the file on the web. I'll place the cursor after InputStream, it looks like I didn't get my import statement and then press Ctrl + Spacebar and I'll make sure that I'm choosing the input string class from Java.I/O and there is the import.
Next I'll create an object which is an instance of BufferedInputStream. I'll type the beginning of the class and then press control space and Eclipse automatically auto completes it and I'll name that object buff for buffered and I'll instantiate it using the class' constructor method new BufferedInputStream and I'll pass in the stream object. The purpose of the BufferedInputStream is to do the same sort of looping that I did in the file, allowing me to read one or more bytes at a time as I loop.
Again, there is an indication of an error over on the left, don't worry about that I'll handle it later. Now we are ready to loop through the content of the file. I'm going to be reading the file this time one byte or one character at a time. In order to collect that data I'll create an object based on the StringBuilder class. I showed you how to use this class in an earlier video. The purpose of StringBuilder is to let you append content or insert content into a string without having to constantly rebuild the string object.
I'll name the StringBuilder object sp and I'll instantiate it with its constructor method. Now I'm ready to loop through the file and read it one byte at a time. I'll create a while loop, I'll type the word while and press Ctrl + Spacebar and choose while loop with condition. And I'll set the condition to the keyword true, that means I'm going to keep on looping until I call the break command and that will cause the flow to jump out of the loop. Each time through the loop, I'm going to read a single character from the stream.
The BufferedInputStream class has a method called read which returns a single byte. It will be returned as an integer initially, so I'll declare a variable named data and declare it as an int and I'll get its value from buf.read(). The rules are that as long as there is an available character to read in the stream the read command will return an integer value representing that character, but if it's gotten to the end of the stream, and the end of the file it will return a value of negative one. So I'll take a look at the value that I just received, I'll use an if command and this will be an 'if' statement and the condition will be data has a value of negative one, if that condition is true that means that I'm at the end of the document and so I'll break out of the while loop.
But otherwise which I'll put into the else block down here I'll append the value that was returned first converting it to a character using the syntax sp.append(data). But before I can pass that data value, I have to convert it to a character otherwise it'll be converted as a numeric. So I'll use casting syntax, I'll place the cursor before the data value and put in (Char). That means take the integer convert it to a character and then append it to the string.
I'll place the cursor after the while loop and finally I'll output the value that I've collected using system.out. println and I'll just output the value of the StringBuilder object. You could if you prefer convert the StringBuilder to a string, if you're going to keep it around in memory for a while. Now I've still got these errors up I have to deal with. Each of the errors indicates that there is an unhandled exception that could occur, as with all such conditions you deal with them by wrapping the code inside a TryCatch block.
So I'll select everything that I've coded inside the main method, I'll right click, I'll chose surround with TryCatch block. And Eclipse figures out which exceptions could occur and creates catch blocks for each of them. I will remove the TODO comments and my application should be complete. I'll Save and Run it and there is the result. The XML file is retrieved over the web and displayed in the Console. So again, this is one approach to retrieving files over the web and dealing with their content.
Once you have the content in memory, you could save it to the local file system, you could display it on the screen in whatever sort of application you're working in, or because it's structured XML, you could search for and extract its data. This sort of coding using the class library of the JDK will work in any Java environment, regardless of whether you're working with Console applications, Web applications, Android applications, it's all the same code because it's all Java.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98552 Viewers
61 Video lessons · 85838 Viewers
71 Video lessons · 69736 Viewers
56 Video lessons · 102043 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.