Writing from JTextArea to File, No Break Lines? - java

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.

Related

java print in colour in text file

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)

How can I hide resource images in executable .Jar?

I made a little java game and if you beat it, it loads embarrassing pictures of my friends.
The problem is they can cheat and open the jar with WinRar and look at all the pictures.
How can I prevent this?
(This is with their consent. It's completely friendly.)
You could convert the image to a hexidecimal String and store it in a file without an extension inside the .jar file.
To make things even more difficult for them you could add a non-hexidecimal character into the text periodically and then remove that character from the String in your code before converting the text back into an image. That way, even if they recognize the text file as an image in text they wouldn't be able to convert it unless they spotted the extra illegal hex characters.
There is no way I know of to do that programmatically. However, what I would do is use an obscure format for the pictures (e.g. .tif, .ico), then name them things like library1.jar or data.dat. Last, add a lot of dummy files that have no purpose. This makes it hard for your friends to tell which files are pictures and how to view them.

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.

JTextArea JLabel compare two txt. files line by line

everybody. I have completed my code two txt files by showing on the same area.
but I want to compare two txt files line by line. and I want to show the differences with Colored lines and finally, The letter or words that are different, different line I'm going to bold.
how can i start?
thanks for now, my code is here.
JTextArea might make a suitable view, but you still need to model the differences. I'd look at the Eclipse or NetBeans platforms, or perhaps svnview.
You might need to ask user to upload both versions of file.
In the server you need to compare line by line, and store the line numbers of the file differences in another file or location
Then, while displaying, get the line numbers and give them proper decoration (i.e bold etc.).

Java: How to immediately send changes in a text field to a file?

Is it hard to do the following with the Java? First I need to generate a window with a text field (no buttons, nothing, just a text field). Than, when a user types a symbol in the text filed, program immediately stores information about this in a file (which symbol was typed and when). That's it.
No, it's not hard. The following code snippet may get you running with the JTextField:
http://www.java2s.com/Code/Java/Swing-JFC/TextfieldsandJavaevents.htm
and this can help with writing to a file:
http://www.java2s.com/Code/Java/File-Input-Output/WritelinesoftexttofileusingaPrintWriter.htm
So you should read something about JFrame and JTextField and after (Add)ActionListeners (function in JTextField) in java and File Streams like FileInputStream and FileOutputStream.
It's very easy !

Categories