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. Docker部署nodejs应用并使用PM2作为守护进程

    环境:centos7.6 Docker version 18.06.0-ce mkdir /data cd /data 拉取最新keymetrics/pm2:latest-alpine镜像 docke ...

  2. python 中的tile函数,shape函数,sum函数

    1.tile函数: tile函数是模板numpy.lib.shape_base中的函数.函数的形式是tile(A,reps) A的类型几乎所有类型都可以:array, list, tuple, dic ...

  3. GITHUB常见命令

    1 常用 $ git remote add origin git@github.com:yeszao/dofiler.git # 配置远程git版本库 $ git pull origin master ...

  4. Jupyter notebook 自动补全

    Jupyter notebook 自动补全   Jupyter notebook使用默认的自动补全是关掉的.要打开自动补全,需修改默认配置.  ipython profile create 以上命令会 ...

  5. linux 出错 “INFO: task java: xxx blocked for more than 120 seconds.” 的3种解决方案

    1 问题描述 最近搭建的一个linux最小系统在运行到241秒时在控制台自动打印如下图信息,并且以后每隔120秒打印一次. 仔细阅读打印信息发现关键信息是“hung_task_timeout_secs ...

  6. Jmeter 正则表达式提取器详解(Regular Expression Exactor)

    Jmeter 正则表达式提取器详解(Regular Expression Exactor) Name(名称):随意设置,最好有业务意义. Comments(注释):随意设置,可以为空 Apply to ...

  7. vue-cli3 ios10白屏问题解决思路

    在出现了这个问题之后先不要盲目的去瞎试,根据网上的方法试了个遍也没解决问题 先看报的是什么错,再针对的解决问题 首先出现的报错是 SyntaxError: Unexpected token '*'  ...

  8. 洛谷P1902 刺杀大使

    题目 二分加广搜 #include <bits/stdc++.h> using namespace std; int n, m, l, r, p[1001][1001], vis[1001 ...

  9. 安装与配置HSDIS与JITWatch

    本作者的系统: 操作系统版本及位数可通过uname -a命令查看,如下: Linux ubuntu 3.13.0-32-generic #57~precise1-Ubuntu SMP Tue Jul ...

  10. ubuntu之路——day14 只用python的numpy在底层实现多层神经网络

    首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...