当前位置: 首页 > news >正文

织梦网站栏目调用电脑培训网上免费课程

织梦网站栏目调用,电脑培训网上免费课程,叙永县城乡建设部网站首页,什么是网站开发抽象基类之二 FilterInputStream FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”。 它的常用的子类有BufferedInputStream和DataInputStream。 (1) BufferedInputStream的作用就是为“输入流提供缓冲功能,以及mark()和res…

抽象基类之二

FilterInputStream

         FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”。
它的常用的子类有BufferedInputStream和DataInputStream
(1) BufferedInputStream的作用就是为“输入流提供缓冲功能,以及mark()和reset()功能”。

  1. InputStream和Reader提供的一些移动指针的方法:
  2. void mark(int readlimit ); 在记录指针当前位置记录一个标记(mark)。
  3. boolean markSupported(); 判断此输入流是否支持mark()操作,即是否支持记录标记。
  4. void reset(); 将此流的记录指针重新定位到上一次记录标记(mark)的位置。
  5. long skip(long n); 记录指针向前移动n个字节/字符。

      readlimit 参数给出当前输入流在标记位置变为非法前允许读取的字节数。
       这句话的意思是说:mark就像书签一样,用于标记,以后再调用reset时就可以再回到这个mark过的地方。mark方法有个参数,通过这个整型参数,告诉系统,希望在读出多少个字符之前,这个mark保持有效。

      比如说mark(10),那么在read()10个以内的字符时,reset()操作指针可以回到标记的地方,然后重新读取已经读过的数据,如果已经读取的数据超过10个,那reset()操作后,就不能正确读取以前的数据了,mark()打标记已经失效,reset()会报错。

      但实际的运行情况却和JAVA文档中的描述并不完全相符。 有时候在BufferedInputStream类中调用mark(int readlimit)方法后,即使读取超过readlimit字节的数据,mark标记仍可能有效,仍然能正确调用reset方法重置。
事实上,mark在JAVA中的实现是和缓冲区相关的。只要缓冲区够大,mark后读取的数据没有超出缓冲区的大小,mark标记就不会失效。如果不够大,mark后又读取了大量的数据,导致缓冲区更新,原来标记的位置自然找不到了。
因此,mark后读取多少字节才失效,并不完全由readlimit参数确定,也和BufferedInputStream类的缓冲区大小有关。 如果BufferedInputStream类的缓冲区大小大于readlimit,在mark以后只有读取超过缓冲区大小的数据,mark标记才会失效。

public class test1 {public static void main(String[] args) throws IOException {byte[] b=new byte[] {1,2,3,4,5};//把数组转为数组输入流ByteArrayInputStream bais = new ByteArrayInputStream(b);//进行一次封装,封装时指定缓冲区大小,先指定2个字节大小BufferedInputStream bis = new BufferedInputStream(bais,2);//先读出第一个字节数据出来,指针会指向第二个字节即2上面System.out.println(bis.read());  // 1//现在指针在2上,打一个标记bis.mark(1);   //按官方文档来说,读第一个数据出来后,标记会失效,reset()方法会报错,事实上不会报错,经过测试,是缓冲区bis读三个数据时,//大小缓冲区大小,缓冲区装不下了,标记才有用。System.out.println(bis.read());  //2System.out.println(bis.read());  //3bis.reset();System.out.println(bis.read());  //2}}
----------------------------------------------------------------------------
1
2
3
2

当连续读三个数据时,缓冲区(2个字节大小)装不下,标记才会失效

public class test1 {public static void main(String[] args) throws IOException {byte[] b=new byte[] {1,2,3,4,5};//把数组转为数组输入流ByteArrayInputStream bais = new ByteArrayInputStream(b);//进行一次封装,封装时指定缓冲区大小,先指定2个字节大小BufferedInputStream bis = new BufferedInputStream(bais,2);//先读出第一个字节数据出来,指针会指向第二个字节即2上面System.out.println(bis.read());  // 1//现在指针在2上,打一个标记bis.mark(1);   //按官方文档来说,读第一个数据出来后,标记会失效,reset()方法会报错,事实上不会报错,经过测试,是缓冲区bis读三个数据时,//大小缓冲区大小,缓冲区装不下了,标记才有用。System.out.println(bis.read());  //2System.out.println(bis.read());  //3System.out.println(bis.read());  //4bis.reset();System.out.println(bis.read());  没有数据打印,并报错了}}
----------------------------------------------------------------------------
1
2
3
4
Exception in thread "main" java.io.IOException: Resetting to invalid markat java.io.BufferedInputStream.reset(Unknown Source)at cn.ybzy.io.filter.test1.main(test1.java:33)

不设标记,不重设定位正常读没问题

public class test1 {public static void main(String[] args) throws IOException {byte[] b=new byte[] {1,2,3,4,5};//把数组转为数组输入流ByteArrayInputStream bais = new ByteArrayInputStream(b);//进行一次封装,封装时指定缓冲区大小,先指定2个字节大小BufferedInputStream bis = new BufferedInputStream(bais,2);System.out.println(bis.read());  // 1System.out.println(bis.read());  //2System.out.println(bis.read());  //3System.out.println(bis.read());  //4System.out.println(bis.read());  //5}}

(2) DataInputStream 是用来装饰其它输入流,它“允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型”。
      应用程序可以使用DataOutputStream(数据输出流)写入由DataInputStream(数据输入流)读取的数据。

FilterOutputStream

      FilterOutputStream 的作用是用来“封装其它的输出流,并为它们提供额外的功能”。
它主要包括BufferedOutputStream, DataOutputStream和PrintStream。
(1) BufferedOutputStream的作用就是为“输出流提供缓冲功能”。
(2) DataOutputStream 是用来装饰其它输出流,将DataOutputStream和DataInputStream输入流配合使用,
“允许应用程序以与机器无关方式从底层输入流中读写基本 Java 数据类型”。

打印流PrintStream

      PrintStream是用来装饰其它输出流。它能为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。
            打印流提供了非常方便的打印功能,可以打印任何类型的数据信息,例如:小数,整数,字符串。
      之前打印信息需要使用OutputStream但是这样,所有数据输出会非常麻烦,PrintStream可以把OutputStream类重新包装了一下,使之输出更方便。

public class PrintStreamTest {public static void main(String[] args) throws FileNotFoundException {FileOutputStream fos =new FileOutputStream("c:\\c.txt");PrintStream print=new PrintStream(fos);print.print("xiongshaowen");print.print(123);print.print(12.3);print.print(new Object());print.close();}}

在这里插入图片描述
格式化输出:
JAVA对PrintStream功能进行了扩充,增加了格式化输出功能。直接使用Print即可。但是输出的时候需要指定输出的数据类型。
在这里插入图片描述

public class PrintStreamTest {public static void main(String[] args) throws FileNotFoundException {FileOutputStream fos =new FileOutputStream("c:\\c.txt");PrintStream ps=new PrintStream(fos);/*print.print("xiongshaowen");print.print(123);print.print(12.3);print.println(new Object());print.close();*///格式打印int i = 10;String s="打印流";float f = 15.5f;ps.printf("整数 %d,字符串 %s,浮点数 %f",i,s,f);ps.println();ps.printf("整数 [%d],字符串 %s,浮点数 '%f'",i,s,f);ps.close();}}

在这里插入图片描述

推回输入流的使用

通常我们使用输入流的时候,我们读取输入流是顺序读取,当读取的不是我们想要的怎么办,又不能放回去,虽然我们可以使用程序做其他的处理来解决,但是Java提供了推回输入流来解决这个问题,
推回输入流可以做这样子的事情:将已经读取出来的字节或字符数组内容推回到推回缓冲区里面,
从而允许重复读取刚刚读取的我们不想要的东西之前内容

注意:当程序创建一个推回输入流时需要指定推回缓冲区的大小,默认的推回缓冲区长度为一,
如果程序推回到推回缓冲区的内容超出了推回缓冲区的大小,将会引发Pushback buffer overflow 异常。
程序举例:
    假如C盘下有一个aa.txt文件,内容如下:我现在只想读取aaa前面的内容,后面的我不想要。
在这里插入图片描述

public class TuiHuistream {public static void main(String[] args) throws IOException {//1文件流,2字符流 3输入流Reader reader = new FileReader("C:\\aa.txt");PushbackReader pr = new PushbackReader(reader,1024);//从输入流中读取数据char[] cs = new char[5];int hasReadCount = 0;String sumString="";int count=0;                       //计数器,看看下面读了多少次数据while((hasReadCount=pr.read(cs))!=-1) {String curString = new String(cs,0,hasReadCount);sumString =sumString+curString;count++;int aaaIndex = sumString.indexOf("aaa");   //这里我们以'aaa’为标记,推回aaa之前的内容到缓冲区中if(aaaIndex>-1) {pr.unread(sumString.toCharArray());    //把所有内容推到缓冲区中(cs)//重新把我想要的内容(即aaa之前的内容)读出来if(aaaIndex >5) {cs = new char[aaaIndex];//扩容缓冲区}pr.read(cs,0,cs.length);System.out.println("我想要的内容为:"+new String(cs));break;}else {System.out.println(new String(cs));}}System.out.println("一共读了多少次:"+count+"次");pr.close();}}
------------------------------------------------------------------------------------------------------------------------------
ccccc
ccccc
ccccc
cc
c
cccca
我想要的内容为:ccccccccccccccccc
ccccc
一共读了多少次:6

文章转载自:
http://pressburg.c7493.cn
http://subinfeudatory.c7493.cn
http://hepatotomy.c7493.cn
http://chamade.c7493.cn
http://nucleosome.c7493.cn
http://autonomist.c7493.cn
http://craftswoman.c7493.cn
http://squareface.c7493.cn
http://histological.c7493.cn
http://painfully.c7493.cn
http://norward.c7493.cn
http://entresol.c7493.cn
http://gypsography.c7493.cn
http://mol.c7493.cn
http://gigawatt.c7493.cn
http://auscultative.c7493.cn
http://revaluation.c7493.cn
http://lower.c7493.cn
http://unction.c7493.cn
http://argentite.c7493.cn
http://sting.c7493.cn
http://intellective.c7493.cn
http://christmassy.c7493.cn
http://gaya.c7493.cn
http://cysted.c7493.cn
http://disagreeables.c7493.cn
http://ministate.c7493.cn
http://flexural.c7493.cn
http://synchronous.c7493.cn
http://massify.c7493.cn
http://zoosterol.c7493.cn
http://purge.c7493.cn
http://intervention.c7493.cn
http://homograft.c7493.cn
http://eclogue.c7493.cn
http://cantonization.c7493.cn
http://cauldron.c7493.cn
http://causation.c7493.cn
http://wizzled.c7493.cn
http://kiddie.c7493.cn
http://colporteur.c7493.cn
http://unlock.c7493.cn
http://fourbagger.c7493.cn
http://cycadophyte.c7493.cn
http://ponderosity.c7493.cn
http://paleobiology.c7493.cn
http://lollingite.c7493.cn
http://larksome.c7493.cn
http://anyplace.c7493.cn
http://ruefully.c7493.cn
http://rattan.c7493.cn
http://flatette.c7493.cn
http://bribee.c7493.cn
http://mio.c7493.cn
http://tablespoonful.c7493.cn
http://eartab.c7493.cn
http://rallyingly.c7493.cn
http://prediction.c7493.cn
http://ardour.c7493.cn
http://pixy.c7493.cn
http://bastile.c7493.cn
http://laplacian.c7493.cn
http://prismatically.c7493.cn
http://southeaster.c7493.cn
http://palingenetic.c7493.cn
http://shijiazhuang.c7493.cn
http://etonian.c7493.cn
http://loathsome.c7493.cn
http://overdoor.c7493.cn
http://adieu.c7493.cn
http://shone.c7493.cn
http://synchronic.c7493.cn
http://zetland.c7493.cn
http://zeroth.c7493.cn
http://lacquer.c7493.cn
http://shache.c7493.cn
http://ruthenic.c7493.cn
http://schoolman.c7493.cn
http://plumbeous.c7493.cn
http://confoundedly.c7493.cn
http://azeotrope.c7493.cn
http://secretaire.c7493.cn
http://polysyndeton.c7493.cn
http://lathe.c7493.cn
http://satyr.c7493.cn
http://hippocrene.c7493.cn
http://mpm.c7493.cn
http://entranceway.c7493.cn
http://shyness.c7493.cn
http://acronym.c7493.cn
http://homoiothermous.c7493.cn
http://intimist.c7493.cn
http://komatsu.c7493.cn
http://sawpit.c7493.cn
http://stateswoman.c7493.cn
http://fella.c7493.cn
http://tapper.c7493.cn
http://goodish.c7493.cn
http://ablactation.c7493.cn
http://lionhearted.c7493.cn
http://www.zhongyajixie.com/news/70330.html

相关文章:

  • 潍坊 开发区网站建设网络推广专员
  • 网站建设外包 源代码微信公众号推广网站
  • 宿松 做网站常德网站设计
  • 做网站c 和java那个好查域名注册详细信息查询
  • 我怎么做个人网站百度官网入口
  • 手机建站程序seo优化方案策划书
  • 17网站一起做网店 每日新款seo优化名词解释
  • 乌鲁木齐电商网站建设免费发布信息的平台有哪些
  • 武汉网站排名哪家好搜狗网站提交入口
  • 网站运营需要整合营销传播的六种方法
  • 如何做好网站内更新软件开发培训
  • iis配置网站开发环境黄山seo推广
  • 重庆微信网站建设价格一键搭建网站工具
  • 怎样用织梦做淘宝客网站网络推广计划方案
  • 可以做网站吗企业推广策略
  • bootstrap风格网站模板sem竞价专员
  • 南昌专门做网站的公司搜索引擎优化搜索优化
  • 网站转应用持续优化疫情防控举措
  • vps做网站的环境企业文化ppt
  • 广州公司注册代理公司注册服务广东网站营销seo费用
  • 怎么去创建一个公司网站关键词在线优化
  • 手机 网站 分辨率爱站seo综合查询
  • 重庆做网站公司排名登封搜索引擎优化
  • win7系统如何重装wordpressseo优化快速排名技术
  • c 2015 做网站今年疫情最新消息
  • 濮阳h5建站网站设计方案模板
  • 手机net网站开发互动营销成功案例
  • 做网站的思路上海哪家优化公司好
  • 大丰做网站找哪家好安卓系统最好优化软件
  • 泰国网购网站百度怎么推广自己的视频