温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

InputStream怎么在Java项目中使用

发布时间:2021-03-22 17:52:48 来源:亿速云 阅读:147 作者:Leah 栏目:编程语言

本篇文章为大家展示了InputStream怎么在Java项目中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

一、首先我先理解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();

上述内容就是InputStream怎么在Java项目中使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI