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:
Nice and easy solution. great work!
Looking forward for more examples.
Post a Comment