public static void demo1() throws FileNotFoundException, IOException { FileInputStream fis1 = new FileInputStream("a.txt"); //创建字节输入流关联a.txt FileOutputStream fos = new FileOutputStream("c.txt"); //创建字节输出流关联c.txt int b1; while((b1 = fis1.read()) != -1) { //不断的在a.txt上读取字节 fos.write(b1); //将读到的字节写到c.txt上 } fis1.close(); //关闭字节输入流 FileInputStream fis2 = new FileInputStream("b.txt"); int b2; while((b2 = fis2.read()) != -1) { fos.write(b2); } fis2.close(); fos.close(); }
FileOutputStream fos = new FileOutputStream("c.txt"); //创建字节输出流关联c.txt
fileoutputstream重新开启会造成文本清空 ,fileinputstream不会造成文本清空
===============================================================================================================================================
序列流
1.什么是序列流
* 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.
2.使用方式
* 整合两个: SequenceInputStream(InputStream, InputStream)
* 整合多个: SequenceInputStream(Enumeration)
public static void demo2() throws FileNotFoundException, IOException { FileInputStream fis1 = new FileInputStream("a.txt"); FileInputStream fis2 = new FileInputStream("b.txt"); SequenceInputStream sis = new SequenceInputStream(fis1, fis2); FileOutputStream fos = new FileOutputStream("c.txt"); int b; while((b = sis.read()) != -1) { fos.write(b); } sis.close(); //sis在关闭的时候,会将构造方法中传入的流对象也都关闭 fos.close(); }
使用枚举添加多个fileinputstream
/** * @param args * 整合两个输入流 * SequenceInputStream(InputStream s1, InputStream s2) * 整合多个输入流 * SequenceInputStream(Enumeration<? extends InputStream> e) * @throws IOException */ public static void main(String[] args) throws IOException { //demo1(); //demo2(); FileInputStream fis1 = new FileInputStream("a.txt"); FileInputStream fis2 = new FileInputStream("b.txt"); FileInputStream fis3 = new FileInputStream("c.txt"); Vector<FileInputStream> v = new Vector<>(); //创建集合对象 v.add(fis1); //将流对象存储进来 v.add(fis2); v.add(fis3); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); //将枚举中的输入流整合成一个 FileOutputStream fos = new FileOutputStream("d.txt"); int b; while((b = sis.read()) != -1) { fos.write(b); } sis.close(); fos.close(); }
内存输出流
* 1.什么是内存输出流
* 该输出流可以向内存中写数据, 把内存当作一个缓冲区, 写出之后可以一次性获取出所有数据
* 2.使用方式
* 创建对象: new ByteArrayOutputStream()
* 写出数据: write(int), write(byte[])
* 获取数据: toByteArray()
*
/** * @param args * ByteArrayOutputStream * 内存输出流 * * FileInputStream读取中文的时候出现了乱码 * * 解决方案 * 1,字符流读取 * 2,ByteArrayOutputStream * @throws IOException */ public static void main(String[] args) throws IOException { //demo1(); FileInputStream fis = new FileInputStream("e.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); //在内存中创建了可以增长的内存数组 int b; while((b = fis.read()) != -1) { baos.write(b); //将读取到的数据逐个写到内存中 } //byte[] arr = baos.toByteArray(); //将缓冲区的数据全部获取出来,并赋值给arr数组 //System.out.println(new String(arr)); System.out.println(baos.toString()); //将缓冲区的内容转换为了字符串,在输出语句中可以省略调用toString方法 fis.close(); } public static void demo1() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("e.txt"); byte[] arr = new byte[3]; int len; while((len = fis.read(arr)) != -1) { System.out.println(new String(arr,0,len)); } fis.close(); }
===========================================================================================
对象操作流ObjectInputStream
首先对象要先实现Serializable接口
public class person implements Serializable{ private String name; private int age; public person() { super(); }
* 读取: new ObjectInputStream(InputStream), readObject() * public class Demo3_ObjectInputStream { /** * @param args * @throws IOException * @throws ClassNotFoundException * @throws FileNotFoundException * 读取对象,反序列化 */ public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt")); Person p1 = (Person) ois.readObject(); Person p2 = (Person) ois.readObject(); System.out.println(p1); System.out.println(p2); ois.close(); } }
* 将对象存储在集合中写出
Person p1 = new Person("张三", 23); Person p2 = new Person("李四", 24); Person p3 = new Person("马哥", 18); Person p4 = new Person("辉哥", 20); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt")); oos.writeObject(list); //将对象信息写入到文本中 oos.close();
ObjectInputStream is = new ObjectInputStream(new FileInputStream("e.txt")); ArrayList<person> list = (ArrayList<person>) is.readObject(); for (person s1 : list) { System.out.println(s1); //将对象信息写出到控制台 } is.close();
* 将对象存储在集合中写出 Person p1 = new Person("张三", 23); Person p2 = new Person("李四", 24); Person p3 = new Person("马哥", 18); Person p4 = new Person("辉哥", 20); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt")); oos.writeObject(list); //写出集合对象 oos.close();
* 读取到的是一个集合对象 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt")); ArrayList<Person> list = (ArrayList<Person>)ois.readObject(); //泛型在运行期会被擦除,索引运行期相当于没有泛型 //想去掉黄色可以加注解 @SuppressWarnings("unchecked") for (Person person : list) { System.out.println(person); } ois.close();
加上id号 好处在于如果类 比如新增 gender属性,没有先存档就直接读档 就显示ID信息不正确
* 注意
* 要写出的对象必须实现Serializable接口才能被序列化
* 不用必须加id号
public class Person implements Serializable { /** * */ private static final long serialVersionUID = 2L; private String name; private int age;