GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - InputStreamReader Class
Selected Reading
Java - InputStreamReader ClassIntroductionThe Java InputStreamReader class is a bridge from byte streams to character streams.It reads bytes and decodes them into characters using a specified charset. Class declarationFollowing is the declaration for Java.io.InputStreamReader class − public class InputStreamReader extends Reader FieldFollowing are the fields for Java.io.InputStreamReader class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Using close() with InputStreamReader (Automatic Closure with Try-With-Resources)The following example shows the usage of Java InputStreamReader close() method. InputStreamReaderDemo.javapackage com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
int data;
while ((data = reader.read()) != -1) { // Read character by character
System.out.print((char) data);
}
// No need to manually close, try-with-resources handles it
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "JavaProgramming")Let us compile and run the above program, this will produce the following result− JavaProgramming Explanation
Example - Getting Default Encoding of InputStreamReaderThe following example shows the usage of Java InputStreamReader getEncoding() method. InputStreamReaderDemo.javapackage com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
System.out.println("Encoding used: " + reader.getEncoding());
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− Encoding used: UTF8 Explanation
Example - Reading One Character at a TimeThe following example shows the usage of Java InputStreamReader read() method. InputStreamReaderDemo.javapackage com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
int data;
while ((data = reader.read()) != -1) { // Read character by character
System.out.print((char) data); // Convert integer to character and print
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "Hello")Let us compile and run the above program, this will produce the following result− Hello Explanation
Advertisements
| ||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |