关闭流的顺序:
* 当A依赖B的时候先关闭A,再关闭B
* 带缓冲的流最后关闭的时候会执行一次flush
ByteArrayInputStream :是字节数组输入流,作用是把字节串(或叫字节数组)变成输入流的形式
1 package object.io; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class ByteArrayStream { 9 10 public static void main(String[] args) throws IOException { 11 // TODO Auto-generated method stub 12 byte[] b=new byte[]{‘a‘,‘b‘,‘c‘}; 13 //将字节输入转成流 14 ByteArrayInputStream input=new ByteArrayInputStream(b); 15 ByteArrayOutputStream output=new ByteArrayOutputStream(); 16 FileOutputStream fileOut=new FileOutputStream("D:\\Program Files (x86)\\io\\ByteArray.txt"); 17 int length; 18 while((length=input.read())!=-1){ 19 fileOut.write(length); 20 } 21 //将缓冲区的内容刷新到输出流 22 output.writeTo(fileOut); 23 input.close(); 24 output.close(); 25 fileOut.close(); 26 } 27 28 29 }
SequenceInputStream :将多个文件合并成一个文件
1 package object.io; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.SequenceInputStream; 8 import java.util.ArrayList; 9 import java.util.Collections; 10 import java.util.Enumeration; 11 import java.util.List; 12 13 14 public class SequenceStream { 15 16 //将多个文件合并到一个文件 17 public static void main(String[] args) throws IOException { 18 // TODO Auto-generated method stub 19 20 FileInputStream f1=new FileInputStream("D:\\Program Files (x86)\\io\\sequence1.txt"); 21 FileInputStream f2=new FileInputStream("D:\\Program Files (x86)\\io\\sequence2.txt"); 22 FileInputStream f3=new FileInputStream("D:\\Program Files (x86)\\io\\sequence3.txt"); 23 List<FileInputStream> list=new ArrayList<FileInputStream>(); 24 list.add(f1); 25 list.add(f2); 26 list.add(f3); 27 28 //将集合转换成枚举类型 29 Enumeration<FileInputStream> e=Collections.enumeration(list); 30 31 //接收的是枚举类型,如上图所示 32 SequenceInputStream s=new SequenceInputStream(e); 33 34 FileOutputStream outputStream=new FileOutputStream("D:\\Program Files (x86)\\io\\sequence4.txt"); 35 int length; 36 while((length=s.read())!=-1){ 37 outputStream.write(length); 38 } 39 f1.close(); 40 f2.close(); 41 f3.close(); 42 s.close(); 43 outputStream.close(); 44 } 45 46 }
BufferedInputStream :缓冲字节输入流,是一个高级流(处理流),与其他低级流配合使用,将其他流包装起来。效率高
BufferedInputStream :同BufferedInputStream
1 package object.io; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class BufferStream { 10 public static void main(String[] args) throws IOException { 11 FileInputStream input=new FileInputStream("D:\\Program Files (x86)\\io\\bufferStream1.txt"); 12 BufferedInputStream binput=new BufferedInputStream(input); 13 FileOutputStream output=new FileOutputStream("D:\\Program Files (x86)\\io\\bufferStream2.txt"); 14 BufferedOutputStream boutput=new BufferedOutputStream(output); 15 int length; 16 while((length=binput.read())!=-1){ 17 boutput.write(length); 18 } 19 //将缓冲区的内容刷新到文件 20 boutput.flush(); 21 input.close(); 22 binput.close(); 23 output.close(); 24 boutput.close(); 25 } 26 }
原文地址:https://www.cnblogs.com/ztt0918/p/8116969.html
时间: 2024-11-09 05:11:52