Friday, September 24, 2010

Write UTF-8 File in JAVA

Writing 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 write the file in a normal way as you did to write any other file the information in that file will be corrupted and you get something like "????" in place of your UTF-8 chars, 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

public void Writer(String fileName,String UTF_8_Data,boolean appendOrNot){
    try{
        FileOutputStream fout=new FileOutputStream(fileName,appendOrNot);
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fout,"UTF8"));
        bw.write(UTF_8_Data,0,UTF_8_Data.length());
        bw.flush();
    }
    catch(Exception ex){
        System.out.println(ex);
    }
}

0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems