StreamTokenizer类nextToken()方法 (StreamTokenizer Class nextToken() method)

  • nextToken() method is available in java.io package.

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

  • nextToken() method is used to parse the next token from the input stream of this StreamTokenizer and the type of the next token is returned in the ttype field.

    nextToken()方法用于解析此StreamTokenizer的输入流中的下一个标记,并且下一个标记的类型在ttype字段中返回。

  • nextToken() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

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

  • nextToken() method may throw an exception at the time of returning next token.

    nextToken()方法在返回下一个令牌时可能会引发异常。

    IOException: This exception may throw when getting any input/output error while performing.

    IOException:在执行过程中遇到任何输入/输出错误时,可能引发此异常。

Syntax:

句法:

    Public int nextToken();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of the method is int, it gets the value of ttype field (i.e. type of the next token).

该方法的返回类型为int ,它获取ttype字段的值(即下一个标记的类型)。

Example:

例:

// Java program to demonstrate the example
// of int nextToken() method of StreamTokenizer
import java.io.*;
public class NextToken {public static void main(String[] args) {String str = "Hi, This is a mathematical expression : \n" +
" 2 * 4 = 8" + "8 + 5 = 13";
try {// Instantiates FileOutputStream  and ObjectOutputStream
FileOutputStream fos_stm = new FileOutputStream("D:\\includehelp.txt");
ObjectOutputStream obj_out_stm = new ObjectOutputStream(fos_stm);
// By using writeUTF() method is to
// write the given string in the file
obj_out_stm.writeUTF(str);
obj_out_stm.flush();
// Instantiates FileOutputStream  and ObjectOutputStream
ObjectInputStream obj_in_stm = new ObjectInputStream(new FileInputStream("D:\\includehelp.txt"));
// Instantiates StreamTokenizer and Reader
Reader reader = new BufferedReader(new InputStreamReader(obj_in_stm));
StreamTokenizer st = new StreamTokenizer(reader);
// Here, we are considering initially
// file is not empty
boolean end_of_file = false;
while (!end_of_file) {// By using nextToken() method is to
// 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();
}
}
}

Output

输出量

= Found.
word: Hi
, Found.
word: This
word: is
word: a
word: mathematical
word: expression
: Found.
number: 2.0
* Found.
number: 4.0
= Found.
number: 88.0
+ Found.
number: 5.0
= Found.
number: 13.0
End of File Found

翻译自: https://www.includehelp.com/java/streamtokenizer-nexttoken-method-with-example.aspx

Java StreamTokenizer nextToken()方法与示例相关推荐

  1. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  2. java user directory,Java ProcessBuilder directory()方法与示例

    语法:public File directory (); public ProcessBuilder directory (File dir); ProcessBuilder类directory()方 ...

  3. Java序列化魔术方法及其示例使用

    在上一篇文章中, 您需要了解有关Java序列化的所有知识 ,我们讨论了如何通过实现Java序列化来启用类的可序列化性. Serializable接口. 如果我们的类未实现Serializable接口, ...

  4. catalog java,Java Connection getCatalog()方法与示例

    通常,目录是一个目录,其中包含有关数据集,文件或数据库的信息.而数据库目录中包含所有数据库,基本表,视图(虚拟表),同义词,值范围,索引,用户和用户组的列表. Connection接口的getCata ...

  5. filepermission java,Java FilePermission getActions()方法与示例

    FilePermission类getActions()方法getActions()方法在java.io包中可用. getActions()方法用于检查此FilePermission和给定对象在路径名和 ...

  6. java方法参数Bundle,Java ResourceBundle keySet()方法及示例

    ResourceBundle类keySet()方法keySet()方法在java.util包中可用. keySet()方法用于从此ResourceBundle及其超级捆绑包中获取所有现有键,以在Set ...

  7. java exec waitfor,Java Process waitFor()方法与示例

    流程类waitFor()方法在java.lang包中提供了waitFor()方法. waitFor()方法用于使当前正在运行的线程在需要时等待,直到由该Process对象表示的进程完成其终止为止. 当 ...

  8. java arraylist.add(),Java ArrayList add()方法与示例

    ArrayList类add()方法 语法:public boolean add(T ele); public void add(int indices, T ele);add()方法在java.uti ...

  9. java rollback用法,Java Connection rollBack()方法与示例

    回滚操作将撤消当前事务所做的所有更改,即,如果调用Connection接口的rollBack()方法,则所有修改都将还原到最后一次提交. 您还可以通过将所需的Savepoint对象作为参数传递给此方法 ...

最新文章

  1. 邓西百度网盘多帐号文件一键搜索工具
  2. python微控制器编程从零开始-单片机可以使用Python语言来控制了!
  3. 被动信息收集之RECON-NG(七)
  4. OC_KVC与KVO简单介绍
  5. ajax 跨站返回值,jquery ajax 跨域问题
  6. eclipse web项目页面显示404_404 Not Found错误页面是什么?
  7. 栈2——局部变量要初始化的原因
  8. VS2017 15.4提供预览版,面向Windows 10秋季更新(FCU)
  9. php分片数据库,4、PHP测试分片集群
  10. Processing 椭圆运动模拟
  11. java servlet容器有哪些_Java Web —— servlet 容器
  12. 有奖推荐|BSRC发布IoT安全专家招募令
  13. js怎么识别图片中的文字,js图片文字识别代码
  14. 怎样用excel剔除异常数据_注意避坑 | 这10个错误的Excel使用方法别再用了!
  15. (金融入门知识点)Double类型丢失精度
  16. 不联网的计算机需要杀毒吗,杀毒软件不联网可以杀毒吗?
  17. html5小游戏(12款)
  18. js新框架 svelte
  19. 使用再生龙clonezilla对win10和ubuntu16的双系统备份与还原
  20. PJblog皮肤模版制作说明-皮肤结构CSS

热门文章

  1. duino例程 stm32_stm32duino
  2. 运维人员mysql如何访问_mysql 运维常见操作
  3. java 相对路径获取_在java项目中通过相对路径获取资源的方式
  4. php 下拉菜单多选get,Jquery实现select二级联动多选下拉菜单
  5. c++用牛顿法开多次根_望远镜的历史之三:大神出世,改变望远镜历史的竟然是牛顿...
  6. Angular之ngx-permissions的路由使用
  7. Problem E: 高于均分的学生
  8. 如何优化 App 的启动耗时?
  9. Gram matrix 格拉姆矩阵
  10. Android Studio导入工程的正确姿势