import java.nio.*;
import java.util.*;
import static net.mindview.util.Print.*; public class RandomWords implements Readable {
private int readCount = 1;//read()执行次数
private int loopCount = 1;
public RandomWords() { }
public int read(CharBuffer cb) { if (loopCount == 1){print("进入read执行第" + readCount + "次");readCount++;cb.append("aa bb cc ");loopCount++;return 1;}//仅仅在第一次和第六次给CharBuffer添加3个数据
if (loopCount == 2){print("进入read执行第" + readCount + "次");readCount++;loopCount++;return 1;}//其他的时候返回1,但是并不往Buffer中存放数据,来骗被调用者hasNext(),来观察出现了什么现象
if (loopCount == 3){print("进入read执行第" + readCount + "次");readCount++;loopCount++;return 1;}
if (loopCount == 4){print("进入read执行第" + readCount + "次");readCount++;loopCount++;return 1;}
if (loopCount == 5){print("进入read执行第" + readCount + "次");readCount++;loopCount++;return 1;} if (loopCount == 6){print("进入read执行第" + readCount + "次");readCount++;cb.append("oo pp qq ");loopCount++;return 1;}
if (loopCount == 7){print("进入read执行第" + readCount + "次");readCount++;loopCount++;return 1;}
if (loopCount == 8){print("进入read执行第" + readCount + "次");readCount++;loopCount++;return 1;}
if (loopCount == 9){print("进入read执行第" + readCount + "次");readCount++;loopCount++;return 1;}
if (loopCount == 10){print("进入read执行第" + readCount + "次");readCount++;return -1;}
return 1;
} public static void main(String[] args) {
int a = 1;//外层循环的次数 print("1");
Scanner s = new Scanner(new RandomWords())
print("2"); while(s.hasNext()){//这个hasNext()方法会调用read()方法的
print("第" + a + "次执行外层循环");
a ++;
System.out.println("从cb中读出 " + s.next());
}
}
}
上面的执行结果是:
1
2
进入read执行第1次
第1次执行外层循环
从cb中读出 aa
第2次执行外层循环
从cb中读出 bb
第3次执行外层循环
从cb中读出 cc
进入read执行第2次
进入read执行第3次
进入read执行第4次
进入read执行第5次
进入read执行第6次
第4次执行外层循环
从cb中读出 oo
第5次执行外层循环
从cb中读出 pp
第6次执行外层循环
从cb中读出 qq
进入read执行第7次
进入read执行第8次
进入read执行第9次
进入read执行第10次
  可以看到,在执行到while(s.hasNext())时,要判断是否输入了内容,此时要进行读取操作,调用read()函数,执行 loopCount == 1 得到了返回值为1,因此程序判断有输入内容,同时 cb.append("aa bb cc ")已经执行,由于s.hasNext()是以空格为间断符
第一次只能读aa,后面的在cd外排序等待,读完之后执行print(),然后由于一次性给过来的是aa bb cc。因此接着读取bb。最后cc。
继续执行 loopCount == 2 此时返回值仍为1,但是s.hasNext()并未检测到有具体内容要读取进来,因此继续调用read()函数,直到s.hasNext()检测到返回值为-1为止,或者有读取内容。
 
 

Java-Readable的更多相关文章

  1. Google Guava vs Apache Commons for Argument Validation

    It is an established good practice to validate method arguments at the beginning of the method body. ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. tomcat报错java.lang.IllegalArgumentException: Document base XXXXX does not exist or is not a readable directory

    启动tomcat的时候报如下错误: java.lang.IllegalArgumentException: Document base F:\java\tools\tomcat\me-webapps\ ...

  4. JMeter学习-027-JMeter参数文件(脚本分发)路径问题:jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File distributed.csv must exist and be readable解决方法

    前些天,在进行分布式参数化测试的时候,出现了如题所示的错误报错信息.此文,针对此做一个简略的重现及分析说明. JMX脚本线程组参数配置如下所示: 参数文件路径配置如下所示: 执行JMX脚本后,服务器对 ...

  5. Java Scanner Readable

    通过implements(实现)Readbale interface(接口)的 read() method(方法) 实现自己添加字符到buffer里,然后读取 //策略模式package object ...

  6. java.lang.IllegalArgumentException:Document base ……does not exist or is not a readable directory错误的解决方案

    关于Tomcat的 Document base ……does not exist or is not a readable directory错误 java.lang.IllegalArgumentE ...

  7. tomcat启动报错:java.lang.IllegalArgumentException: Document base D:\apache-tomcat-6.0.45\webapps\activiti-explorer does not exist or is not a readable directory

    java.lang.IllegalArgumentException: Document base D:\apache-tomcat-6.0.45\webapps\erp does not exist ...

  8. 12.Java中Comparable接口,Readable接口和Iterable接口

    1.Comparable接口 说明:可比较(可排序的) 例子:按照MyClass的y属性进行生序排序 class MyClass implements Comparable<MyClass> ...

  9. Error starting static Resources java.lang.IllegalArgumentException: Document base D:\Program Files\apache-tomcat-xxx\webapps\xxx does not exist or is not a readable directory

    网上的答案大多数如下: 但并没有解决我的问题  经过我的观察: 在tomcat的server.xml有Lottery项目描述,但实际上,该项目已被我删除,不存在于webapps中了    该行Cont ...

  10. java.lang.IllegalArgumentException: Document base XXX does not exist or is not a readable directory解决方法

    一.配置Eclipse,部署项目 1.双击打开Tomcat设置页面 2.选择Modules模式 3.选择Add External Web Module.. (1)Document base:选择htd ...

随机推荐

  1. 10 分钟了解 Actor 模型

    http://www.moye.me/2016/08/14/akka-in-action_actor-model/ 过去十几年CPU一直遵循着摩尔定律发展,单核频率越来越快,但是最近这几年,摩尔定律已 ...

  2. STM32F103 串口-IAP程序升级

    STM32F103 串口-IAP程序升级 通常情况下我们给STM32系列的单片机烧录程序文件的时候,使用SWD.J-link或者通过设置BOOT引脚后,使用串口进行程序下载,这样的方式直接一次性将程序 ...

  3. JavaScript Array 對象

    JavaScript array 對象 array對象,是用於在單個變量中存儲多個值的一種變量類型. 創建array對象的語法: new array(); new array(size); new a ...

  4. 4、NameNode启动过程详解

    NameNode 内存 本地磁盘 fsimage edits 第一次启动HDFS 格式化HDFS,目的就是生成fsimage start NameNode,读取fsimage文件 start Data ...

  5. 宽带DOA估计方法

    Wideband DOA Estimation. 语音信号以及野外的车辆信号的声音都是宽带信号,所以传统的窄带DOA算法(MUSIC,ESPRIT等)都不适用.需要采用宽带DOA算法来计算目标信号的波 ...

  6. 关于html异步加载外部json文件报错问题

    一. HTML代码如下: 参考网站(echarts-JSON请求数据):https://blog.csdn.net/you23hai45/article/details/51585506 <!D ...

  7. cnetos6.5安装Varnish

    安装依赖包: tar -zxvf docutils-0.13.1.tar.gz python setup.py install unzip pcre2-10.23.zip ./configure -- ...

  8. 帝国cms更换Ueditor编辑器上传图片加水印

    Ueditor安装包,里面有个/php/文件夹,找到Uploader.class.php,这是通用上传类文件找到private function upFile(),这是上传文件的主处理方法,找到122 ...

  9. SpringBoot框架 之 Druid与Swagger2

    目录 Druid Druid连接池配置 Druid数据监控 集成Swagger2 Swagger2简介 1.添加依赖 2.创建Swagger2配置类 3.在控制器方法上添加对应api信息 4.启动Sp ...

  10. Mybatis(下)

    Mybatis(下) 一.MaBatis核心配置文件 Mybatis 中文文档 Mybatis 中文文档 1. properties 定义属性及读取属性文件,取的时候用 $(name) ,name 为 ...