在JTextPane中读取文件时丢失的行

时间:2011-06-08 18:29:22

标签: java swing

我正在将一个java文件读入JTextPane并且一些行被跳过,我似乎无法找到哪里,我想我只需要另一组眼睛来查看我的读取方法。

/**
 * 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()

任何帮助都会很棒。

亨特

也张贴于:http://www.coderanch.com/t/541081/java/java/Lines-lost-during-reading-file#2454886

4 个答案:

答案 0 :(得分:1)

问题出现在这里:

//ensure more lines exist
             if(fileScanner.hasNextLine())
             {
                line = fileScanner.nextLine();
                trimmedLine = line.trim();
             }

您正在用新行替换行,然后才追加它。因此,在任何附加之前,原始行将替换为新行。

答案 1 :(得分:1)

您的代码不会输出任何如下所示的行:

    int example /* my example is 3 */ = 3;
    for (int i = 0; i < 3; i ++) { // process now.   ... 

    } // okay I'm done.

注释可能不会在修剪线的开头处开始。

答案 2 :(得分:0)

你可以尝试

String s;
if ((s = fileScanner.nextLine()) != null) {
    trimmedLine = s.trim();
    //do stuff
}

答案 3 :(得分:0)

您应该尝试在Swing事件调度线程中运行您的方法。您可能遇到了线程问题。

如果这是问题,为了提高性能,您应该在某个线程中读取该文件,并在将整个文件作为String时将结果提交给GUI。