我正在将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.


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

07-26 09:29
查看更多