81 lines
5.3 KiB
HTML
81 lines
5.3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>ReadFileFromWeb1.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
|
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
|
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb1.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.mycompany.lab6.exceptions_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> ReadFileFromWeb1 {
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) {
|
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter a URL (defaults to </span><span class="literal">\"</span><span class="ST0">https://calebfontenot.com</span><span class="ST0">\</span><span class="literal">"</span><span class="string">): </span><span class="string">"</span>);
|
|
String URLinput = <span class="literal">new</span> Scanner(System.<span class="ST3">in</span>).nextLine();
|
|
String URLString;
|
|
<span class="literal">if</span> (URLinput.isEmpty()) {
|
|
URLString = <span class="string">"</span><span class="ST0">https://calebfontenot.com</span><span class="string">"</span>;
|
|
}
|
|
<span class="literal">else</span> {
|
|
URLString = URLinput;
|
|
}
|
|
<span class="literal">try</span> {
|
|
java.net.URL url = <span class="literal">new</span> java.net.URL(URLString);
|
|
<span class="literal">int</span> count = <span class="number">0</span>, divCount = <span class="number">0</span>, pCount = <span class="number">0</span>;
|
|
String text =<span class="string">""</span>;
|
|
Scanner input = <span class="literal">new</span> Scanner(url.openStream());
|
|
<span class="literal">while</span> (input.hasNext()) {
|
|
String line = input.nextLine();
|
|
count += line.length();
|
|
text += line;
|
|
<span class="literal">if</span> (line.contains(<span class="string">"</span><span class="string"><div</span><span class="string">"</span>)) {
|
|
divCount++;
|
|
}
|
|
<span class="literal">if</span> (line.contains(<span class="string">"</span><span class="string"><p</span><span class="string">"</span>)) {
|
|
pCount++;
|
|
}
|
|
|
|
}
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">The file size is </span><span class="string">"</span> + count + <span class="string">"</span><span class="string"> characters.</span><span class="string">"</span>);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">There are </span><span class="string">"</span> + divCount + <span class="string">"</span><span class="string"> <div> tags on this site.</span><span class="string">"</span>);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">There are </span><span class="string">"</span> + pCount + <span class="string">"</span><span class="string"> <p> tags on this site.</span><span class="string">"</span>);
|
|
System.<span class="ST3">out</span>.println(text);
|
|
} <span class="literal">catch</span> (java.net.MalformedURLException ex) {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Invalid URL</span><span class="string">"</span>);
|
|
}
|
|
<span class="literal">catch</span> (java.io.IOException ex) {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">IO Errors!</span><span class="string">"</span>);
|
|
}
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|