1

i m trying to read a file having having arabic text and then i need to place read text in a text view..

following is the code i tried:

public static String readRawFile(Context ctx, int resId) throws UnsupportedEncodingException {


    InputStream inputStream = ctx.getResources().openRawResource(resId);
    InputStreamReader isr = new InputStreamReader(inputStream, "UTF-8");
    BufferedReader reader = new BufferedReader(isr);
    String line;
    StringBuilder text = new StringBuilder();

    try {
        while ((line = reader.readLine()) != null) {
            text.append(line);
            Log.e("Line Read", line);
            text.append('\n');
        }
    } catch (IOException e) {
        return null;
    }
    return text.toString();
}

but what i the file read is following

��(P3�EP ��qDDGP ��qD1QN-�EN@pFP ��qD1QN-PJEP ���

how should i read file such that the text read is in arabic

STF
  • 1,485
  • 3
  • 19
  • 36
  • There may be a difference between the actual string and what's logged on the console. Check what you got in the string by setting it to some TextView to figure out if the data is indeed read wrong or is it only a logging issue. – Talha Mir May 31 '16 at 12:49

1 Answers1

0

trying using only Bufferreader, with it i can read both arabic and english words at the same time. And you dont need the UT-8 encoding. I hope this helps you:

public static void readArabic(String path) throws FileNotFoundException {


        try(BufferedReader red = new BufferedReader(new FileReader(path));) {
            String out;
            while ((out = red.readLine()) != null){
                System.out.println(out);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

This is the main method:

public static void main(String[] args) throws FileNotFoundException {

        String path1 = "/Users/addodennis/Desktop/Projects/HotelReservation/src/Data/testAra";
        readArabic(path1);


    }

And this is the output: I just copied some arabic text from the wikipedia page.

الأَبْجَدِيَّة العَرّة‎‎ al-abjadīyah al-ʻarabīyah or الحُرُوف العَرَبِيَّة ’ī (هِجَائِي) or alifbā’ī (أَلِفْبَائِي) يواجه مستخدمو الانترنت السوريون منذ بعض الوقت صعوبة في الدخول إلى موقع (ويكيبيديا الولوج إلى صفحات موقع الموسوعة الحرة (ويكيبيديا)، وهو واحد من أشهر المواقع العالمية على الشبكة الدولية.

And if you still want to use StringBuilder then you can do this also:

    public static String readArabic(String path) throws FileNotFoundException {

    StringBuilder add = new StringBuilder();
    try(BufferedReader red = new BufferedReader(new FileReader(path));) {
        String out;
        while ((out = red.readLine()) != null){
            //System.out.println(out);
            add.append(out);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return String.valueOf(add);
}
Seek Addo
  • 1,871
  • 2
  • 18
  • 30