File to read: src/main/resources/examples/request.xml
|
1 2 3 4 |
<employee> <name>Peter Smith</name> <age>25</age> </employee> |
1. Using Java API without any libraries
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.mytutorial; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class ReadTextFileFromClasspath { public static void main(String[] args) throws IOException { InputStream is = ReadTextFileFromClasspath.class.getResourceAsStream("/examples/request.xml"); InputStreamReader isr = new InputStreamReader(is); StringBuilder sb=new StringBuilder(); BufferedReader br = new BufferedReader(isr); String read = br.readLine(); while(read != null) { sb.append(read); read =br.readLine(); sb.append("\n"); } System.out.println(sb.toString()); } } |
You can also use the “ClassLoader”
|
1 2 |
ClassLoader loader = Thread.currentThread().getContextClassLoader(); InputStream is = loader.getResourceAsStream("examples/request.xml"); |
2. Using Java 7 NIO
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.mytutorial; import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class ReadTextFileFromClasspath { public static void main(String[] args) throws IOException, URISyntaxException { List<String> readLines = Files.readAllLines( Paths.get(ReadTextFileFromClasspath.class.getResource("/examples/request.xml").toURI()), Charset.defaultCharset()); System.out.println(readLines); } } |
3. Using Apache commons-io library
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.mytutorial; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; public class ReadTextFileFromClasspath { public static void main(String[] args) throws IOException { InputStream is = ReadTextFileFromClasspath.class.getResourceAsStream("/examples/request.xml"); String str = IOUtils.toString(is, "UTF-8"); System.out.println(str); } } |
4. Using Spring library
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.mytutorial; import java.io.IOException; import java.net.URL; import org.apache.commons.io.IOUtils; import org.springframework.util.ResourceUtils; public class ReadTextFileFromClasspath { public static void main(String[] args) throws IOException { final URL url = ResourceUtils.getURL("classpath:examples/request.xml"); String str = IOUtils.toString(url); System.out.println(str); } } |
5. Using Java 8 API
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.mytutorial; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadTextFileFromClasspath { public static void main(String[] args) throws IOException { Stream<String> stream = Files.lines(Paths.get("/examples/request.xml"),Charset.defaultCharset()); stream.forEach(System.out::println); } } |
