/home/caleb/ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromURL.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.mycompany.lab6.exceptions_calebfontenot;
import java.util.Scanner;
@author
public class ReadFileFromURL {
public static void main(String[] args)
{
boolean continueLoop = false;
boolean exceptionThrown = false;
do {
System.out.print("Enter a URL: ");
String URLString = new Scanner(System.in).next();
try {
java.net.URL url = new java.net.URL(URLString);
int count = 0;
Scanner input = new Scanner(url.openStream());
while (input.hasNext()) {
String line = input.nextLine();
count += line.length();
}
System.out.println("The file size is " + count + " characters");
exceptionThrown = false;
} catch (java.net.MalformedURLException ex) {
System.out.println("Invalid URL");
exceptionThrown = true;
} catch (java.io.IOException ex) {
System.out.println("IO Errors");
}
if (exceptionThrown) {
continueLoop = true;
} else {
continueLoop = false;
}
} while (continueLoop);
}
}