博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义buffered缓存流
阅读量:2396 次
发布时间:2019-05-10

本文共 2012 字,大约阅读时间需要 6 分钟。

自定义字符缓存流

import java.io.*;class mybuffered{	private FileReader fr = null;	mybuffered(FileReader fr)	{		this.fr = fr ;	}	public String mynewline()throws IOException	{		StringBuilder sb = new StringBuilder();		int demo = 0;		while ((demo = fr.read())!=-1)  //多次返回之后指针还是会自动改变的		{			if (demo == '\r')           //int 和 char 之间是会自动匹配的				continue;			if (demo == '\n')				return sb.toString();			sb.append((char)demo);		}		if (sb.length()!=0)		{			return sb.toString();		}		return null ;			}	public void myclose()throws IOException	{		fr.close();	}}public class anli{	public static void main (String[] args)throws IOException	{		FileReader fr = new FileReader("demo.txt");		mybuffered mf = new mybuffered(fr);		String str = null;		while ((str = mf.mynewline())!=null)		{			sop(str);		}		mf.myclose();	}	public static void sop (Object obj)	{		System.out.println(obj);	}}
自定义字节缓存流

import java.io.*;class mybufferedinputstream{	private FileInputStream in ;	private int pos = 0,count = 0;  //在java中,类中可以初始化,这点和C++不一样	private byte[] bt = new byte[1024];	mybufferedinputstream(FileInputStream in)	{		this.in = in ;	}	public void myclose()throws IOException //只需要在这里抛出异常就可以了	{                 //这里用的是public,因为是公共部分给人调用的。。。		in.close();   //类中不可以调用自己类中的函数,除非是定义为静态的,就好像是主函数那样子	}	public int myread ()throws IOException  //返回值是byte,而返回类型确实int,其中的含义	{		if (count==0)		{			count = in.read(bt);			if (count < 0)			{				return -1;			}			pos = 0;  //记得把pos恢复为0			byte term = bt[pos];			count--;			pos++;			return term&255;   //255的含义		}		else if (count > 0)		{			byte term = bt[pos];  //存放的是字节			count--;			pos++;			return term&255;		}		return -1;	}}public class anli{	public static void main (String[] args)throws IOException	{		FileInputStream fi = new FileInputStream ("anli.java");		mybufferedinputstream ms = new mybufferedinputstream(fi);		int term = 0;		while ((term = ms.myread())!=-1)		{			sop((char)term);		}	}	public static void sop(Object obj)	{		System.out.print(obj);	}}

转载于:https://my.oschina.net/u/2356176/blog/466562

你可能感兴趣的文章
Ubuntu 12.04安装Jetty
查看>>
Ubuntu 12.04安装OpenCV
查看>>
K-D树 C++实现
查看>>
搜索引擎的预料库 —— 万恶的爬虫
查看>>
网易工程师 Ruheng 一文教你轻松学会 Git
查看>>
文字与编码的奥秘(下)
查看>>
阿里分布式事务框架 GTS 全解析
查看>>
轻量级 Web 框架 Gin 结构分析
查看>>
一个字节的网络漫游故事独白
查看>>
RabbitMQ 消息可靠性、延时队列以及高可用集群
查看>>
分布式系统的可靠性指的是什么 —— 你可能从来就没有认真思考过
查看>>
布隆过滤器过时了,未来属于布谷鸟过滤器?
查看>>
面试题 —— 数字幻方
查看>>
5折抢购最后一天 | 戴尔顶级配置电脑,限时秒!
查看>>
SpringBoot 究竟是如何跑起来的?
查看>>
阿里开源限流组件 Sentinel 集群流控全解析
查看>>
深度解密HTTP通信细节
查看>>
日活亿级用户的服务器架构要怎么搭?
查看>>
MySQL 是怎样运行的:从根儿上理解 MySQL
查看>>
开源搜索技术的核心引擎 —— Lucene
查看>>