From 0ae80a905242d9bc4ee12950cc5bc9c8f75464fc Mon Sep 17 00:00:00 2001 From: Hammy Date: Fri, 25 Dec 2020 04:41:35 +0000 Subject: [PATCH] Implement Try-With-Resources --- .../resources/file1.txt | 1 + .../resources/file2.txt | 1 + .../src/trying/with/resources/Helper.java | 21 +++++ .../src/trying/with/resources/Main.java | 77 +++++++++++++++++++ .../with/resources/MyAutoCloseable.java | 18 +++++ 5 files changed, 118 insertions(+) create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/resources/file1.txt create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/resources/file2.txt create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/Helper.java create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/Main.java create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/MyAutoCloseable.java diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/resources/file1.txt b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/resources/file1.txt new file mode 100644 index 0000000..a6860d9 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/resources/file1.txt @@ -0,0 +1 @@ +ABCDEFGHIJKLMNOPQRSTUVWXYZ \ No newline at end of file diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/resources/file2.txt b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/resources/file2.txt new file mode 100644 index 0000000..a6860d9 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/resources/file2.txt @@ -0,0 +1 @@ +ABCDEFGHIJKLMNOPQRSTUVWXYZ \ No newline at end of file diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/Helper.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/Helper.java new file mode 100644 index 0000000..11a54e1 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/Helper.java @@ -0,0 +1,21 @@ +package trying.with.resources; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Paths; + +/** + * Created by Jim on 1/9/2016. + */ +public class Helper { + static public Reader openReader(String fileName) throws IOException { + return Files.newBufferedReader(Paths.get(fileName)); + } + + static public Writer openWriter(String fileName) throws IOException { + return Files.newBufferedWriter(Paths.get(fileName)); + } + +} diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/Main.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/Main.java new file mode 100644 index 0000000..5bc9130 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/Main.java @@ -0,0 +1,77 @@ +package trying.with.resources; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.util.Arrays; + +public class Main { + private static final String filename = "C:\\Users\\sgoud\\JavaProjects\\Learning-Java\\Pluralsite\\src\\intermediate\\Java Fundamentals - The Core Platform\\resources\\"; + + public static void main(String[] args) { + // doTryCatchFinally(); + // doTryWithResources(); + // doTryWithResourcesMulti(); + doCloseThing(); + } + + public static void doTryCatchFinally() { + char[] buff = new char[8]; + int length; + Reader reader = null; + try { + reader = Helper.openReader("C:\\Users\\sgoud\\JavaProjects\\Learning-Java\\Pluralsite\\src\\intermediate\\Java Fundamentals - The Core Platform\\resources\\file1.txt"); + while((length = reader.read(buff)) >= 0) { + System.out.println("\nlength: " + length); + for(int i=0; i < length; i++) + System.out.print(buff[i]); + } + } catch(IOException e) { + System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage()); + } finally { + try { + if (reader != null) + reader.close(); + } catch(IOException e2) { + System.out.println(e2.getClass().getSimpleName() + " - " + e2.getMessage()); + } + } + } + + public static void doTryWithResources() { + char[] buff = new char[8]; + int length; + try (Reader reader = Helper.openReader(filename + "file1.txt")) { + while((length = reader.read(buff)) >= 0) { + System.out.println("\nlength: " + length); + for(int i=0; i < length; i++) + System.out.print(buff[i]); + } + } catch(IOException e) { + System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage()); + } + } + + public static void doTryWithResourcesMulti() { + char[] buff = new char[8]; + int length; + try (Reader reader = Helper.openReader(filename + "file1.txt"); Writer writer = Helper.openWriter(filename + "file2.txt")){ + while((length = reader.read(buff)) >= 0) { + System.out.println("\nlength: " + length); + writer.write(buff, 0, length); + } + } catch(IOException e) { + System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage()); + } + } + + private static void doCloseThing() { + try(MyAutoCloseable ac = new MyAutoCloseable()) { + ac.saySomething(); + } catch (IOException e) { + System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage()); + Arrays.stream(e.getSuppressed()).forEach(System.out::println); + } + } + +} diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/MyAutoCloseable.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/MyAutoCloseable.java new file mode 100644 index 0000000..c2f7c82 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/trying/with/resources/MyAutoCloseable.java @@ -0,0 +1,18 @@ +package trying.with.resources; + +import java.io.IOException; + +/** + * Created by Jim on 1/9/2016. + */ +public class MyAutoCloseable implements AutoCloseable { + public void saySomething() throws IOException{ + throw new IOException("Exception Thrown 1"); +// System.out.println("Something"); + } + @Override + public void close() throws IOException { + throw new IOException("Exception Thrown 2"); +// System.out.println("close"); + } +}