From 16d8bf228004f71aa9435e0e74b52b87bae0c0ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Kvarnstr=C3=B6m?= Date: Sat, 19 Dec 2020 11:35:40 +0100 Subject: Resource folder with example text file and image. ResourceTester class. --- src/se/liu/liuid123/HelloWorld.java | 11 ++++++++ src/se/liu/liuid123/Main.java | 11 -------- src/se/liu/liuid123/ResourceTester.java | 47 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 src/se/liu/liuid123/HelloWorld.java delete mode 100644 src/se/liu/liuid123/Main.java create mode 100644 src/se/liu/liuid123/ResourceTester.java (limited to 'src/se') diff --git a/src/se/liu/liuid123/HelloWorld.java b/src/se/liu/liuid123/HelloWorld.java new file mode 100644 index 0000000..36e2290 --- /dev/null +++ b/src/se/liu/liuid123/HelloWorld.java @@ -0,0 +1,11 @@ +package se.liu.liuid123; + +/** + * A simple test class used to verify that your development environment is working. + */ +public class HelloWorld +{ + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} diff --git a/src/se/liu/liuid123/Main.java b/src/se/liu/liuid123/Main.java deleted file mode 100644 index 9ad934c..0000000 --- a/src/se/liu/liuid123/Main.java +++ /dev/null @@ -1,11 +0,0 @@ -package se.liu.liuid123; - -/** - * A simple test class used to verify that your development environment is working. - */ -public class Main { - - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} diff --git a/src/se/liu/liuid123/ResourceTester.java b/src/se/liu/liuid123/ResourceTester.java new file mode 100644 index 0000000..89bd438 --- /dev/null +++ b/src/se/liu/liuid123/ResourceTester.java @@ -0,0 +1,47 @@ +package se.liu.liuid123; + +import javax.swing.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; + +/** + * A simple test class used to exemplify how resources work. + */ +public class ResourceTester +{ + public static void main(String[] args) { + // Reading a text file from *resources* is a bit cumbersome. + // We don't know if the file is stored directly in the file system + // or inside an *archive* (JAR file), so should access it + // as a "resource" identified by an URL. + final URL readme = ClassLoader.getSystemResource("README.md"); + + // Then we can use this URL to open an *input stream*, + // create an InputStreamReader that converts bytes to characters + // according to the default character encoding (typically UTF-8), + // and then createa a BufferedReader which can be used to read lines. + // + // All of this is insiude a "try" statement that ensures the streams + // and readers are closed when we are done. + try (final BufferedReader reader = new BufferedReader(new InputStreamReader(readme.openStream()))) { + System.out.println("Contents of the file:"); + + // Read and print strings until you get null + String str = reader.readLine(); + while (str != null) { + System.out.println(str); + str = reader.readLine(); + } + } catch (IOException e) { + // Needs to be handled somehow... + e.printStackTrace(); + } + + final URL image = ClassLoader.getSystemResource("images/hello_world.png"); + + ImageIcon icon = new ImageIcon(image); + System.out.println("Read an image with width " + icon.getIconWidth() + " and height " + icon.getIconHeight()); + } +} -- cgit v1.2.1