Runtime method getLocalizedInputStream() in Java
A Runtime method getLocaizedInputStream() used to create a localized version of an input stream. It takes an InputStream and returns an InputStream equivalent to the argument in all respects except that it is localized: as characters in the local character set are read from the stream, they are automatically converted from the local character to Unicode.
Syntax
 1 public InputStream getLocalizedInputStream(InputStream in)
in : It is an object of InputStream that includes the detail of the file to be read.
return : It returns an object of InputStream to read specified file content.
example.txt
 1 This is content of file.
Runtime method getLocalizedInputStream()
 1 package com.java;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 
 8 public class Main {
 9 
 10     public static void main(String[] args) throws IOException {
 11         Runtime runTime = Runtime.getRuntime();
 12         InputStream fileInputStream = null;
 13         
 14         try {
 15             fileInputStream = new FileInputStream("C:/examples/example.txt");
 16             InputStream is = runTime.getLocalizedInputStream(fileInputStream);
 17             // read characters one by one from stream
 18             System.out.println("Char : " + (char) is.read());
 19             System.out.println("Char : " + (char) is.read());
 20             System.out.println("Char : " + (char) is.read());
 21         } catch (FileNotFoundException e) {
 22             System.out.println("Exception : " + e.getMessage());
 23         }
 24     }
 25 }
In the above example, a Runtime object is created by calling a static method getRuntime(). A getLocalizedInputStream() method of Runtime class is called by passing a FileInputStream object which was created by passing a path of file. We are reading first 3 characters of file using InputStream class read() method that prints the contents of file.
Output
 1 Char : T
 2 Char : h
 3 Char : i

Read content of file

Read content of file
 1 package com.java;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) {
 10         Runtime runTime = Runtime.getRuntime();
 11 
 12         InputStream fileInputStream = null;
 13         try {
 14             fileInputStream = new FileInputStream("C:/examples/example.txt");
 15             InputStream is = runTime.getLocalizedInputStream(fileInputStream);
 16             int character;
 17             while ((character = is.read()) != -1) {
 18                 System.out.print((char)character);
 19             }
 20             fileInputStream.close(); // use try with resource to avoid this line
 21         } catch (IOException e) {
 22             e.printStackTrace();
 23         }
 24     }
 25 }
In the above example, an instance of InputStream object is created and we are reading a character until a read() method returns an integer value -1, the content of file will be printed.
Output
 1 This is content of file.
Note This method is deprecated from Java 11, the preferred way to translate a byte stream in the local encoding into a character stream in Unicode is via the InputStreamReader and BufferedReader classes.
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us