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. es6 添加事件监听

    //定义被侦听的目标对象 }; //定义处理程序 var interceptor = { set: function (receiver, property, value) { console.log ...

  2. 走,去出海,一起“Copy to World” | 36氪出海行业报告

    http://www.sohu.com/a/200845344_114778 从工具类产品在海外聚集大量流量到新闻.社交游戏等内容类产品在海外取得优异成绩,中国正在完成从Copy to China向C ...

  3. 微软安全技术Shim

    Shim是微软系统中一个小型函数库,用于透明地拦截API调用,修改传递的参数.自身处理操作.或把操作重定向到其他地方.Shim主要用于解决遗留应用程序在新版Windows系统上的兼容性问题,但Shim ...

  4. zabbix值显示的问题

    虽然在创建监控项的时候,是可以选值类型的,目前有的是整型,浮点型,日志,文本,字符串.但是不要误认为zabbix采集数据的时候就是按照这个格式采集的. zabbix各种接口采集到的数据都是字符串类型, ...

  5. 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

    00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...

  6. tomcat9源码导入idea

    maven部署 下载源码 tomcat最新版的github地址 tomcat9官网下载 步骤 源码根目录新建 home 文件夹 把 conf 文件夹和 webapps 文件夹移动到 home 文件夹 ...

  7. Java-根据经纬度计算距离(百度地图距离)

    最近碰到一个需求,需要根据两个点的经纬度查询两点的距离.感觉以后还会用到,所以小记一波. 第一步:添加Maven依赖. <dependency> <groupId>org.ga ...

  8. MSSQL手工注入 报错注入方法

    例子:www.kfgtfcj.gov.cn/lzygg/Zixun_show.aspx?id=1[1]首先爆版本:http://www.kfgtfcj.gov.cn/lzygg/Zixun_show. ...

  9. EasyEarth三维可视化解决方案——智慧河长

    EasyEarth—— 为河长装上“千里眼.顺风耳” 为各级河长办应急指挥.任务指派. 实绩考核提供快速直观的 高效.精准.智能化决策平台. 河长制背景 我国治水工作呈现出新老问题交织态势,河湖管理保 ...

  10. 用Visual Studio编写UDF的一点小技巧(二)