Lines lost during reading of a file in JTextPane
I am reading a java file into a JTextPane and some of the lines ar开发者_开发技巧e getting skipped over, and I cant seem to find where, I think I just need another set of eyes to look at my read method.
/**
* Reads a File object line by line and appends its data
* to the JTextPane. I chose to NOT use the JTextPane's read()
* function because it creates formatting conflicts.
*
* @param file The File object to read data from
*/
public void readFileData(File file)
{
Scanner fileScanner = null;
try
{
fileScanner = new Scanner(file);
}
catch(FileNotFoundException fnfe)
{
System.err.println(fnfe.getMessage());
}
while(fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
String trimmedLine = line.trim();
//test line for potential keywords, ignore comments
if(!trimmedLine.contains("/**") && !trimmedLine.contains("/*") &&
!trimmedLine.contains("//"))
{
boolean tst = Keywords.hasKeywords(line);
if(tst) //keywords exist in the line, split the line up
{
String[] words = line.split("\\s");
for(String word : words)
{
if(Keywords.isKeyword(word))
{
//copy keyword object
Keywords k = map.get(word);
//append keyword with proper color
ui.append(k.getText() + " ", k.getColor());
}
else //not a keyword append normally
{
ui.append(word + " ", Color.BLACK);
}
}
ui.append(newline);
}
else //if the line had no keywords, append without splitting
{
ui.append(line, Color.BLACK);
ui.append(newline);
}
}
else
{
//create darker color, because the built-in
//orange is too bright on your eyes
Color commentColor = Color.ORANGE.darker().darker();
//if this is the start of a multiline comment
if(trimmedLine.startsWith("/**") || trimmedLine.startsWith("/*") )
{
//while not at the end of the comment block
while(!trimmedLine.endsWith("*/"))
{
//append lines
ui.append(line, commentColor);
ui.append(newline);
//ensure more lines exist
if(fileScanner.hasNextLine())
{
line = fileScanner.nextLine();
trimmedLine = line.trim();
}
}
//append the ending line of the block comment, has '*/'
ui.append(line, commentColor);
ui.append(newline);
}
else if(trimmedLine.startsWith("//")) //start of single line comments
{
ui.append(line, commentColor);
ui.append(newline);
}//end if
}//end if
}//end while
fileScanner.close();
}//end readFileData()
Any help would be great.
Hunter
also posted at: http://www.coderanch.com/t/541081/java/java/Lines-lost-during-reading-file#2454886
Your code is not going to output any lines that look like the following:
int example /* my example is 3 */ = 3;
for (int i = 0; i < 3; i ++) { // process now. ...
} // okay I'm done.
comments might not start at the beginning of a trimmed line.
The problem arises here:
//ensure more lines exist
if(fileScanner.hasNextLine())
{
line = fileScanner.nextLine();
trimmedLine = line.trim();
}
You are replacing line with a new line, and only then you append it. So the original line is replaced with the new one before any append.
you could try
String s;
if ((s = fileScanner.nextLine()) != null) {
trimmedLine = s.trim();
//do stuff
}
You should try running your method in the Swing event dispatch thread. You are possibly having threading issues.
If that is the problem, for performance, you should read the file in some thread and submit the result to the GUI once you have the whole file as a String.
精彩评论