Thursday, September 23, 2010

Read UTF-8 File in JAVA

Reading a UTF-8 file or in other words a file which contains symbols which are not used in normal English, symbols or character of Arabic, Urdu or Persian comes in this category. if you try to read the file in a normal way as you did to read any other file the information in that file will be corrupted so in order to prevent it from corruption java provide a very easy method to do this below is the sample code to read UTF-8 file in java.

Sample Code

class UTF_File_Reader{

public void readFile(){
    try{
        FileInputStream fin=new FileInputStream("PATH_TO_UTF-8_FILE");
        InputStreamReader isr=new InputStreamReader(fin,"UTF-8");
        // you can write other encoding as well
        // like UTF-7, iso-8859-1 etc ...
        BufferedReader br=new BufferedReader(isr);
        String temp="";
       while((temp=br.readLine())!=null){
           //Something Useful here
        }
    }
    catch(Exception ex){
        System.out.println(ex);
    }
}
}

2 comments:

Palli on February 7, 2013 at 1:29 AM said...

Nice and easy solution. great work!

Palli on February 7, 2013 at 1:31 AM said...

Looking forward for more examples.

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems