4

i'm trying to write in text file sentences that contains Arabic and English but the problem that both languages have direction RTL and LTR so the output text file don't save the order of the words and some words come before each other that is wrong, the problem fixed when i change the text direction from the notepad or any text editor, is there any way to fix this using java?

Mahmoud Ismail
  • 187
  • 1
  • 3
  • 12

3 Answers3

2

As I explained in my comment on your previous question, Unicode text files store the characters in logical order. There is a documented algorithm for how to handle bidirectional text, and control characters you can insert into the text stream to give hints to the renderer about, for example, where to attach punctuation when you have an Arabic quotation in the middle of an English sentence.

But ultimately the choice of the top-level "predominant" direction of the text as a whole is a matter for the component that is displaying the text rather than something that the text itself can control - the renderer has to decide whether it's dealing with a mostly-English paragraph containing some bits of Arabic or vice-versa.

For example, suppose I have a file containing the following logical sequence of characters (in line with the conventions in the bidi algorithm spec I use lowercase for left-to-right characters such as English and UPPERCASE for right-to-left characters such as Arabic):

abc def GHI! JKL mno? PQR

A viewer configured to treat the text as predominantly LTR would render this as

abc def LKJ !IHG mno? RQP

whereas a viewer configured to treat it as predominantly RTL would render exactly the same text as

                                      RQP ?mno LKJ !IHG abc def

(in the absence of control characters to the contrary the punctuation that lies at the boundary between a LTR and RTL segment will attach to the one that matches the overall paragraph direction)

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • thanks lan for your clarification, but i still can't solve my problem i tried the output text file to be UTF-8 and ansi to fix but the problem still exist, the editor i use is notepad or notepad++ – Mahmoud Ismail Jul 13 '14 at 14:34
  • @MahmoudIsmail I've added an example that may clarify things a bit more. – Ian Roberts Jul 13 '14 at 15:15
  • For plain text I don't think there's a way within the text itself to tell the viewer which direction to use. Higher level formats have "out of band" signals you can use, e.g. `` in HTML. – Ian Roberts Jul 13 '14 at 18:11
0

i think you can just set the charset to UTF-8 and youll get the order of the words right. take a look at this

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Output.txt"), "UTF-8"));
try {
        out.write("1.");
        out.write("English ");
        out.write("2.");
        out.write("عربي ");
        out.write("3.");
        out.write("Hey ");
        out.write("4.");
        out.write("السلام ");
    } finally {
        out.close();
    }

File f = new File("Output.txt");
Scanner fileprint = new Scanner(f);

while(fileprint.hasNext()){
    System.out.println(fileprint.next());
}
Hamad AlGhanim
  • 166
  • 1
  • 13
  • thanks hamad for you reply but my problem you can see in the generated text file you called output.txt if you open it you will find that the words order changed and some english words shifted and that the exact problem that i face. – Mahmoud Ismail Jul 13 '14 at 14:31
0

If you run in Eclipse, right click you program, "Run as", "Run Configuration", "common", "Encoding", "Default inherit UTF-8", just use regular File reader and writer without specifying UTF-8 in your code.

Feng Zhang
  • 1,698
  • 1
  • 17
  • 20