Question
For the following question how do I use TextIO file system instead of the usual Scanner file reader In this part of the lab, you
For the following question how do I use TextIO file system instead of the usual Scanner file reader
In this part of the lab, you will write program that fetches the information stored at a give URL on the web and saves that data to a file. This will also include networking and file operations and partly an exercise in using exceptions.
For doing I/O, Java has a pair of nice abstractions:InputStreamandOutputStream. These are abstract classes in the packagejava.io.AnInputStreamis a place from which you can read data; anOutputStreamis a place to which you can write data. For the lab, you will use anInputStreamto represent the data read from the Web URL, and you will use anOutputStreamto represent the file where you want to save a copy of the data. Once you have the streams, the data can be copied just by calling the following method, which you can copy into your program:
private static void copyStream(InputStream in, OutputStream out)
throws IOException {
int oneByte = in.read();
while (oneByte >= 0) { // negative value indicates end-of-stream
out.write(oneByte);
oneByte = in.read();
}
}
Aside from this method, you should have amainroutine that does the following:
- Declare variables to represent the InputStream and the OutputStream. It would be a good idea to initialize them to null to avoid uninitialized variable errors.
- Read the URL and the file name as strings from the user.
- To connect to the web, you need a variable-- say url --of typeURL(from packagejava.io). You can create the URL object with the constructor callurl = new URL(urlString), whereurlStringis the string provided by the user. This constructor will throw aMalformedURLExceptionif the string is not a legal URL. (Note: the string must be a complete URL, beginning with "http://".)
- To get the input stream, you can simply callurl.openStream(), which returns a value of type InputStream. This can throw anIOException, for example, if the web address that you are asking for does not exist.
- To get the output stream, you can use the constructor newFileOutputStream(fileName), wherefileNameis the file name that was input by the user. This can throw aFileNotFoundExceptionif it is not possible to open the specified file for reading (for example, if the user is trying to write a new file in a directory where they don't have write permission).Warning:If a file of the same name already exists, the old file will be erased and replaced by the new one, without giving the user any notice!
- Now, copy the data from the web into the file by calling the above method. Note that this can throw anIOException.
- Finally, use afinallyto clause to make sure that both streams are closed (if they were successfully opened). Both InputStream and OutputStream have aclose()method for closing the stream. Note that you can test whether the stream was opened by testing whether the value of the variable is stillnull.
Note that an exception should not crash your program. You shouldcatchthe exception and print out a reasonable error message before ending the program. It would be nice if the error message depends on the type of error that occurred (which means using severalcatchclauses).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started