I have a program that users can input arabic text on text area. arabic text direction is right to left, and when i write the text to the file, Text's direction change to left to right. Can I write the arabic text to file with direction is right to left?
Reverse the text in memory then write it to disk. Reverse the reverse...
StringUtils.reverse(str)
You don't need to. Its upto the displaying program to show it in the right direction. You can store the data in file as it is, and the guy who reads it and displays it is responsible for that.
The direction desplayed depends on the program you open your file with. All you can do is to use Unicode and if the opening program has problems with Unicode you can do nothing about it. The OS where file opened on should support Unicode as well.
Refer to this blog. It will solve your problem
Related
I'm working currently on java project that uses Arabic Language, I found difficulty in writing in Arabic as shown in the image:
I wrote Arabic without any edit.
I added a reverse() method, it worked good but the letters aren't attached to each other, they're separate.
StringBuilder input = new StringBuilder();
input.append(jTextField2.getText());
input = input.reverse();
jTextField1.setText(input.toString());
I use site the flip the text, it didn't work as well.
I use the same site, but with jLabel it worked.
other method I use, but didn't work:
Try Orientation jTextField1.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Change the IDE encoding to URT-8 (I'm using Netbeans-JDK8).
Can anyone help me how to write & print Arabic in java correctly?
Please refer to this question -
Forcing RTL order in a JTextArea
Here is a sugestion to start the string with the character \u202e to force the text to be RTL.
Also i think it is not good approach to reverse the string, as it is not good user experience when the user do "copy paste", as he will copy reversed string...
A string entirely composed of characters from the Arabic block should render with correct RTL presentation without any directionality control characters. If it does not, it is likely that you have a problem with your operating system configuration, not with your Java code. Reversing the string is a terrible idea. Trying for visual-order rendering is going to get all messed up.
hi there I have a Java program I wanna know how to print the result of this program in a text file without losing it's colour .I mean the out put is in colour and I want to have the colourfull printed result Thanks
You can't.
Plain text is just that. There is no formatting in a plain text file that lets you specify color/font/size.
However, if you are displaying the text in a Bash shell or have configured your windows command console correctly, you could use ANSI Escape Codes to format the text.
You can't. Textfiles don't have colors.
You could wrap them in HTML tags and style them with css. (there are probably libaries that do that for you). This HTML file can be viewed with a webbrowser.
You could also use ANSI escape code to format your text (e.g. https://github.com/fusesource/jansi)
I am trying to write Persian text to a .txt file in Java. Because Persian (Farsi) is read from right to left, how can I write each line such that it is right aligned?
I am currently using Apache's FileUtils.writeLines(), but I am open to other alternatives in order to achieve the problem.
Thanks!
Text alignment is determined by UI that would show your text file. If you are using a plain text file, so it does not have facilities to tell it its text alignment.
If you insist on it, there are special Unicode characters that can tell UI it must be interpreted as right-to-left text. Please see here.
You can wrap each line into a String.format
String.format("%s", formatter.format(i))
or
Apache StringUtils has several methods: leftPad, rightPad, center and repeat.
Read following thread.
How can I pad a String in Java?
You just add spaces if you want to have lines with specific size, otherwise it depends on the tool you use for reading it.
i have a java applet application in which i use rich text area . i write URDU the national language of PAKISTAN. i managed to do so with uni codes. the problem is, when i write urdu in text area and select a font and color for each line it do all of this but when i save this file using UTF-8 encoding and then open it again it shows all text formatted as i choose format of last line.
my requirement is to open file as it is saved. i mean each file should have same formatting as i done before saving.
I'm still suffering with this problem even after bounty can any one help! dated 07-06-2010.
See, when you actually format text using some font and color, it will generate some RTF/HTML code right? You should try to get the RTF/HTML of the text area so that all your formatting can be saved in a file.
Basically all its a text file, so you need to get it with all code right?
Check this link for RTF formatted text saving mechanism.
Java JTextPane RTF Save
Also check HTMLEditorKit for more info.
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/html/HTMLEditorKit.html
thanks.
UTF-8 is an assignment of codes to characters. For convenience a decision was made that the lowest 127 codes are the same in ASCII and UTF-8. For all characters the codes differ.
UTF-8 Fonts have a character map (cmap) which assigned unicode code to their glyphs. There are very few fonts that cover large portions of the unicode range (Arial Unicode and Gentium i know, there are some others), and to get full coverage in a rendering solution, you have to mix fonts.
To be able to display arbitray Unicode texts, you therefore have to create a set of fonts with one as the default font and fallback fonts for the unicode characters that are not contained the default font. Back to Java and your Textpane: If you select a font for a given part of text in your Textpane, this only means that to render the text glyph are used from the selected font. But the text itself is not associated with the font in any means.
So you have two options:
You don't just store the UTF-8 text,
but also information about the
selected font, or
more interesting:
You store the text simply as UTF-8
and apply fonts after loading the
text into your textpanel!
I have a JTextArea which displays HTML of an Arabic web page. So it's essentially a mix of English and Arabic. In the JTextArea, with columns set to 30, certain text just disappears instead of wrapping properly. The weird thing is that if I copy the invisible text and paste it into Notepad, then I can see it in Notepad. If I change the number of columns to 40, everything displays fine. Any ideas?
See this screenshot of the problem:
Elie, thanks for the response. Not sure I explained the problem properly though. On the left in the screenshot is the JTextArea. On the right is the selection from the JTextArea pasted into Notepad. Does this make more sense now?
Is it the 30th character which is disappearing? It's possible due to the script that the JTextArea cannot render the Arabic characters properly. So it's counting the characters correctly, but doesn't realize that they take up more space. Support for such fonts is not great, so you may want to write a custom renderer for your JTextArea to deal with this (so you can manually take into account the proper amount of space required per character in Arabic and adjust the line wrap accordingly).