Advertisement
Gleemingknight

Untitled

Mar 23rd, 2021 (edited)
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.HttpURLConnection;
  3. import java.net.URL;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * A simple program that finds information from a website
  8.  * of your choosing and saves it to a file of your choosing
  9.  */
  10. public class PaulIsDumb {
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         // Ask and Receive URL input
  15.         System.out.println("Please input a URL:");
  16.         String url = scanner.nextLine();
  17.  
  18.         // Detect if valid url
  19.         if(!url.startsWith("http") || !url.contains("//")) {
  20.             System.out.println("Invalid URL.");
  21.             return;
  22.         }
  23.  
  24.         // Ask and Receive file name
  25.         System.out.println("Please input a file name:");
  26.         String fileName = scanner.nextLine();
  27.  
  28.         // Retrieve information from the website
  29.         String retrieved;
  30.         try { retrieved = makeGETRequest(url);
  31.         } catch(IOException ex) {
  32.             System.out.println("There was an error whilst attempting to complete your request.");
  33.             return;
  34.         }
  35.  
  36.         // Write to file
  37.         try { writeToFile(new File(fileName), retrieved);
  38.         } catch(IOException ex) {
  39.             System.out.println("There was an error whilst attempting to write to the file.");
  40.             return;
  41.         }
  42.  
  43.         // Tell user the operation has concluded
  44.         System.out.println("Successfully written to file.");
  45.     }
  46.  
  47.     /**
  48.      * Writes a string to a file
  49.      *
  50.      * @param file File to write to
  51.      * @param meta Information to write
  52.      */
  53.     private static void writeToFile(File file, String meta) throws IOException {
  54.         FileWriter writer = new FileWriter(file);
  55.         writer.write(meta);
  56.         writer.close();
  57.     }
  58.  
  59.     /**
  60.      * Makes a GET request to a specific URL
  61.      *
  62.      * @param url Website Address
  63.      * @return Response
  64.      */
  65.     private static String makeGETRequest(String url) throws IOException {
  66.         URL obj = new URL(url);
  67.  
  68.         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  69.         con.setRequestProperty("User-Agent", "Mozilla/5.0");
  70.  
  71.         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  72.         String inputLine;
  73.         StringBuilder response = new StringBuilder();
  74.         while ((inputLine = in.readLine()) != null) response.append(inputLine).append("\n");
  75.  
  76.         in.close();
  77.         return response.toString();
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement