JTextArea JLabel compare two txt. files line by line - java

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.).

Related

Java tabs ("\t") not working using FileWritter

As you can see, after every text comes a tab, everything is working fine, but after third tab (see output) it generates a space not a tab.
fileWriter = new FileWriter(indexFile, true);
fileWriter.append(id).append("\t");
fileWriter.append(String.valueOf(idx)).append("\t");
fileWriter.append(String.valueOf(pageCount)).append("\t");
fileWriter.append(postal.toUpperCase()).append("\t"); <-- this one
fileWriter.append(address.toUpperCase());
fileWriter.append("\r\n");
My output:
00000347 1 1 FB-6666 DUMMY STREET 1 LAT
The problem comes after "FB-6666".
Any ideas on this?
No, it really is generating a tab - it's just that whatever you're using to view the file is deciding to handle tabs by aligning them to some boundary or other. If you make your postal value FB-6666x I suspect you'll then see a much larger space.
This isn't a problem with the file content at all.
If you want to enforce a certain number of spaces between columns, you'll need to write that many spaces. Alternatively, something else to view the file...
Tabstops don't work this way. As the wikipedia article states,
Tab stops are set manually, and pressing the tab key causes the carriage to go to the next tab stop. In text editors on a computer, the same concept is implemented simplistically with automatic, fixed tab stops.
This means the tabstop will stop at a predefined position in the textfile, not after let's say the space it would take to insert a specific amount of spaces.
It is writing a tab I promise - to convince yourself of this you should either open the resulting file in a hex editor and look at the value in that position, or add another character to the value before the tab and see how the format of the output changes.

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.

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.

importing dialogue from a text file and displaying it on a drawpanel - Java

i have a bit of problem which i have run into, im trying to shorten my code for a game and i got the idea to import all the dialogue from a text file instead of hardcoding it into the code itself. however it would work if every screen only had one line to display but somescreens have more than one
if (screenCount==1)
g.drawString("Hi there!", 25,515);
if (screenCount==2)
g.drawString("Welcome to the world of Pok\u00E9mon!", 25,515);
if (screenCount==3){
g.drawString("My name is Professor BLANK!", 25,515);
g.drawString("Everyone calls me the Pok\u00E9mon Professor!", 25,540);
}
As you can see for screen one and two i could easily put the dialogue in a text file like so:
1:Hi there!
2:Welcome to the world of Pok\u00E9mon!
But for the third screen I couldnt figure out how to import it/ write it in a text file and import it
3:My name is Professor Shinwa!
Everyone calls me the Pok\u00E9mon Professor!
MORE INFO: The game only displays two lines at a time at
g.drawString("", 25,515); //the first line x and y values
g.drawString("", 25,540); // the second line x and y values
I have around 37 screens and around half or more are two lines.
Thanks for any help, much apreciated :D
You can use the escape sequence \n in your String as pointed in the JavaDocs:
Insert a newline in the text at this point.
Your String could look like:
3:My name is Professor Shinwa!\nEveryone calls me the Pok\u00E9mon Professor!
BUT I think a better solution is to put each screen text in a separate textfile. You can then read the file until no more lines are remaining and print them on the screen. The textfiles should be named with screen#, e.g. screen1, screen2 ... so you don't need this big amount of if-blocks in your code. Just concatenate the screen with your current screennumber and read this file, done. What if you need dialogues with response from the user? How would you handle this case?

Categories