Ahmad Aboud
Ahmad Aboud

Reputation: 35

Reading Arabic chars from text file

I had finished a project in which I read from a text file written with notepad. The characters in my text file are in Arabic language,and the file encoding type is UTF-8. When launching my project inside Netbeans(7.0.1) everything seemed to be ok,but when I built the project as a (.jar) file the characters where displayed in this way: ÇáãæÇÞÚááÊØæíÑ. How could I solve This problem please?

Upvotes: 3

Views: 7085

Answers (2)

Pshemo
Pshemo

Reputation: 124225

Maybe this example will help a little. I will try to print content of utf-8 file to IDE console and system console that is encoded in "Cp852".

My d:\data.txt contains ąźżćąś adsfasdf

Lets check this code

//I will read chars using utf-8 encoding
BufferedReader in = new BufferedReader(new InputStreamReader(
        new FileInputStream("d:\\data.txt"), "utf-8"));

//and write to console using Cp852 encoding (works for my windows7 console)
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out,
        "Cp852"),true); // "Cp852" is coding used in
                    // my console in Win7

// ok, lets read data from file
String line;
while ((line = in.readLine()) != null) {
    // here I use IDE encoding
    System.out.println(line);
    // here I print data using Cp852 encoding
    out.println(line);
}

When I run it in Eclipse output will be

ąźżćąś adsfasdf
Ą«ľ†Ą? adsfasdf

but output from system console will be

enter image description here

Upvotes: 2

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

Most likely you are using JVM default character encoding somewhere. If you are 100% sure your file is encoded using UTF-8, make sure you explicitly specify UTF-8 when reading as well. For example this piece of code is broken:

new FileReader("file.txt")

because it uses JVM default character encoding - which you might not have control over and apparently Netbeans uses UTF-8 while your operating system defines something different. Note that this makes FileReader class completely useless if you want your code to be portable.

Instead use the following code snippet:

new InputStreamReader(new FileInputStream("file.txt"), "UTF-8");

You are not providing your code, but this should give you a general impression how this should be implemented.

Upvotes: 4

Related Questions