1、SequenceInputStream(序列流)
/** * 序列流整合多个 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { FileInputStream s1 = new FileInputStream("a.txt"); FileInputStream s2 = new FileInputStream("b.txt"); FileInputStream s3 = new FileInputStream("c.txt"); FileOutputStream fos = new FileOutputStream("d.txt"); Vector<FileInputStream> v = new Vector<>(); v.add(s1); v.add(s2); v.add(s3); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); int b; while((b = sis.read()) != -1) { fos.write(b); } fos.close(); sis.close(); }
2、ByteArrayOutputStream:内存输出流
FileInputStream fis = new FileInputStream("a.txt"); /** * 在内存中创建了可以增长的字节数组 */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b; while((b = fis.read()) != -1) { baos.write(b); } /** * 将缓冲区的数据全部获取 赋值给arr */ //1,new String()可以指定编码表 byte[] arr = baos.toByteArray(); System.out.println(new String(arr)); //2,不能制定编码表 System.out.println(baos.toString()); fis.close();
3、ByteArrayOutputStream:内存输出流
FileInputStream fis = new FileInputStream("d:\\iotest\\2.txt"); byte[] arr = new byte[5]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len; while((len = fis.read(arr)) != -1) { baos.write(arr, 0, len); } System.out.println(baos); fis.close();
4、序列化和反序列化
(public class Person implements Serializable)
public static void main(String[] args) throws Exception, IOException { Person p1 = new Person("p1",1); Person p2 = new Person("p1",2); Person p3 = new Person("p1",3); Person p4 = new Person("p1",4); Person p5 = new Person("p1",5); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); list.add(p5); /** * 序列化(存储数据到文件) */ ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("1.txt")); oos.writeObject(list); oos.close(); /** * 反序列化(读取文件) */ ObjectInputStream ois = new ObjectInputStream(new FileInputStream("1.txt")); ArrayList<Person> al = (ArrayList<Person>) ois.readObject(); for(Person p : al) { System.out.println(p); } } /** * 反序列化:读取数据 * @throws Exception */ @SuppressWarnings({ "resource", "unused" }) private static void demo() throws Exception{ ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt")); Person p1 = (Person) ois.readObject(); Person p2 = (Person) ois.readObject(); //若出现第三个对象 EOFException! System.out.println(p1+","+p2); }
5、小程序:遍历某个文件夹上所有的.mp3文件,然后拼接
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { String path = "d:\\f"; ArrayList<String> list = new ArrayList<>(); getDir(new File(path),list); /** * 文件拼接 */ FileInputStream[] fArr = new FileInputStream[list.size()]; Vector<FileInputStream> v = new Vector<>(); for (int i = 0; i < list.size(); i++) { fArr[i] = new FileInputStream(path + "\\" + list.get(i)); v.add(fArr[i]); } Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("d:\\succ.mp3"); int b; while((b = sis.read()) != -1) { fos.write(b); } fos.close(); for (int i = 0; i < list.size(); i++) { fArr[i].close(); } } /** * 获取mp3文件 * @param file * @param list */ private static void getDir(File file,ArrayList<String> list) { File[] arr = file.listFiles();//获取文件数组 for(File f : arr) { if(f.isFile() && f.getName().endsWith(".mp3")) { list.add(f.getName()); } else if (f.isDirectory()){ getDir(f,list); } } }