当前位置: 移动技术网 > IT编程>开发语言>Java > 常用输入字节流InputStream介绍

常用输入字节流InputStream介绍

2019年07月19日  | 移动技术网IT编程  | 我要评论

输入字节流inputstream的一系列实现,以下是五种较常用的子类

1、字节数组作为输入源——bytearrayinputstream

该类包括两个构造方法

bytearrayinputstream(byte[] buf);
bytearrayinputstream(byte[] buf,int offset,int length );

该类重写了inputstream中的所有方法,因此我们可以调用其父类同名的方法进行读写操作。

下面是如何通过一个字节数组创建字节输入流的过程,并从输入流中读取数据输出到操作台。

import java.io.bytearrayinputstream;
import java.io.ioexception;

public class testbytearrayinputstream {
 public static void main(string[] args) throws ioexception {
  // 初始化字节数组
  byte[] buf = new byte[3];
  buf[0] = 100;
  buf[1] = 101;
  buf[2] = 102;
  // 创建输入流
  bytearrayinputstream input = new bytearrayinputstream(buf);
  // 从输入流中读取数据
  byte[] out = new byte[3];
  input.read(out);
  system.out.println(new string(out));
  // 关闭输入流
  input.close();

 }
}

2、文件作为输入源——fileinputstream

fileinputstream从文件系统中的某个文件中获取输入字节,适用于读取诸如图像数据之类的原始数据流。有一下两种创建方法:

fileinputstream(file file);//通过系统中的file对象file指定
fileinputstream(string name);//通过系统中路径名name指定同样的我们使用重写的inputstream中的方法的名称来实现读文件内容。

下面是如何读取磁盘文件来创建一个文件输入流,循环读取该数据流中的数据并输数据到控制台。

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

public class testfileinputstream {
 public static void main(string[] args) throws filenotfoundexception {
  try {
   // 创建输入流
   fileinputstream input = new fileinputstream("d:/demo/test.txt");
   // 从输入流中读取数据
   while (input.available() > 0) {
    int out = input.read();
    system.out.println((char) out);
   }
   // 关闭输入流
   input.close();
  } catch (ioexception e) {
   e.printstacktrace();
  }
 }
}

3、对象作为输入源——objectinputstream

objectinputstream与fileinputstream一起使用时,可以为应用程序提供对对象图形的持久储存。

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.objectinputstream;
import java.sql.date;

public class testobjectinputstream {
 public static void main(string[] args) throws classnotfoundexception {
  try {
   // 创建文件输入流
   fileinputstream file = new fileinputstream("d:/demo?test.tmp");
   // 创建对象输入流
   objectinputstream object = new objectinputstream(file);
   // 读取对象的数据
   int i = object.readint();
   string string = (string) object.readobject();
   date date = (date) object.readobject();
   system.out.println("i=" + i);
   system.out.println("string=" + string);
   system.out.println("date=" + date);
   // 关闭流
   object.close();
   file.close();
  } catch (filenotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }

 }
}

4、字符串作为输入源——stringbufferinputstream

stringbufferinputstream允许输入流由字符串内容提供,与bytearrayinputstream使用字节数组作为输入源类似,但是只有一种构造方法:

stringbufferinputstream(string s); //以从指定字符串读取数据

只需要一个字符串作为输入就可以创建,以下是一个实例:

import java.io.ioexception;
import java.io.stringbufferinputstream;

public class teststringbufferinputstream {
 public static void main(string[] args) {
  // 创建输入流
  stringbufferinputstream input = new stringbufferinputstream("hello world!");
  // 从输入流中读取数据
  while (input.available() > 0) {
   int out = input.read();
   system.out.print((char) out);
  }
  // 关闭输入流
  try {
   input.close();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }
}

5、缓存输入流——bufferedinputstream

bufferedinputstream为另一个输入流添加一些功能,即缓存输入功能。在创建bufferedinputstream时,会创建一个内部缓存区间数组。在读取或者跳过流中字节时,可根据需要从包含的输入流在此填充该内部缓存区,一次填充多个字节。

如果你需要一个具有缓存的文件输入流,则应当组合使用fileinputstream和bufferedinputstream,这将能提高读取效率。

下面一段代码是将文件输入流对象转换成缓存输入流的过程:

import java.io.bufferedinputstream;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

public class testbufferedinputstream {
 private void mian() {
  // todo auto-generated method stub
  try {
   // 创建文件输入流
   fileinputstream input = new fileinputstream("d:/demo/test.txt");
   bufferedinputstream buffer = new bufferedinputstream(input);
   // 从输入流中读取数据
   while (buffer.available() > 0) {
    int out = buffer.read();
    system.out.print((char) out);
   }
   // 关闭流
   buffer.close();
   input.close();
  } catch (filenotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }

 }
}

以上这篇常用输入字节流inputstream介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网