java - 在 JTextPane 中读取文件时丢失行

标签 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

最佳答案

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

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

    } // okay I'm done.

注释可能不会从修剪行的开头开始。

关于java - 在 JTextPane 中读取文件时丢失行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6283467/

相关文章:

java - 如何在链表中的某个索引处递归插入节点

java - 如何一次性停止所有for循环的运行

java - thymeleaf : Getting value from a map while iterating through a list

java - .getSize() 在 JScrollPane 中更新,但不在 JPanel 中更新

java - Android GradientDrawable setColor 不起作用

java - 如何在 JTextPane java 中复制图像?

JavaFX ScrollPane 禁用箭头键滚动

java - 以奇数序列打印奇数

java - 我如何使输出出现在不同的列中?

java - JTextPane - 自动换行插入的 JLabel 组件