From 50674e0cef8770590657f0ea259dda48369df333 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Tue, 21 Mar 2023 07:31:32 -0500 Subject: [PATCH] Work on lab6 --- .../lab6-Exceptions_CalebFontenot/scores.txt | 2 ++ .../ReadFileFromWeb.java | 36 +++++++++++++++++++ .../WriteWithAutoClose.java | 32 +++++++++++++++++ .../WriteWithAutoClose2.java | 34 ++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 Semester 2/Assignments/lab6-Exceptions_CalebFontenot/scores.txt create mode 100644 Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb.java create mode 100644 Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose.java create mode 100644 Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose2.java diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/scores.txt b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/scores.txt new file mode 100644 index 0000000..a225378 --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/scores.txt @@ -0,0 +1,2 @@ +John T Smith 90 +Eric K Jones 85 diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb.java new file mode 100644 index 0000000..9ae6773 --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb.java @@ -0,0 +1,36 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.mycompany.lab6.exceptions_calebfontenot; + +import java.util.Scanner; + +/** + * + * @author caleb + */ +public class ReadFileFromWeb { + public static void main(String[] args) { + 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; + String text =""; + Scanner input = new Scanner(url.openStream()); + while (input.hasNext()) { + String line = input.nextLine(); + count += line.length(); + text += line; + } + System.out.println("The file size is " + count + " characters."); + System.out.println(text); + } catch (java.net.MalformedURLException ex) { + System.out.println("Invalid URL"); + } + catch (java.io.IOException ex) { + System.out.println("IO Errors!"); + } + } +} diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose.java new file mode 100644 index 0000000..ad732fc --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose.java @@ -0,0 +1,32 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.mycompany.lab6.exceptions_calebfontenot; + +import java.io.IOException; + +/** + * + * @author caleb + */ +public class WriteWithAutoClose { + public static void main(String[] args) throws IOException + { + java.io.File file = new java.io.File("scores.txt"); + if (file.exists()) { + System.out.println("File already exists!"); + System.exit(0); + } + + //throw new IOException(); //Just for testing :) + + try (java.io.PrintWriter output = new java.io.PrintWriter(file);) { + // Write formatted output to the file + output.print("John T Smith "); + output.println(90); + output.print("Eric K Jones "); + output.println(85); + } + } +} diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose2.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose2.java new file mode 100644 index 0000000..df6b4f7 --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose2.java @@ -0,0 +1,34 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.mycompany.lab6.exceptions_calebfontenot; + +import java.io.IOException; + +/** + * + * @author caleb + */ +public class WriteWithAutoClose2 { + public static void main(String[] args) { + try { + java.io.File file = new java.io.File("scores.txt"); + if (file.exists()) { + System.out.println("File already exists!"); + System.exit(0); + } + //throw new IOException(); //Just for testing :) + + try (java.io.PrintWriter output = new java.io.PrintWriter(file);) { + // Write formatted output to the file + output.print("John T Smith "); + output.println(90); + output.print("Eric K Jones "); + output.println(85); + } + } catch (Exception ex) { + System.err.println("Cought exception: " + ex); + } + } +}