How to write inside a text file using Arabic letters with accents? Java

I have a problem with the viewer Netbeans

. I have a string in Arabic that includes accents at the top of each letter. When I remove accents from a string, the letters are displayed correctly. However, when I write a line with accents, it becomes somehow messy (wrong).

This is an example of what's going on:

  • Unaccented text (correct): بسم الله الرحمن الرحيم

  • Accepted text (incorrect): it shows broken, but if i copy it here it prints correctly

  • It should be like this (correct): بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ

The code I wrote is to read a text file that includes an Arabic string along with its accents, then write it correctly in the new file, then delete the old file at the end. This is the code:

public void arabicReformer(File disordered) {
    File output = new File("data/temp2.txt");

    try {
        BufferedReader br = new BufferedReader(
                new InputStreamReader(new FileInputStream(disordered), "UTF8"));
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(output), "UTF8"));
        String line;

        while ((line = br.readLine()) != null) {
            bw.write(line.trim() + "\n");
        }
        br.close();
        bw.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());
    }
    output.renameTo(disordered);
}

      

PS: when I copy the wrong arabic accented string here it prints correctly!

+3


source to share





All Articles