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.
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)
Doing linguistics and phonetics, I often need to use certain special phonetic symbols. Although I'm using a special keyboard layout that enables me to write some of those characters by typing, they key combinations can often get both quite complex and highly repetitive, so I would like to create a litle app that would contain some buttons, perhaps, each of them capable of sending a specified (phonetic) symbol to whatever the current cursor position is, no matter what window on one's screen is in focus.
Is anything of this sort possible to do in Java?
I've seen a solution that copies the values into clipboard and then pastes them (Java paste to current cursor position), but that is not a very clean way to do it, is it? Is there a way better than just pasting the charactedr(s) via ctrl+V?
Many thanks for any help or advice in advance!
P.
You can use the AWT Robot to generate key press events. This will not provided the ability to insert arbitrary unicode characters but you can combine it with the technique you already described: transfer the unicode characters to the clipboard and generate a CTRL+V key event afterwards. You can try to save and restore the original clipboard content but this will work with types supported by Java only.
The focus problem mentioned in the comments can be solved by setting the window to not receive the focus via Window.setFocusableWindowState with an argument of false.
An alternative is to provide the unicode text via drag&drop. Most applications support dropping text in their input fields. The code for exporting the text is very similar as both, clipboard and d&d use the same interfaces in Java.
I just got a question about alignment in the library iText. Say if i was to do a quote program.
For example
On the Left side of the page I would want to put Quote # 01 and on the same line on the other side of the page I want to put the date.
Is this possible using the Paragraph class? If not can any body help me out with any other solutions?
To the best of my knowledge, you can't do this within a single paragraph. You'll have to get the current position (cb.getYLine()) and place a separate chunk with the date. See itext positioning text absolutely if you need pointers on placing the chunk.
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