I have a large text file having some content written in Hindi and german language. I want to convert every single special character to UTF-8 encoding. (line by line)
I was using this code but it is giving me an error as :
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3332) at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:566) at java.lang.StringBuilder.append(StringBuilder.java:181) at ConvertUTF.main(ConvertUTF.java:47)
This is the code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.InputStream;
public class ConvertUTF {
public static void main(String[] args){
try {
InputStream is = null;
is = new FileInputStream("file.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(is, "UTF8"));
int str;
char[] cbuf = new char[is.available()];
int i=1;
StringBuilder sb1 = new
StringBuilder("");
while ((str = (in.read(cbuf,0,8))) != 0 && i<7) {
sb1.append(cbuf);
}
System.out.print(sb1);
in.close();
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}