关于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) 随着社会的进步.服务行业的服务水平不断发展与提高,宾馆.酒店.旅游等服务行业的信息量和工作 ...
随机推荐
- 【HDOJ】4400 Mines
(1) KD树,但实际没有STL快,3000+ /* 4400 */ #include <iostream> #include <string> #include <ma ...
- Awesomplete 屌爆了
自动完成,功能强大! 具体请参考 http://leaverou.github.io/awesomplete
- phpstrom 与 xdebug 配合实现PHP单步调试
不说废话,直接开始. 第一步: 安装并配置xdebug 安装 可以从官网直接下载对应php版本的xdebug,下载地址: https://xdebug.org/download.php 配置,典型的 ...
- CMD命令窗口复制与粘贴
cmd命令提示符窗口中快速复制粘贴的方法常规方法 在“命令提 示符”窗口的任意一处,点击右键,在弹出的快捷菜单中选择“标记”命令. 此时在窗口的左上角处闪烁着一个长方块状的光标,将鼠标移动到希望复制的 ...
- BZOJ_1007_ [HNOI2008]_水平可见直线_(单调栈+凸包)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1007 给出一些直线,沿着y轴从上往下看,能看到多少条直线. 分析 由于直线相交,会遮挡住一些直 ...
- BZOJ3166: [Heoi2013]Alo
3166: [Heoi2013]Alo Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 394 Solved: 204[Submit][Status] ...
- android 欢迎界面的淡入效果
package com.example.spinnertest; import android.app.Activity; import android.content.Intent; import ...
- C# VS2010中,用微软自带的System.Data.OracleClient来连接Oracle数据库
由于微软在.Net框架4.0中已经决定撤销使用System.Data.OracleClient,造成在VS2010中无法连接Oracle数据库,但它还依旧存在于.Net架构中,我们可以通过自己引用 C ...
- WCF入门(一)——基本知识
构建一个WCF程序通常分为三个部分:服务类(Server).宿主(Host).客户程序(Client).有一个很重要的程序集(System.ServeiceModel)要引用,它包含WCF的核心功能. ...
- Mysql常用操作记录
在linux平台中相关的MySql操作 打开Mysql mysql -uroot -p //-u后边为用户名,-p后边为密码 1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql& ...