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. 写一段程序,删除字符串a中包含的字符串b,举例 输入a = "asdw",b = "sd" 返回 字符串 “aw”;一个容易被忽略的bug

    代码如下: public class test{ public static void main(String args[]){ String test=test("sahsjkshabsh ...

  2. 002——keil-Error: L6915E: Library reports error: __use_no_semihosting was requested解决

    ..\OBJ\KEY.axf: Error: L6915E: Library reports error: __use_no_semihosting was requested, but _ttywr ...

  3. tensorflow2.0 学习(三)

    用tensorflow2.0 版回顾了一下mnist的学习 代码如下,感觉这个版本下的mnist学习更简洁,更方便 关于tensorflow的基础知识,这里就不更新了,用到什么就到网上取搜索相关的知识 ...

  4. SOS.DLL在windbg里加载错误

    sos.dll/mscordacwks.dll 公共语言运行库(CLR)是执行托管代码的Microsoft.NET框架的核心引擎.简单地说,它通过在托管程序集中使用中间语言和元数据,JIT按需编译代码 ...

  5. BZOJ 3435: [Wc2014]紫荆花之恋

    二次联通门 : BZOJ 3435: [Wc2014]紫荆花之恋 二次联通门 : luogu P3920 [WC2014]紫荆花之恋 /* luogu P3920 [WC2014]紫荆花之恋 怀疑人生 ...

  6. 【loj2339】【WC2018】通道

    题目 三棵带边权的树,求 \[ dis1(u,v) + dis2(u,v) + dis3(u,v) \] 的最大值 \(1 \le n \le 10^5\) 题解 对\(T_1\)做边分治,把分治边的 ...

  7. mysql avg()函数,获取字段的平均值

    mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...

  8. Shiro安全框架-简介

    1. 简介 Apache Shiro是Java的一个安全框架.功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案. 实际上,Shiro的主要功能 ...

  9. 微信小程序之如何定义页面标题

    效果图: 这个标题是在哪里定义的呢?type.js核心代码如下(通常这段代码放在onLoad函数体内): wx.setNavigationBarTitle({ title: "支出类型列表& ...

  10. Oracle定时任务执行存储过程备份日志记录表

    写在前面 需求 1.备份系统日志表T_S_LOG, 按照操作时间字段OPERATETIME, 将每天的日志增量备份到另一张表. 思路 1.创建一张数据结构完全相同的表T_S_LOG_BAK作为备份表 ...