📜  Java Java类设置 1

📅  最后修改于: 2022-05-13 01:54:58.019000             🧑  作者: Mango

Java Java类设置 1

StreamTokenizer 类 - 设置 1

Java.io.StreamTokenizer 类将输入流解析为“令牌”。它允许一次读取一个令牌。 Stream Tokenizer 可以识别数字、带引号的字符串和各种注释样式。
宣言 :

public class StreamTokenizer
  extends Object

构造函数:
StreamTokenizer(Reader arg):创建一个解析给定字符流的分词器。

方法 :

  • 评论字符:
  • Java .io.StreamTokenizer.commentChar(int arg)通过这个 StreamTokenizer 忽略从单行注释字符到行尾的所有字符。
    句法 :
public void commentChar(int arg) 
Parameters : 
arg : the character after which all characters are ignored in the line.
Return :
No value is returned.

执行 :

// Java Program illustrating use of commentChar() method
  
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws InterruptedException, 
                                              FileNotFoundException, IOException
    {
        FileReader reader = new FileReader("ABC.txt");
        BufferedReader bufferread = new BufferedReader(reader);
        StreamTokenizer token = new StreamTokenizer(bufferread);
        // Use of commentChar() method
        token.commentChar('a');
        int t;
        while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
        {
            switch (t)
            {
            case StreamTokenizer.TT_NUMBER:
                System.out.println("Number : " + token.nval);
                break;
            case StreamTokenizer.TT_WORD:
                System.out.println("Word : " + token.sval);
                break;
  
            }
        }
    }
}

笔记 :
该程序不会在此处运行,因为不存在“ABC”文件。您可以在系统上的Java编译器上检查此代码。
要检查此代码,请在您的系统上创建一个文件“ABC”。
“ABC”文件包含:

程序员
1
2
3
极客
你好
一个程序在这里解释我的朋友。

输出:

Word : Progr
Number : 1.0
Number : 2.0
Number : 3.0
Word : Geeks
Word : Hello

  • lineno() : Java.io.StreamTokenizer.lineno()返回此 StreamTokenizer 的当前行号。
    句法 :
    public int lineno()
    Parameters : 
    arg : the character after which all characters are ignored in the line.
    Return :
    returns the current line number of this StreamTokenizer.

    执行 :

    // Java Program  illustrating use of lineno() method
      
    import java.io.*;
    public class NewClass
    {
      
        public static void main(String[] args) throws InterruptedException, 
                                                      FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
              
            token.eolIsSignificant(true);
            // Use of lineno() method 
            // to get current line no.
            System.out.println("Line Number:" + token.lineno());
      
            token.commentChar('a');
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_EOL:
                    System.out.println("");
                    System.out.println("Line No. : " + token.lineno());
                    break;
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
      
                }
            }
        }
    }
    

    输出:

    Line Number:1
    Word : Progr
    
    Line No. : 2
    Number : 1.0
    
    Line No. : 3
    Number : 2.0
    
    Line No. : 4
    Number : 3.0
    
    Line No. : 5
    Word : Geeks
    
    Line No. : 6
    Word : Hello
    
    Line No. : 7
    Word : This
    Word : is
    

  • toString() : Java.io.StreamTokenizer.toString()将当前 Stream 令牌表示为字符串及其行号。
    句法 :
    public String toString()
    Return :
    represents current Stream token as a string along with it's line no.

    执行 :

    // Java Program  illustrating use of toString() method
       
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException, 
                                                  FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
              
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_NUMBER:
                    // Value of ttype field returned by nextToken() 
                    System.out.println("Number : " + token.nval);
                    break;
                    // Value of ttype field returned by nextToken()
                case StreamTokenizer.TT_WORD:
                      
                    // Use of toStringn() method
                    System.out.println("Word : " + token.toString());
                    break;
                 }
            }
        }
    }
    

    输出:

    Word : Token[Programmers], line 1
    Number : 1.0
    Number : 2.0
    Number : 3.0
    Word : Token[Geeks], line 5
    Word : Token[Hello], line 6
    Word : Token[a], line 7
    Word : Token[Program], line 7
    Word : Token[is], line 7
    Word : Token[explained], line 7
    Word : Token[here], line 7
    Word : Token[my], line 7
    Word : Token[friends.], line 7
    

  • eolIsSignificant() : Java.io.StreamTokenizer.eolIsSignificant(boolean arg)告诉是否将行尾视为标记。如果 'arg' 为真,则它的行尾被视为一个标记。如果为真,则该方法返回 TT_EOL 并在到达行尾时设置 ttype 字段。
    如果 'arg' 为假,则行尾被简单地视为一个空格。
    句法 :
    public void eolIsSignificant(boolean arg)
    Parameters :
    arg : boolean which tells whether to take EOL as a token or white space
    Return :
    No value is returned.

    执行 :

    // Java Program  illustrating use of eolIsSignificant() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
        FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            boolean arg = true;
            // Use of eolIsSignificant() method
            token.eolIsSignificant(arg);
            // Here the 'arg' is set true, so EOL is treated as a token
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_EOL:
                    System.out.println("End of Line encountered.");
                    break;
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
      
                }
            }
        }
    }
    

    笔记 :
    该程序不会在此处运行,因为不存在“ABC”文件。您可以在系统上的Java编译器上检查此代码。
    要检查此代码,请在您的系统上创建一个文件“ABC”。
    “ABC”文件包含:

    1
    极客
    2
    为了
    3
    极客

    输出 :

    Number : 1.0
    End of Line encountered.
    Word : Geeks
    End of Line encountered.
    Number : 2.0
    End of Line encountered.
    Word : For
    End of Line encountered.
    Number : 3.0
    End of Line encountered.
    Word : Geeks

  • nextToken() : Java.io.StreamTokenizer.nextToken()解析输入流中的下一个标记,并将它的值连同诸如“sval”、“nval”之类的附加字段一起返回给 ttype 字段。
    句法 :
    public int nextToken()
    Parameters :
    ------
    Return :
    value to the ttype field

    执行 :

    // Java Program  illustrating use of nextToken() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
        FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            // Use of nextToken() method to parse Next Token from the Input Stream
            int t = token.nextToken();
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
      
                }
            }
        }
    }
    

    笔记 :
    该程序不会在此处运行,因为不存在“ABC”文件。您可以在系统上的Java编译器上检查此代码。
    要检查此代码,请在您的系统上创建一个文件“ABC”。
    “ABC”文件包含:

    1
    这个程序告诉
    2
    关于使用
    3
    下一个令牌()方法

    输出 :

    Word : This
    Word : program
    Word : tells
    Number : 2.0
    Word : about
    Word : use
    Word : of
    Number : 3.0
    Word : next
    Word : token
    Word : method

  • lowerCaseMode() : Java.io.StreamTokenizer.lowerCaseMode(boolean arg)告诉是否自动将单词小写。如果标志 - 'arg' 设置为 true,则降低 'sval' 字段的值。
    句法 :
    public void lowerCaseMode(boolean arg)
    Parameters :
    arg : indicates whether to lowercase the word tokens automatically or not
    Return :
    void

    执行 :

    // Java Program  illustrating use of lowerCaseMode() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
        FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            /* Use of lowerCaseMode() method to
               Here, the we have set the Lower Case Mode ON
            */
            boolean arg = true;
            token.lowerCaseMode(arg);
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
      
                }
            }
        }
    }
    

    笔记 :
    该程序不会在此处运行,因为不存在“ABC”文件。您可以在系统上的Java编译器上检查此代码。
    要检查此代码,请在您的系统上创建一个文件“ABC”。
    “ABC”文件包含:

    你好极客
    这是关于
    小写模式()

    输出 :

    Word : hello
    Word : geeks
    Word : this
    Word : is
    Word : about
    Word : lowercasemode

  • normalChar() : Java.io.StreamTokenizer.ordinaryChar(int arg)将 'arg'字符设置为普通字符。如果 arg 字符具有单词、数字、空格或注释字符的任何意义,它将删除它。
    句法 :
    public void ordinaryChar(int arg)
    Parameters :
    arg : the character which is to be set as an Ordinary Character
    Return :
    void

    执行 :

    // Java Program  illustrating use of ordinaryChar() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
        FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            // Use of ordinaryChar() method 
            // Here we have taken 's' as an ordinary character
             token.ordinaryChar('s');
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
      
                }
            }
        }
    }
    

    笔记 :
    该程序不会在此处运行,因为不存在“ABC”文件。您可以在系统上的Java编译器上检查此代码。
    要检查此代码,请在您的系统上创建一个文件“ABC”。
    “ABC”文件包含:

    你好极客
    Thissss 关于
    普通字符()

    此方法已从整个 Stream 中删除 's'

    输出 :

    Word : Hello
    Word : Geek
    Word : Thi
    Word : I
    Word : zz
    Word : About
    Word : ordinaryChar

  • normalChars() : Java.io.StreamTokenizer.ordinaryChars(int low, int high)将范围内的字符- 'a 到 b' 设置为 StreamTokenizer 中的普通字符
    句法 :
    public void ordinaryChars(int low, int high)
    Parameters :
    low : lower limit of range
    high : higher limit of range
    Return :
    void

    执行 :

    // Java Program  illustrating use of ordinaryChars() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException,
        FileNotFoundException, IOException
        {
            FileReader reader = new FileReader("ABC.txt");
            BufferedReader bufferread = new BufferedReader(reader);
            StreamTokenizer token = new StreamTokenizer(bufferread);
      
            // Use of ordinaryChars() method 
            // Here we have taken low = 'a' and high = 'c' 
             token.ordinaryChars('a','c');
      
            int t;
            while ((t = token.nextToken()) != StreamTokenizer.TT_EOF)
            {
                switch (t)
                {
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number : " + token.nval);
                    break;
                case StreamTokenizer.TT_WORD:
                    System.out.println("Word : " + token.sval);
                    break;
      
                }
            }
        }
    }
    

    笔记 :
    该程序不会在此处运行,因为不存在“ABC”文件。您可以在系统上的Java编译器上检查此代码。
    要检查此代码,请在您的系统上创建一个文件“ABC”。
    “ABC”文件包含:

    你好极客


    关于
    普通字符()

    输出 :

    Word : Hello
    Word : Geeks
    Word : This
    Word : is
    Word : out
    Word : ordin
    Word : ryCh
    Word : rs