CS1103 Programming Assignment Unit 1 - Fetch the information stored at a given URL on the web, save that data to a file, and handle the exceptions

Questions

This program will fetch the information stored at a given URL on the web and save that data to a file. Besides, this will also include networking and file operations and partly an exercise in using exceptions.

1. Codes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

/**
* ExceptionHandling fetches the information stored at a given 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
*
* @author Zhiyao He
*/
public class ExceptionHandling {

/**
* copy the data from the web into the file
*
* @param in a formal parameter of type InputStream
* @param out a formal parameter of type OutputStream
* @throws IOException
*/
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();
}
}

/**
* Read the URL and the file name as strings from the user.
* Get the input stream and the output stream.
* Copy the data from the web into the file.
* Handle the exceptions with try ... catch... blocks.
*
* @param args
*/
public static void main(String[] args) throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;

String urlString;
String fileName;

// Read the URL and the file name as strings from the user.
// Surrounded by a try-with-resources block,
// the in.close() method is automatically called when an exception occurs.
try (Scanner in = new Scanner(System.in)) {
System.out.print("Please input the URL: ");
urlString = in.nextLine();

System.out.print("Please input the fileName: ");
fileName = in.nextLine();
System.out.print("Writing...");
}

try {
// create the URL object
URL url = new URL(urlString);

// get the input stream
inputStream = url.openStream();

// get the output stream
outputStream = new FileOutputStream(fileName);

// copy the data from the web into the file
copyStream(inputStream, outputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) inputStream.close();
if (outputStream != null) outputStream.close();
}
}
}

2. Outputs:

Console output:

1
2
3
4
Please input the URL: https://math.hws.edu/eck/cs124/javanotes8/source/chapter11/ReverseFileWithResources.java
Please input the fileName: file
Writing...
Process finished with exit code 0

Output file (The information stored on the web at the above URL):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
* Reads numbers from a file named data.dat and writes them to a file
* named result.dat in reverse order. The input file should contain
* exactly one real number per line.
* This version of the ReverseFile program uses try-with-resource
* statements to safely open and close the Scanner that is used to
* read the data and the PrintWriter that is used to
* write the data.
*/
public class ReverseFileWithResources {

public static void main(String[] args) {

ArrayList<Double> numbers; // An ArrayList for holding the data.

numbers = new ArrayList<Double>();

// Read the data from the input file.

try( Scanner data = new Scanner(new File("data.dat")) ) {
// Read numbers, adding them to the ArrayList.
while ( data.hasNextDouble() ) { // Read until end-of-file.
double inputNumber = data.nextDouble();
numbers.add( inputNumber );
}
}
catch (FileNotFoundException e) {
// Can be caused if file does not exist or can't be read.
System.out.println("Can't open input file data.dat!");
System.out.println("Error: " + e);
return; // Return from main(), since an error has occurred.
}

// Write the data to the output file.

try( PrintWriter result = new PrintWriter("result.dat") ) {
// Output the numbers in reverse order.
for (int i = numbers.size()-1; i >= 0; i--)
result.println(numbers.get(i));
result.flush(); // Make sure data is actually sent to the file.
if (result.checkError())
System.out.println("Some error occurred while writing the file.");
else
System.out.println("Done!");
}
catch (FileNotFoundException e) {
// Can only be caused by the PrintWriter constructor
System.out.println("Can't open file result.dat!");
System.out.println("Error: " + e);
}

} // end of main()

} // end class ReverseFileWithTextReader

​


This is the ending, thanks for reading.