write text file mix between arabic and english

江枫思渺然 提交于 2019-12-01 20:16:22

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)

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());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!