/** all files should be added to src/main/resources folder for them to be packaged at root */ public static void Do() throws IOException { Scanner s = null; Main obj = new Main(); try { s = new Scanner( new BufferedReader( new InputStreamReader( // can not use this as it is static method // it is relative resources folder which after build is at root obj.getClass().getResourceAsStream("/xanadu.txt")))); while (s.hasNext()) { System.out.println(s.next()); /** * this breaks the line into words, using a different token separator, invoke * useDelimiter(),s.useDelimiter(",\\s*"); * * <p>In Xanadu did Kubla Khan A stately pleasure-dome */ } } catch (Exception e) { System.out.println(e.getMessage()); // logs an exception thrown from somewhere logger.error("This is error", e); } finally { if (s != null) { s.close(); } } }
public static void DoProperties() throws IOException { Main obj = new Main(); Properties props = new Properties(); InputStream resourceStream = null; try { resourceStream = obj.getClass().getResourceAsStream("/myproperties.properties"); props.load(resourceStream); System.out.println(props.getProperty("TestKey")); } catch (Exception e) { System.out.println(e.getMessage()); // logs an exception thrown from somewhere logger.error("This is error", e); } finally { if (resourceStream != null) { resourceStream.close(); } } }