views:

834

answers:

4

I'm trying to read a file which contain English & Arabic characters on each line and another file which contains English & Chinese characters on each line. However the characters of the Arabic and Chinese fail to show correctly - they just appear as question marks. Any idea how I can solve this problem?

Here is the code I use for reading:

try
    {

        String sCurrentLine;
        BufferedReader br = new BufferedReader(new FileReader(directionOfTargetFile));
        int counter = 0;

        while ((sCurrentLine = br.readLine()) != null)
        {

            String lineFixedHolder = converter.fixParsedParagraph(sCurrentLine);
            System.out.println("The line number "+ counter + " contain : " + sCurrentLine);
            counter++;

        }

    }

Edition 01

After reading the line and getting the Arabic and Chinese word I use a function to translate them by simply searching for Given Arabic Text in an ArrayList (which contain all expected words) (using indexOf(); method). Then when the word's index is found it's used to call the English word which has the same index in another Arraylist. However this search always returns false because it fails when searching the question marks instead of the Arabic and Chinese characters. So my System.out.println print shows me nulls, one for each failure to translate.

*I'm using Netbeans 6.8 Mac version IDE


Edition 02

Here is the code which search for translation:

int testColor = dbColorArb.indexOf(wordToTranslate);
        int testBrand = -1;
        if ( testColor != -1 )
        {
            String result = (String)dbColorEng.get(testColor);
            return result;
        }

        else
        {
            testBrand = dbBrandArb.indexOf(wordToTranslate);
        }
        //System.out.println ("The testBrand is : " + testBrand);
        if ( testBrand != -1 )
        {
            String result = (String)dbBrandEng.get(testBrand);
            return result;
        }
        else{
            //System.out.println ("The first null");
            return null;

        }

I'm actually searching 2 Arraylists which might contain the the desired word to translate. If it fails to find them in both ArrayLists, then null is returned.


Edition 03

When I debug I found that lines being read are stored in my String variable as the following:

 "3;0000000000;0000001001;1996-06-22;;2010-01-27;����;;01989;������;"

Edition 03

The file I'm reading has been given to me after it has been modified by another program (which I know nothing about beside it's made in VB) the program made the Arabic letters that are not appearing correctly to appear. When I checked the encoding of the file on Notepad++ it showed that it's ANSI. however when I convert it to UTF8 (which replaced the Arabic letter with other English one) and then convert it back to ANSI the Arabic become question marks!

+2  A: 

IT is most likely Reading the information in correctly, however your output stream is probably not UTF-8, and so any character that cannot be shown in your output character set is being replaced with the '?'.

You can confirm this by getting each character out and printing the character ordinal.

Paul Wagland
+6  A: 

FileReader javadoc:

Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

So:

Reader reader = new InputStreamReader(new FileInputStream(fileName), "utf-8");
BufferedReader br = new BufferedReader(reader);

If this still doesn't work, then perhaps your console is not set to properly display UTF-8 characters. Configuration depends on the IDE used and is rather simple.

Update : In the above code replace utf-8 with cp1256. This works fine for me (WinXP, JDK6)

But I'd recommend that you insist on the file being generated using UTF-8. Because cp1256 won't work for Chinese and you'll have similar problems again.

Bozho
I'm getting a error "incompatible types - require: java.io.FileReader found: "java.io.InputStreamReader"
MAK
where are you getting that? just copy the two lines from my updated answer
Bozho
ok it's executing...however I still have the same problem of characters showing as <?>
MAK
then check the other part of my answer. and tell me what IDE you are using (if you are using one)
Bozho
I'm using Netbeans 6.8 Mac version IDE
MAK
check my update.. you are looking at the wrong place.. ;)
Bozho
I'm sorry for the confusion...I'm not using the String comparing, I'm actually searching it in Arraylist.
MAK
I double checked the file, it's not corrupted...I mean it's showing fine on most of the PCs I tried it on. Please check the links for the photos that shows the data. Mac: http://www.4shared.com/file/221863564/381bfd08/text-Mac.htmlPC: http://www.4shared.com/file/221862075/e8705951/text-Windows.html
MAK
Thanks Bozho for you incredible effort to assist me in the question. Problem finally is solved :)
MAK
A: 

i have a related query here. Please help me if its possible. I am not able to read chinese characters from the console to a String variable using Buffered Reader.

If I try saving the unicode value to a string and then try printing, it works. So I am guessing the problem is in reading it to string alone.

Advise please !

harirajmohan
A: 
public void writeTiFile(String fileName,String str){
    try {
        FileOutputStream out = new FileOutputStream(fileName);
        out.write(str.getBytes("windows-1256"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Ahmad Alhaj Hussein