Java StreamTokenizer slashSlashComments()方法及示例

StreamTokenizer类slashSlashComments()方法

  • slashSlashComments()方法在java.io包中可用。

  • slashSlashComments()方法用于标识此StreamTokenizer是否识别双斜杠注释(C ++类型注释)。当布尔变量设置为true时,表明此StreamTokenizer识别双斜杠注释,否则特别不使用双斜杠注释。

  • slashSlashComments()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • slashSlashComments()方法在标识双斜杠注释时不会引发异常。

语法:

    Public void slashSlashComments(boolean status);

参数:

  • 布尔状态–当此参数识别并避免双斜杠注释(即,基于C ++的注释)时,将其设置为true。

返回值:

该方法的返回类型为void,不返回任何内容。

示例

//“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; Java程序演示示例 
//“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;无效的slashSlashComments(boolean status)
//“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; StreamTokenizer的方法

import java.io.*;

public class slashSlashComments {
    public static void main(String[] args) {
        String str = "Hi, This is \n a mathematical expression :\n " +
            " 2 * 4 = 8 //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;" + "8 + 5 = 13";

        try {
            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; Instantiates FileOutputStream  and ObjectOutputStream 
            FileOutputStream fos_stm = new FileOutputStream("D:\\includehelp.txt");
            ObjectOutputStream obj_out_stm = new ObjectOutputStream(fos_stm);

            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; By using writeUTF() method is to
            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; write the given string in the file
            obj_out_stm.writeUTF(str);
            obj_out_stm.flush();

            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; Instantiates FileOutputStream  and ObjectOutputStream 
            ObjectInputStream obj_in_stm = new ObjectInputStream(new FileInputStream("D:\\includehelp.txt"));

            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; Instantiates StreamTokenizer and Reader
            Reader reader = new BufferedReader(new InputStreamReader(obj_in_stm));
            StreamTokenizer st = new StreamTokenizer(reader);

            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; By using slashSlashComments() method is to
            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; set comments when boolean set to true and
            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; when it encounters '//“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;' in str
            st.slashSlashComments(true);

            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; Here, we are considering initially 
            //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; file is not empty
            boolean end_of_file = false;

            while (!end_of_file) {
                //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; By using nextToken() method is to
                //“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“;“ +” 8 + 5 = 13“; parse the next token from the stream
                int token = st.nextToken();

                switch (token) {
                    case StreamTokenizer.TT_EOF:
                        System.out.println("End of File Found");
                        end_of_file = true;
                        break;

                    case StreamTokenizer.TT_EOL:
                        System.out.println("End of Line Found");
                        break;

                    case StreamTokenizer.TT_WORD:
                        System.out.println("word: " + st.sval);
                        break;

                    case StreamTokenizer.TT_NUMBER:
                        System.out.println("number: " + st.nval);
                        break;

                    default:
                        System.out.println((char) token + " Found.");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

输出结果

word: BHi
, Found.
word: This
word: is
word: a
word: mathematical
word: expression
: Found.
number: 2.0
* Found.
number: 4.0
= Found.
number: 8.0
End of File Found