| Author |
How to keep formatting while reading files
|
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
I'm trying to read a .java file into a JTextArea and no matter what method I use to read in the file the formatting is never preserved. The actual code is ok but the comments always get messed up. Here are my attempts.
Scanner:
Scanner reading the full text at once:
BufferedReader:
Is there anyway to keep the formatting the same??
PS - also posted at http://stackoverflow.com/questions/6157294/how-to-keep-formatting-while-reading-files
Hunter
|
"If the facts don't fit the theory, get new facts" --Albert Einstein
|
 |
Carey Brown
Ranch Hand
Joined: Nov 19, 2001
Posts: 161
|
|
|
Use a fixed width font.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Formatting marks exist regardless of the type of font I am using.
Hunter
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
"Messed up"?
"Formatting marks"?
I think we need a better description of the problem. I can't tell from those phrases what the problem is. Can you show us an example?
By the way I wouldn't have written any of that code. Here's the code I would have written:
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Sure, I have a .java that looks like this with comments organized the way I want them:
Then I use the readFileData() method to read this file into a JTextArea and the comments are organized the way I had them in the original file. This is what is in the JTextArea:
As you can see the comments come out not lined up in the way they were in the original file. I am referring to the block comments at line 55+.
Hunter
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
Did you use tabs in those comments? If so, have you got the tab handling set up to work the same in the JTextPane and in wherever you created this text?
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
I'm not sure if I used tabs or not, that seems like a likely reason. Also I didn't know that I could specify how tabs were read. Is that a JTextPane only setting? or will I be able to do that is JTextArea, which is what I am using.
Edit: I looked at the API closer and there is a setTabSize() method in the JTextArea class. I will look into that, thanks for the help everyone.
Hunter
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Paul Clapham wrote:By the way I wouldn't have written any of that code. Here's the code I would have written:
Surely you mean this:
After all, you should always close your resources.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Paul Clapham wrote:Did you use tabs . . .
Yes. some of the indentation is done with tabs and some with spaces.
You could have found that out, as I did, because you could use the "edit" button to inspect the code.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
|
You can also copy the code from the "view plain" window onto a word-processor and use the "view non-printing characters" option.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Rob Spoor wrote:
Paul Clapham wrote:By the way I wouldn't have written any of that code. Here's the code I would have written:
Surely you mean this:
After all, you should always close your resources.
I had seen the read() method in the JTextComponent method list, but didn't use it immediately. The overall goal of this is for me to be able to create a text editor for .java and other files. Swing is also an area of Java where I am very weak, which is another reason I am doing this project. Thank you all for your advice. I am sure that I will have more questions as I make progress in this project.
Hunter
|
 |
 |
|
|
subject: How to keep formatting while reading files
|
|
|