SequenceInputStream用例题讲述用法
需求:1.把a.txt与b.txt 文件的内容合并
2.把a.txt与b.txt 、c.txt文件的内容合并
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; public class Demo1 { public static void main(String[] args) throws IOException { fangfa1(); } private static void fangfa1() throws IOException { // TODO Auto-generated method stub File file1 = new File("F:\a.txt"); File file2 = new File("F:\b.txt"); File file3 = new File("F:\c.txt"); FileInputStream filein1 = new FileInputStream(file1); FileInputStream filein2 = new FileInputStream(file2); FileOutputStream fileout3 = new FileOutputStream(file3); ArrayList<FileInputStream> list = new ArrayList<FileInputStream>(); list.add(filein1); list.add(filein2); byte[] buf = new byte[1024]; int content; for(int i = 0;i < list.size();i++) { FileInputStream filein = list.get(i); while((content = filein.read(buf))!=1) { fileout3.write(buf, 0, content); } filein.close(); } fileout3.close(); } }
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.ArrayList; public class Demo2 { public static void main(String[] args) throws IOException { fangfa1(); } private static void fangfa1() throws IOException { // TODO Auto-generated method stub File file1 = new File("F:\a.txt"); File file2 = new File("F:\b.txt"); File file3 = new File("F:\c.txt"); FileInputStream filein1 = new FileInputStream(file1); FileInputStream filein2 = new FileInputStream(file2); FileOutputStream fileout3 = new FileOutputStream(file3); SequenceInputStream sinput = new SequenceInputStream(filein1, filein2); byte[] buf = new byte[1024]; int content; while((content = sinput.read(buf))!=1) { fileout3.write(buf, 0, content); } sinput.close(); fileout3.close(); } }
//第二题
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; public class Demo3 { public static void main(String[] args) throws IOException { fangfa1(); } private static void fangfa1() throws IOException { // TODO Auto-generated method stub File file1 = new File("F:\a.txt"); File file2 = new File("F:\b.txt"); File file3 = new File("F:\c.txt"); File file4 = new File("F:\d.txt"); FileInputStream filein1 = new FileInputStream(file1); FileInputStream filein2 = new FileInputStream(file2); FileInputStream filein3 = new FileInputStream(file3); FileOutputStream fileout4 = new FileOutputStream(file4); Vector<FileInputStream> vector = new Vector<FileInputStream>(); vector.add(filein1); vector.add(filein2); vector.add(filein3); Enumeration<FileInputStream> it = vector.elements(); SequenceInputStream sinput = new SequenceInputStream(it); byte[] buf = new byte[1024]; int content; while((content = sinput.read(buf))!=1) { fileout4.write(buf, 0, content); } sinput.close(); fileout4.close(); } }
上题第三段代码用Vector集合的原因是在SequenctInputStream中有一个带参数的构造方法调用的是Vector的迭代器!
需求: 把一首mp3先切割成n份,然后再把这些文件合并起来。
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; public class Demo4 { public static void main(String[] args) throws IOException { //jianqie();//剪切 hebing();//合并 } public static void hebing() throws IOException{ File dir = new File("F:\music"); Vector<FileInputStream> vector = new Vector<FileInputStream>(); File[] files = dir.listFiles(); for(File file : files){ if(file.getName().endsWith(".mp3")){ vector.add(new FileInputStream(file)); } } Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream inputStream = new SequenceInputStream(e); FileOutputStream fileOutputStream = new FileOutputStream("F:\合并.mp3"); byte[] buf = new byte[1024]; int length = 0 ; while((length = inputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,length); } fileOutputStream.close(); inputStream.close(); } public static void jianqie() throws IOException{ File file = new File("F:\1.mp3"); File dir = new File("F:\music"); FileInputStream fileInputStream = new FileInputStream(file); byte[] buf = new byte[1024*1024]; int length = 0; for(int i = 0 ; (length = fileInputStream.read(buf))!=-1 ; i++){ FileOutputStream fileOutputStream = new FileOutputStream(new File(dir,"part"+i+".mp3")); fileOutputStream.write(buf,0,length); fileOutputStream.close(); } fileInputStream.close(); } }
对象的输入输出流 : 对象的输入输出流主要的作用是用于写对象的信息与读取对象的信息。 对象信息一旦写到文件上那么对象的信息就可以做到持久化了
对象的输出流: ObjectOutputStream .
对象的输入流: ObjectInputStream
ObjectOutputStream的使用步骤:
对象输入输出流要注意的细节:
1. 如果对象需要被写出到文件上,那么对象所属的类必须要实现Serializable接口。 Serializable接口没有任何的方法,是一个标识接口而已。
2. 对象的反序列化创建对象的时候并不会调用到构造方法的、
3. serialVersionUID 是用于记录class文件的版本信息的,serialVersionUID这个数字是通过一个类的类名、成员、包名、工程名算出的一个数字。
4. 使用ObjectInputStream反序列化的时候,ObjeectInputStream会先读取文件中的serialVersionUID,然后与本地的class文件的serialVersionUID
进行对比,如果这两个id不一致,那么反序列化就失败了。
5. 如果序列化与反序列化的时候可能会修改类的成员,那么最好一开始就给这个类指定一个serialVersionUID,如果一类已经指定的serialVersionUID,然后
在序列化与反序列化的时候,jvm都不会再自己算这个 class的serialVersionUID了。
6. 如果一个对象某个数据不想被序列化到硬盘上,可以使用关键字transient修饰。
7. 如果一个类维护了另外一个类的引用,那么另外一个类也需要实现Serializable接口。
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Address implements Serializable{ String country; String city; public Address(String country,String city){ this.country = country; this.city = city; } } class User implements Serializable{ String userName ; String password; transient int age;// transient 透明 Address address ; public User(String userName , String passwrod) { this.userName = userName; this.password = passwrod; } public User(String userName , String passwrod,int age,Address address) { this.userName = userName; this.password = passwrod; this.age = age; this.address = address; } @Override public String toString() { return "用户名:"+this.userName+ " 密码:"+ this.password+" 年龄:"+this.age+" 地址:"+this.address.city; } } public class Demo5 { public static void main(String[] args) throws IOException, Exception { writeObj(); //readObj(); } //把文件中的对象信息读取出来 public static void readObj() throws IOException, ClassNotFoundException{ //找到目标文件 File file = new File("F:\obj.txt"); //建立数据的输入通道 FileInputStream fileInputStream = new FileInputStream(file); //建立对象的输入流对象 ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); //读取对象信息 User user = (User) objectInputStream.readObject(); System.out.println(user); } //定义方法把对象的信息写到硬盘上 public static void writeObj() throws IOException{ //把user对象的信息持久化存储。 Address address = new Address("中国","广州"); User user = new User("admin","123",15,address); //找到目标文件 File file = new File("F:\obj.txt"); //建立数据输出流对象 FileOutputStream fileOutputStream = new FileOutputStream(file); //建立对象的输出流对象 ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); //把对象写出 objectOutputStream.writeObject(user); //关闭资源 objectOutputStream.close(); } }