Java Java类设置 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
句法 :
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
句法 :
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
如果 '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
句法 :
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
句法 :
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
句法 :
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
句法 :
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