1.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo4 { public static void main(String[] args) { InputStream inputStream = null; try { //创建一个流对象 inputStream = new FileInputStream("a.txt"); //存储数据 int length=0; //使用缓冲区来存储读取到的数据 byte[] str=new byte[1024]; while ((length=inputStream.read(str))!=-1){ System.out.println(new String(str,0,length)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }