Java-Readable
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());
}
}
}
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次
Java-Readable的更多相关文章
- 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. ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 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\ ...
- JMeter学习-027-JMeter参数文件(脚本分发)路径问题:jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File distributed.csv must exist and be readable解决方法
前些天,在进行分布式参数化测试的时候,出现了如题所示的错误报错信息.此文,针对此做一个简略的重现及分析说明. JMX脚本线程组参数配置如下所示: 参数文件路径配置如下所示: 执行JMX脚本后,服务器对 ...
- Java Scanner Readable
通过implements(实现)Readbale interface(接口)的 read() method(方法) 实现自己添加字符到buffer里,然后读取 //策略模式package object ...
- 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 ...
- 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 ...
- 12.Java中Comparable接口,Readable接口和Iterable接口
1.Comparable接口 说明:可比较(可排序的) 例子:按照MyClass的y属性进行生序排序 class MyClass implements Comparable<MyClass> ...
- 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 ...
- 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 ...
随机推荐
- LeetCode 935. Knight Dialer
原题链接在这里:https://leetcode.com/problems/knight-dialer/ 题目: A chess knight can move as indicated in the ...
- learning java NIO 之 RandomFileChannel
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.i ...
- Xamarin安装及调试
Xamarin介绍 Xamarin是一个跨平台的开发框架(工具集),创始于2011年,旨在使移动开发变得难以置信地迅捷和简单,它是跨平台的,它允许开发人员有效创建可跨 iOS.Android.Wind ...
- 【区间DP】加分二叉树
https://www.luogu.org/problemnew/show/P1040#sub 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为 ...
- CH定理与线性递推
才发觉自己数学差的要死,而且脑子有点浑浑噩噩的,学了一个晚上才学会 如果说的有什么不对的可以在下面嘲讽曲明 以下无特殊说明时,默认方阵定义在实数域上,用\(|A|\)表示\(A\)的行列式 特征值与特 ...
- Django基础之redirect()
参数可以是: 一个模型: 将调用模型的get_absolute_url()函数 一个视图, 可以带有函数: 可以使用urlresolvers.reverse来反向解析名称 一个绝对的或相对的URL, ...
- 【Java 8】巧用Optional之优雅规避NPE问题
避之不及的 NullPointerException NPE : NullPointerException 空指针异常是最常见的Java异常之一,抛出NPE错误不是用户操作的错误,而是开发人员的错误, ...
- hdu1237 简单计算器[STL 栈]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1237 题干 代码和解释 解本题时使用了STL 栈,要记得使用#include<stack>. 解本题时使用了isdigit()函 ...
- 导入eclipse有Unbound classpath variable: 'M2_REPO报错的解决方法
Eclipse maven of the project reported in Unbound classpath variable: 'M2_REPO /**/***/***. jar' But ...
- Windows server 2012 英文系统 中文软件显示乱码的问题
1.安装语言包 Start -> Control Panel -> Language 如果没有中文,请点击 Add a language 添加可能需要 联网下载语言包,按照要求下载即可 ...