This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLWritter.java
66 lines (56 loc) · 1.77 KB
/
URLWritter.java
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
// =============================================================================
// Filename: URLWritter.java ~ November 21, 2019
// =============================================================================
// Jaime Bohorquez
// Created using Atom + iTerm2 on Mac OS
// This program extracts data from websites and writes it to a file.
// =============================================================================
// package vexutilities;
import java.util.*;
import java.io.*;
import java.net.*;
class URLWritter
{
private URLWritter() {}
protected static void writeToFile(String linkToWebPage, String fileName)
{
URL url;
FileWriter fileWriter;
PrintWriter printWriter;
URLConnection con;
InputStream is;
Scanner sc;
try
{
url = new URL(linkToWebPage);
}
catch(MalformedURLException e)
{
System.out.println("Invalid URL: " + linkToWebPage + "\n" + e);
return;
}
try
{
fileWriter = new FileWriter(fileName);
printWriter = new PrintWriter(fileWriter);
// Get the input stream through URL Connection.
con = url.openConnection();
is = con.getInputStream();
}
catch(IOException e)
{
System.out.println(e);
return;
}
sc = new Scanner(new InputStreamReader(is));
String line = null;
// Read each line and write to file.
while (sc.hasNext())
{
line = sc.nextLine();
printWriter.print(line);
}
printWriter.close();
sc.close();
}
}