Rachelle 6 years ago
parent
commit
786cc2fbde
3 changed files with 78 additions and 0 deletions
  1. 1
    0
      .idea/misc.xml
  2. 77
    0
      src/main/java/Main.java
  3. BIN
      target/classes/Main.class

+ 1
- 0
.idea/misc.xml View File

@@ -7,4 +7,5 @@
7 7
       </list>
8 8
     </option>
9 9
   </component>
10
+  <component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
10 11
 </project>

+ 77
- 0
src/main/java/Main.java View File

@@ -0,0 +1,77 @@
1
+import java.util.Scanner;
2
+import java.net.URL;
3
+import java.net.URLConnection;
4
+import java.io.BufferedReader;
5
+import java.io.IOException;
6
+import java.io.InputStream;
7
+import java.io.InputStreamReader;
8
+
9
+public class Main{
10
+
11
+public static void main(String[] args) {
12
+    String url;    // The url from the command line or from user input.
13
+    String urlLC;  // The url transformed to lower case.
14
+    if (args.length == 0) {
15
+        Scanner stdin = new Scanner(System.in);
16
+        System.out.print("Enter a url: ");
17
+        url = stdin.nextLine();
18
+    } else {
19
+        url = args[0];
20
+    }
21
+    urlLC = url.toLowerCase();
22
+    if (!(urlLC.startsWith("http://") || urlLC.startsWith("ftp://") ||
23
+            urlLC.startsWith("file://"))) {
24
+        System.out.println("Using: " + url);
25
+    }
26
+    System.out.println();
27
+    try {
28
+        readTextFromURL(url);
29
+    } catch (IOException e) {
30
+        System.out.println("\n*** Sorry, an error has occurred ***\n");
31
+        System.out.println(e);
32
+        System.out.println();
33
+    }
34
+}
35
+
36
+    static void readTextFromURL( String urlString ) throws IOException {
37
+
38
+        /* Open a connection to the URL, and get an input stream
39
+           for reading data from the URL. */
40
+
41
+        URL url = new URL(urlString);
42
+        URLConnection connection = url.openConnection();
43
+        InputStream urlData = connection.getInputStream();
44
+
45
+        /* Check that the content is some type of text.  Note: If
46
+           getContentType() method were called before getting the input
47
+           stream, it is possible for contentType to be null only because
48
+           no connection can be made.  The getInputStream() method will
49
+           throw an error if no connection can be made. */
50
+
51
+        String contentType = connection.getContentType();
52
+        System.out.println("Stream opened with content type: " + contentType);
53
+        System.out.println();
54
+        if (contentType == null || contentType.startsWith("text") == false)
55
+            throw new IOException("URL does not seem to refer to a text file.");
56
+        System.out.println("Fetching context from " + urlString + " ...");
57
+        System.out.println();
58
+
59
+        /* Copy lines of text from the input stream to the screen, until
60
+           end-of-file is encountered  (or an error occurs). */
61
+
62
+        BufferedReader in;  // For reading from the connection's input stream.
63
+        in = new BufferedReader( new InputStreamReader(urlData) );
64
+
65
+        while (true) {
66
+            String line = in.readLine();
67
+            if (line == null)
68
+                break;
69
+            System.out.println(line);
70
+        }
71
+        in.close();
72
+
73
+    } // end readTextFromURL()
74
+
75
+} // end class FetchURL
76
+
77
+

BIN
target/classes/Main.class View File