aspose file tools*
The moose likes I/O and Streams and the fly likes How do you display what's read from a file in a JTextArea properly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How do you display what Watch "How do you display what New topic
Author

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

Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
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.


SCJP
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8705
    
    6

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Thanks so much Joe,
Fixed it.
Just in case anyone's looking... here's the code that got it fixed:

Again, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you display what's read from a file in a JTextArea properly?
 
Similar Threads
Accessing mp3 properties (128 bytes at the end) dirty hack...How to make this more efficient?
Performing read/write to the file : how
JQ+ question RandomAccessFile help.....
io
file, UTF8