• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you display what's read from a file in a JTextArea properly?

 
Femi Alla
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!,
I have this application that logs user activity to a log file on the local disk.At the user's request, it displays the log file in a JTextArea. The problem is that what get's displayed looks like junk. Upon close inspection, you'll realize that the contents of the log file are actually being displayed, but intersparsed with a lot of squares. I suspect this is because characters and Strings are represented or stored using 16 bits and because ASCII is 8 bit,(most of the text data within a program uses standard ASCII.) the upper 8 bits result in a blank character. Is this diagnosis right?
How do I solve this problem?
Any useful suggestions would be appreciated.
And just in case you would need to see the whole program to get a clearer picture of what's being discussed, I'm pasting all the parts of it that relate to the current discussion, here.
The log file is created using a RandomAccessFile, like this:

Now I have a JFrame that implements ActionListener which has a JMenu that calls




Thanks reading up to this point. You may as well tell me what you think
Thanks.
 
Joe Ess
Bartender
Posts: 9295
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are writing binary data with RandomAccessFile then trying to read it in as character data using BufferedReader. Either change your output to use a Reader or your input to use a RandomAccessFile.
 
Femi Alla
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much Joe,
Fixed it.
Just in case anyone's looking... here's the code that got it fixed:

Again, thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic