关于Bufferedreader的功能扩写
package cn.hncu.pattern.decorator.v1;
import java.io.FileReader;
import java.io.IOException;
public class MyBufferedReader {
private FileReader r;//封装或组合一个对象
private char[]cbuf = new char[1024];
private int count=0;//记录缓冲区中字符的个数 100
private int pos=0;
public MyBufferedReader(FileReader r){
this.r = r;
}
//功能增强1
public int myRead() throws IOException{
if(count<=0){
count = r.read(cbuf);//50
pos=0;
}
if(count==-1){
return -1;
}
count--;//每读一次,缓冲区中的还剩的字符数减1
char ch = cbuf[pos];//本次读的字符
pos++;
return ch;
}
public void close() throws IOException {
r.close();
}
//换行: linux--\n window--\r\n
public String myReadLine() throws IOException{
StringBuffer sb = new StringBuffer();
int ch=0;
while( (ch=myRead())!=-1 ){
if(ch=='\r'){
continue;
}
if(ch=='\n'){
return sb.toString();
}
sb.append( (char)ch );//字符连接 str = str+(char)ch;
}
if(sb.length()!=0){//文件的最后一行是没有换行符的
return sb.toString();
}
return null;
}
}
-----------------------------------------------------------------
package cn.hncu.pattern.decorator.v1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.junit.Test;
public class TestMyBufferedreader {
@Test
public void jdk_testRead() throws IOException {
FileReader r = new FileReader("a.txt");
BufferedReader br = new BufferedReader( r );
int ch=0;
while( (ch=br.read())!=-1 ){
System.out.print((char)ch);
}
br.close();
}
@Test
public void my_testRead() throws IOException {
FileReader r = new FileReader("a.txt");
MyBufferedReader br = new MyBufferedReader( r );
int ch=0;
while( (ch=br.myRead())!=-1 ){
System.out.print((char)ch);
}
br.close();
}
/////
@Test
public void jdk_testReadLine() throws IOException {
FileReader r = new FileReader("a.txt");
BufferedReader br = new BufferedReader( r );
String line = null;
while( (line =br.readLine())!=null ){
System.out.println(line);
}
br.close();
}
@Test
public void my_testReadLine() throws IOException {
FileReader r = new FileReader("a.txt");
MyBufferedReader br = new MyBufferedReader( r );
String line = null;
while( (line =br.myReadLine())!=null ){
System.out.println(line);
}
br.close();
}
}
关于Bufferedreader的功能扩写的更多相关文章
- ? 原创: 铲子哥 搜狗测试 今天 shell编程的时候,往往不会把所有功能都写在一个脚本中,这样不太好维护,需要多个脚本文件协同工作。那么问题来了,在一个脚本中怎么调用其他的脚本呢?有三种方式,分别是fork、source和exec。 1. fork 即通过sh 脚本名进行执行脚本的方式。下面通过一个简单的例子来讲解下它的特性。 创建father.sh,内容如下: #!/bin/bas
? 原创: 铲子哥 搜狗测试 今天 shell编程的时候,往往不会把所有功能都写在一个脚本中,这样不太好维护,需要多个脚本文件协同工作.那么问题来了,在一个脚本中怎么调用其他的脚本呢?有三种方式,分别 ...
- jremoting的功能扩展点
1 InvokeFilter,实现此接口 可以在consumer端 与provider端的调用过程中拦截住请求调用. 已经实现的InvokeFilter包括 RetryInvokeFilter:实现 ...
- 把登录和退出功能单独写到一个公共.py脚本,其它用例test1,test2调用公共登录,退出函数
公共登录/退出函数模块(login_exit.py): #coding:utf-8import timedef login(driver, username, password):#此处的driver ...
- C# CheckBoxList 实现全选/反选功能怎么写?
首先我们用RadioButtonList控件,且必须包含OnSelectedIndexChanged事件和AutoPostBack=‘true’属性, <asp:LinkButton ID=&q ...
- 【C++】【TinyXml】xml文件的读写功能使用——写xml文件
TinyXml工具是常用比较简单的C++中xml读写的工具 需要加载 #include "TinyXml\tinyxml.h" 在TinyXML中,根据XML的各种元素来定义了一些 ...
- dedecms用runphp功能,写for循环,@me输出不出来
今天在{dede:field name='typeid' runphp='yes'}中写for循环,出现@me输出不了内容,把for循环删掉之后,就可以输出.死了几十万脑细胞,没有解决,后来把循环 f ...
- 作为一个Java程序员连简单的分页功能都会写,你好意思嘛!
今天想说的就是能够在我们操作数据库的时候更简单的更高效的实现,现成的CRUD接口直接调用,方便快捷,不用再写复杂的sql,带吗简单易懂,话不多说上方法 1.Utils.java工具类中的方法 1 /* ...
- 手写一个HTTP框架:两个类实现基本的IoC功能
jsoncat: 仿 Spring Boot 但不同于 Spring Boot 的一个轻量级的 HTTP 框架 国庆节的时候,我就已经把 jsoncat 的 IoC 功能给写了,具体可以看这篇文章&l ...
- 基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写
基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写 专业程序代写服务(QQ:928900200) 随着社会的进步.服务行业的服务水平不断发展与提高,宾馆.酒店.旅游等服务行业的信息量和工作 ...
随机推荐
- C 语言函数指针
c代码: #include <stdio.h> int add(int x,int y); int subtract(int x,int y); int domath(int (*math ...
- Visual Studio 那些隐藏的调试功能(转)
原文出处: 微软互联网开发支持 Visual Studio 是一个强大的调试工具,里面很多隐藏功能少有人问津,但是在特定场景可以节省你很多时间,本文主要介绍一些Visual Studio调试相关 ...
- 学习kernel编程的建议
我把我学习kernel编程的过程介绍给大家,希望大家有个参考. 学习kernel编程需要阅读大量的kernel方面的书籍,在此我列举一下我读过的kernel书籍(按时间先后顺序),并给一些建议. 1. ...
- 【HDOJ】1238 Substrings
深搜+剪枝,简单字符串. #include <stdio.h> #include <string.h> #define MAXLEN 105 #define MAXNUM 10 ...
- Cannot Create Supplier Site (Address) (文档 ID 1069032.1)
Error Address and Site Creation - Unable to create address and sites because of the following error ...
- 【转】can't find referenced method 'android.app.RemoteInput[] getRemoteInputs()' in class android.app.Notification$Action
原文网址:http://stackoverflow.com/questions/25508735/cant-find-referenced-method-android-app-remoteinput ...
- 浅谈MS-SQL锁机制
锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 脏 ...
- 【转】git使用教程
Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是 ...
- LA4329 Ping pong 树状数组
题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...
- UVALive-4287 Proving Equivalences 有向图的强连通分量+缩点
题意:有n个命题,已知其中的m个推导,要证明n个命题全部等价(等价具有传递性),最少还需要做出几次推导. 思路:由已知的推导可以建一张无向图,则问题变成了最少需要增加几条边能使图变成强连通图.找出所有 ...