java print in colour in text file - java

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)

Related

Emoji inside Whatsapp chat

I am having a Whatsapp chat txt file inside of which emojis have been replaced by such text------> "���"
I want to convert that text to specifics emojis. How can I do that and use it inside a java application?
As #Abhishek pointed out, you need to use a different type of encoding. Whatsapp does backup with UTF-8 converting the emoticons into string representations. If you want to see the real emoji, you will have to use Unicode instead. Unicode contains sections which specify emoji as "characters". They're regular characters, you only need a font which can display them. Also see the Unicode Emoji FAQ.
In a text file, characters are basically encoded as numbers in the form of bytes. To display those visually on a computer screen you need a font which contains the visual glyph to render this character. Since the process is always numeric identifier → font → visible glyph, it should be pretty obvious that a "character" can be anything visual, including emoji or any other image.
Maybe all you need is a a font which contains the visual glyph to render these characters. See this for reference.

Creating a Java file with right-aligned strings

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.

Writing from JTextArea to File, No Break Lines?

Ok I can type into the JTextArea and save it to a file, however when I open the file in notepad for example it has no breaklines. When loaded back into my program the file has the breaklines again.
I want to know why this happens, whats the point why not just have it the same?
and how to make the format be the same when outputting to file so it is the same as it looks in the JTextArea.
In the JTextArea api I do not see any methods that give me any clue on how to do this.
Thanks!
Your version of Notepad is most probably not recognizing "unix" end of line sequences (also used/produced by some Java libraries). Try with another text editor. If I remember correctly, Wordpad makes a better job. If not this, you should post code and operating environment.
I can type into the JTextArea and save it to a file,
How are you saving the file?
In the JTextArea api I do not see any methods that give me any clue on how to do this
Use the textArea.write(...) method to save the file. It will save the file with the proper end of line string for the current platform you are using.
See Text and New Lines for more information.

How to change the fontsize when writing to a text file

I am writing information to a text file, how do i I change the font size for Data that i am writing to the text file. also how do you set the data to be written in bold or underlined.
Actually, the .txt format does not contain formatting such as font size, bond, italics, etc.
You probably want to use a markup language like html, or another file format, such as rtf, pdf or something else.
Here's some information on learning html: w3schools.com
And a java library for writing rtf: srw library
And a java library for writing pdf: pdfbox
Good luck.
Plaintext files do not have format and style options.
For this you would have to write the data in a different format. E.g. html if you want to view it in a browser later.
But the format you chose greatly depends upon how and by whom those data should be read.
Supposing you are editing your text in a JTextArea you might do this:
this.textArea.setFont(new Font("Arial", Font.PLAIN, 12));
//the code comments itself
if you want bold then write Font.BOLD
As someone already told you, unfortunately plain .txt files don't contain formatting. If you need to format the text, you should change format and try HTML.
As all people say .txt files don't have format options.
I want to clarify something
It's possible have a txt file with specific format.
The way to do it is with a specific viewer:
You can add style, text size with notepad
You can add underlines or line spacing with notepad++
depending on you txt viewer you can postformat.
Then a possible solution would be POSTFORMATING, it works, for example if you want to print your file, only change setting on your viewer and you could obtain the result that you are looking for.
I'm working in this way for printing my txt reports.

how to manage formatting of text when read a save file?

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!

Categories