Different ways of loading a file as an InputStream

What’s the difference between: InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName) and InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) and InputStream is = this.getClass().getResourceAsStream(fileName) When are each one more appropriate to use than the others? The file that I want to read is in the classpath as my class that reads the file. My class and the file are in the … Read more

How do I convert a String to an InputStream in Java?

Given a string: String exampleString = “example”; How do I convert it to an InputStream? 5 s 5 Like this: InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8)); Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8. For versions of Java less than 7, … Read more