This post demonstrates how to convert a file to InputStreamReader.
[wp_ad_camp_1]
From FileInputStream
From FileInputStream
, we need to convert it to InputStreamReader
by pass the object to its constructor.
1 2 | InputStream is = new FileInputStream("c:/Users/user11/Desktop/sample/myxml.xml"); InputStreamReader isr = new InputStreamReader(is); |
You can even create an InputStreamReader
object that uses a named charset, e.g., “UTF-8”.
[wp_ad_camp_2]
1 | InputStreamReader isr = new InputStreamReader(is, "UTF-8); |
Using ClassLoader
1 | new InputStreamReader(ClassLoader.getSystemResourceAsStream("myxml.xml")); |
The file has to be in the classpath. You may test it more easily with Maven by putting the file in src/main/resource
directory.
From FileReader
1 | InputStreamReader isr = new FileReader(c:/Users/user11/Desktop/sample/myxml.xml"); |
Yes, you can directly create InputStreamReader
object using FileReader.
This is the straight-forward solution.
[wp_ad_camp_3]