微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java InputStream的多种使用详解

这篇文章主要介绍了Java InputStream的多种使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

以前写东西,尤其是网络传输方面总会碰到将某种格式的文本或者图片等转幻成数据流的方式来传输,那时候用的就直接网上找点就粘贴,也没什么搞懂到底是怎么个机理。后来抽点空就死啃了点这方面的文章,稍微懂了点,特意分享一下。

InputStream  FileInputStream BufferInputStream  InputStreamreader ByteArrayInputStream这些东西到底什么关系呢?

一、首先我先理解InputStream是老大,剩下的这些都是为其服务的,先建立一个标准,而FileInputStream是其子类。他可以对文件进行数据流的转换。

String fileName = "E:\电影\[高清电影]"; InputStream inputstream = new FileInputStream("fileName");//然后对InputStream 进行读操作,为啥是读呢?可以把内存当作主体,这是某个网友说的,你从硬盘往内存里Input 东西就是读取数据咯。 另外这里因为FileInputStream继承InputStream 类//所以可以这样用 byte[] by = new byte[8192];//此数字不唯一哦; int len ; while( (len=inputStream.read(by))!=-1 ){ //len就是得出的字节流了。 } inputStream.close();//最后别忘记关闭,当然应该还有个if判断是否为空和try catch 的语句。 File f = new File("F:\……"); if (!f.exists()) { System.out.println("creat " + f.toString()); f.createNewFile(); } FileOutputStream fos = new FileOutputStream(f);//InputStream和OutputStream 一般辩证的来看,一个读,一个写,两者差不多的用法。 fos.write(b, 0, len); fos.flush(); fos.close();

二、在接下来说BufferInputStream,他是数据流缓存的东西,不带缓冲的操作,每读一个字节就要写入一个字节,由于涉及磁盘的IO操作相比内存的操作要慢很多,所以不带缓冲的流效率很低。带缓冲的流,可以一次读很多字节,但不向磁盘中写入,只是先放到内存里。等凑够了缓冲区大小的时候一次性写入磁盘,这种方式可以减少磁盘操作次数,速度就会提高很多!这就是两者的区别。

public class InputStreamTest { private static final String FILENAME="E:\电影\[高清电影]阿甘正传.1994.美国.中文字幕.1280x720.rmvb"; public static void main(String[] args) throws IOException { long l1 = readByBufferedInputStream(); long l2 = readByInputStream(); System.out.println("通过BufferedInputStream读取用时:"+l1+";通过InputStream读取用时:"+l2); } public static long readByInputStream() throws IOException { InputStream in=new FileInputStream(FILENAME); byte[] b=new byte[8192]; int l=0; long start=System.currentTimeMillis(); while(in.read(b,0,8192)!=-1){ } long end=System.currentTimeMillis(); return end-start; } public static long readByBufferedInputStream() throws IOException { BufferedInputStream in=new BufferedInputStream(new FileInputStream(FILENAME)); byte[] b=new byte[8192]; int l=0; long start=System.currentTimeMillis(); while(in.read(b,0,8192)!=-1){ } long end=System.currentTimeMillis(); return end-start; } }

三、InputStreamreader其实就是将字节流转成字符流。

import java.io.*; class InputStreamReaderDemo { public static void transReadNoBuf() throws IOException { /** * 没有缓冲区,只能使用read()方法。 */ //读取字节流 //InputStream in = system.in;//读取键盘的输入。 InputStream in = new FileInputStream("D:\demo.txt");//读取文件的数据。 //将字节流向字符流的转换。要启用从字节到字符的有效转换, //可以提前从底层流读取更多的字节. InputStreamReader isr = new InputStreamReader(in);//读取 //综合到一句。 //InputStreamReader isr = new InputStreamReader( //new FileInputStream("D:\demo.txt")); char []cha = new char[1024]; int len = isr.read(cha); System.out.println(new String(cha,0,len)); isr.close(); } public static void transReadByBuf() throws IOException { /** * 使用缓冲区 可以使用缓冲区对象的 read() 和 readLine()方法。 */ //读取字节流 //InputStream in = system.in;//读取键盘上的数据 InputStream in = new FileInputStream("D:\demo.txt");//读取文件上的数据。 //将字节流向字符流的转换。 InputStreamReader isr = new InputStreamReader(in);//读取 //创建字符流缓冲区 BufferedReader bufr = new BufferedReader(isr);//从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的 //高效读取。 可以指定缓冲区的大小,或者可使用认的大小。大多数情况下,认值足够大。类似于BufferInputStream //只是两者缓冲的对象不一样。 //BufferedReader bufr = new BufferedReader( //new InputStreamReader(new FileInputStream("D:\demo.txt")));可以综合到一句。 /*int ch =0; ch = bufr.read(); System.out.println((char)ch); */ String line; while((line = bufr.readLine())!=null){ System.out.println(line); } isr.close(); } }

四、最后一个ByteArrayInputStream 这个其实用的的不多,但是ByteArrayOutputStream还是用的多点,所以拿ByteArrayOutputStream来上代码。他的作用就是把字节流转成字符。其实我感觉没啥用,可能是我知道的不多    

文件中读取二进制数据,全部存储到ByteArrayOutputStream中。

FileInputStream fis=new FileInputStream("test"); BufferedInputStream bis=new BufferedInputStream(fis); ByteArrayOutputStream baos=new ByteArrayOutputStream(); int c=bis.read();//读取bis流中的下一个字节 while(c!=-1){ baos.write(c); c=bis.read(); } bis.close(); byte retArr[]=baos.toByteArray();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐