javaSE基础知识——day16 IO流:数据输入输出流,内存操作流,打印流,随机访问流,序列化流,Properties的使用

编程入门 行业动态 更新时间:2024-10-09 07:20:51

javaSE<a href=https://www.elefans.com/category/jswz/34/1769428.html style=基础知识——day16 IO流:数据输入输出流,内存操作流,打印流,随机访问流,序列化流,Properties的使用"/>

javaSE基础知识——day16 IO流:数据输入输出流,内存操作流,打印流,随机访问流,序列化流,Properties的使用

数据输入输出流的概述和使用

A:数据输入输出流的概述
通过API查看
数据输入和输出流:
数据输入流: DataInpuStream
数据输出流: DataOutputStream

特点: 可以读写基本类型
B:案例演示: 数据输入输出流的基本使用

案例:

package javaSEreview20190724.杂七杂八流;import java.io.*;/*** @Description:TODO* @Author:@李小白* @Date:2019/7/24 23:24*/
public class Demo01数据输入输出流 {public static void main(String[] args) throws IOException {getOut();//特点:可以读写基本类型//怎么读的就怎么取,顺序不要乱DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));System.out.println(in.readInt());System.out.println(in.readBoolean());System.out.println(in.readDouble());System.out.println(in.readUTF());in.close();}private static void getOut() throws IOException {DataOutputStream out = new DataOutputStream(new FileOutputStream("a.txt"));out.writeInt(100);out.writeBoolean(false);out.writeDouble(3.14);out.writeUTF("可以是字符串");out.close();}
}

内存操作流的概述和使用

A:内存操作流的概述
a:操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
此流关闭无效,所以无需关闭
b:操作字符数组
CharArrayWrite
CharArrayReader
c:操作字符串
StringWriter
StringReader
B:案例演示: 内存操作流的使用
// 构造方法: public ByteArrayOutputStream()

案例:
a:操作字节数组
ByteArrayOutputStream
ByteArrayInputStream

package javaSEreview20190724.杂七杂八流;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;/*** @Description:TODO* @Author:@李小白* @Date:2019/7/24 23:35*/
public class Demo02内存操作流 {public static void main(String[] args) throws IOException {ByteArrayOutputStream out = new ByteArrayOutputStream();out.write("不关联文件在内存中操作".getBytes());out.write("自带缓冲区,并可以自己扩大缓冲区".getBytes());out.write("此流不需要关闭".getBytes());//取出缓冲区的数据byte[] bytes = out.toByteArray();String s = new String(bytes);System.out.println(s);//        String s1 = out.toString();
//        System.out.println(s1);
//        out.close();此流无需关闭
//        out.write("abc".getBytes());
//        String s2 = out.toString();
//        System.out.println(s2);ByteArrayInputStream in = new ByteArrayInputStream(bytes);byte[] bytes1 = new byte[1024 * 8];int len = in.read(bytes1);String s1 = new String(bytes1,0,len);System.out.println(s1);}
}

b:操作字符数组
CharArrayWrite
CharArrayReader

package javaSEreview20190724.杂七杂八流;import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;/*** @Description:TODO* @Author:@李小白* @Date:2019/7/24 23:49*/
public class Demo03内存操作流字符数组 {public static void main(String[] args) throws IOException {CharArrayWriter cw = new CharArrayWriter();cw.write("abc");cw.write("abc");cw.write("abc");//取出缓冲区的数据char[] chars = cw.toCharArray();//输出缓冲区的数据System.out.println(cw.toString());System.out.println(String.valueOf(chars));System.out.println(new String(chars));//        CharArrayReader cr = new CharArrayReader(chars);}
}

c:操作字符串
StringWriter
StringReader

package javaSEreview20190724.杂七杂八流;import java.io.StringWriter;/*** @Description:TODO* @Author:@李小白* @Date:2019/7/24 23:56*/
public class Demo04内存操作流字符串 {public static void main(String[] args) {StringWriter sw = new StringWriter();sw.write("abc");sw.write("abc");sw.append("ccc");//直接输出String s = sw.toString();System.out.println(s);}
}

把多个文件合成一个文件,两种方法

方法一:

package javaSEreview20190724.杂七杂八流;import java.io.*;
import java.util.ArrayList;/*** @Description:TODO* @Author:@李小白* @Date:2019/7/25 0:01*/
public class Demo05几个文件合成一个文件歌曲大合唱 {public static void main(String[] args) throws IOException {FileInputStream in1 = new FileInputStream(new File(""许巍-曾经的你.mp3""));FileInputStream in2 = new FileInputStream(new File(""许巍-曾经的你.mp3""));FileOutputStream out = new FileOutputStream(new File("大合唱"));ArrayList<FileInputStream> list = new ArrayList<>();list.add(in1);list.add(in2);int len=0;byte[] bytes = new byte[1024 * 8];for (FileInputStream in : list) {while ((len=in.read(bytes))!=-1) {out.write(len);out.flush();}in.close();}out.close();}
}

方法二:

package javaSEreview20190724.杂七杂八流;import java.io.*;
import java.util.ArrayList;/*** @Description:TODO* @Author:@李小白* @Date:2019/7/25 0:17*/
public class Demo06几个文件合成一各文件 {public static void main(String[] args) throws java.io.IOException {FileInputStream in1 = new FileInputStream("许巍 - 曾经的你.mp3");FileInputStream in2 = new FileInputStream("许巍 - 蓝莲花.mp3");FileOutputStream out = new FileOutputStream("歌曲大联唱.mp3");ByteArrayOutputStream byteOut = new ByteArrayOutputStream();//创建一个集合ArrayList<FileInputStream> list = new ArrayList<>();list.add(in1);list.add(in2);int len=0;byte[] bytes = new byte[1024 * 8];for (FileInputStream in : list) {while ((len=in.read(bytes))!=-1){byteOut.write(bytes,0,len);byteOut.flush();}in.close();}//取出两首歌的字节数据byte[] allBytes = byteOut.toByteArray();System.out.println(allBytes.length);//将两首歌的字节数据,写到硬盘上ByteArrayInputStream byteIn = new ByteArrayInputStream(allBytes);int len2 = 0;byte[] bytes2 = new byte[1024 * 8];while ((len2 = byteIn.read(bytes)) != -1) {out.write(bytes, 0, len2);out.flush();}out.close();}}

打印流的概述和特点以及作为Writer的子类使用

A:打印流的概述
通过API查看
字节流打印流
字符打印流
B:打印流的特点
a: 打印只能操作目的,不能操作数据源(不能进行读取)

  • b: 可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型

c: 如果我们启用自动刷新,那么在调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新
/**
通过以下构造创建对象 能够启动自动刷新 然后调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新
public PrintWriter(OutputStream out, boolean autoFlush) 启动 自动刷新
public PrintWriter(Writer out, boolean autoFlush) 启动自动刷新
*/
d: 这个流可以直接对文件进行操作(可以直接操作文件的流: 就是构造方法的参数可以传递文件或者文件路径)
C:案例演示: PrintWriter作为Writer的子类使用
案例:

package javaSEreview20190724.杂七杂八流;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;/*** @Description:TODO* @Author:@李小白* @Date:2019/7/25 0:24*/
public class Demo07打印流 {public static void main(String[] args) throws IOException {//字节打印流://字符打印流://打印流之关联目标文件,不关联源文件//字节打印流// PrintStream  PrintStream 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。// System.out; out:标准的输出流,所关联的设备是屏幕// out.write("字节打印刘".getBytes());// out.print(3.14);// 构造方法摘要// PrintStream(File file)// 创建具有指定文件且不带自动行刷新的新打印流。// PrintStream(File file, String csn)// 创建具有指定文件名称和字符集且不带自动行刷新的新打印流。// PrintStream(OutputStream out)// 创建新的打印流。// PrintStream(OutputStream out, boolean autoFlush)// 创建新的打印流。// PrintStream out = System.out;//通过System.out 返回的字节打印流,打印的数据会输出到屏幕PrintStream ps = new PrintStream("a.txt");ps.write(1);ps.write("孤独的打印字符流".getBytes());ps.println("abc");ps.println("abc");ps.println("abc");ps.close();}
}

案例一:
打印流复制文本文件 true自动刷新,两种打印方式

package javaSEreview20190724.杂七杂八流;import java.io.*;
import java.util.Scanner;/*** @Description:TODO* @Author:@李小白* @Date:2019/7/25 0:28*/
public class Demo08打印流复制文本文件 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("b.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));String lin=null;while ((lin=br.readLine())!=null) {bw.write(lin);bw.flush();}bw.close();br.close();System.out.println("--------------------");
//也可以用Scanner进行读Scanner sc = new Scanner(new File("b.txt"));BufferedWriter bw2 = new BufferedWriter(new FileWriter("a.txt",true));while (sc.hasNextLine()){String s = sc.nextLine();System.out.println(s);}bw2.close();sc.close();}
}

案例二:
字节打印流

package org.westos.demo3;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;/*** @Author: ShenMouMou* @CreateTime: 2019-07-24 14:55* @Description:西部开源教育科技有限公司*/
public class 字节打印流 {public static void main(String[] args) throws IOException {//打印流:只操作目的地,不关联源文件//  PrintStream 字节打印流PrintStream printStream = new PrintStream(new FileOutputStream("b.txt"));printStream.write("字节打印流".getBytes());printStream.print(true);printStream.println(100);printStream.close();PrintStream out = System.out;out.write("abc".getBytes());out.println(3.14);System.out.println("abc");}
}

PrintWriter实现自动刷新和换行

A:案例演示:PrintWriter实现自动刷新和换行
PrintWriter pw = new PrintWriter(new FileWriter(“printWriter2.txt”) , true) ;
pw.println(true) ;
pw.println(100) ;
pw.println(“中国”) ;

案例:

//通过现有的 OutputStream 创建新的 PrintWriter。
//与 PrintStream 类不同,如果启用了自动刷新,
//则只有在调用 println、printf
//或 format 的其中一个方法时才可能完成此操作,

案例三:
字符打印流

package org.westos.demo3;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;/*** @Author: ShenMouMou* @CreateTime: 2019-07-24 15:01* @Description:西部开源教育科技有限公司*/
public class 字符打印流 {public static void main(String[] args) throws IOException {//参数2:true 自动刷新//如果启用了自动刷新,则只有在调用 println、printf 或 format 的其中一个方法时才可能完成此操作PrintWriter writer = new PrintWriter(new FileOutputStream("c.txt"),true);//writer.write("字符打印流");writer.println("abc");writer.println("abc");writer.println("abc");writer.println("abc");writer.println("abc");writer.println("abc");writer.println("abc");writer.flush();writer.close();}
}

标准输入输出流概述和输出语句的本质

A:标准输入输出流概述
在System这个类中存在两个静态的成员变量:

public static final InputStream in: 标准输入流, 对应的设备是键盘

public static final PrintStream out: 标准输出流 , 对应的设备就是显示器
System.in的类型是InputStream.
System.out的类型是PrintStream是OutputStream的孙子类FilterOutputStream 的子类.

二种方式实现键盘录入

A:Scanner
B:BufferedReader的readLine方法。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

package day20190725.study01.杂七杂八流;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;/*** @description: 2种键盘录入方式* @author: @李小白* @create: 2019-07-25 15:02*/
public class Demo02标准的输入输出流2种键盘录入方式 {public static void main(String[] args) throws IOException {//方式一:Scanner scanner = new Scanner(System.in);//方式二:BufferedReader br = new BufferedReader(new InputStreamReader(System.in));while (true) {System.out.println("请输入你想要输入的数据");String s = br.readLine();if (s.equals("886")) {break;}System.out.println(s);}}}

随机访问流概述和写出数据(断点下载,断点复制)

A:随机访问流概述 RandomAccessFile概述

  • 用于断点下载,断点复制,按顺序读写

  • 最大特点 能读能写 RandomAccessFile类不属于流,是Object类的子类。

  • 但它融合了InputStream和OutputStream的功能。 支持对随机访问文件的读取和写入。​

  • RandomAccessFile的父类是Object , 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据.

  • getFilePointer方法获取文件指针

  • 且可以通过seek(0)方法设置文件指针

B:案例演示: 随机访问流写出数据

案例:

//随机访问流:此流最大的特点是能读能写,而来可以设置文件指针的位置
//此类的实例支持对随机访问文件的读取和写入。
//随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。
//存在指向该隐含数组的光标或索引,称为文件指针;
//输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。

package day20190725.study01.杂七杂八流;import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;/*** @description: 打印断点* @author: @李小白* @create: 2019-07-25 15:13*/
public class Demo03随机访问流 {public static void main(String[] args) throws IOException {getWriter();RandomAccessFile rafr = new RandomAccessFile("a.txt", "rw");//读写顺序应该一致int i = rafr.readInt();System.out.println(rafr.getFilePointer());//指针的位置boolean b = rafr.readBoolean();System.out.println(rafr.getFilePointer());//指针的位置double v = rafr.readDouble();System.out.println(rafr.getFilePointer());//指针的位置String s = rafr.readUTF();System.out.println(rafr.getFilePointer());//指针的位置System.out.println(i);System.out.println(b);System.out.println(v);System.out.println(s);rafr.close();}private static void getWriter() throws IOException {RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");raf.write(100);raf.writeBoolean(false);raf.writeDouble(3.14);raf.writeUTF("怎么读就怎么写");raf.close();}
}

随机访问流读取数据和操作文件指针(指针的放置位置)

import javax.sound.midi.Soundbank;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;public class MyTest {public static void main(String[] args) throws IOException {//随机访问流:此流最大的特点是能读能写,而来可以设置文件指针的位置//此类的实例支持对随机访问文件的读取和写入。//随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。//存在指向该隐含数组的光标或索引,称为文件指针;//输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。// writeData();//你怎么写的就怎么读,顺不要乱RandomAccessFile raf = new RandomAccessFile("e.txt", "rw");boolean b = raf.readBoolean();//获取当前文件的指针位置System.out.println("指针位置:"+raf.getFilePointer());double v = raf.readDouble();System.out.println("指针位置:" + raf.getFilePointer());int i = raf.readInt();System.out.println("指针位置:" + raf.getFilePointer());String s = raf.readUTF();System.out.println("指针位置:" + raf.getFilePointer());System.out.println(b);System.out.println(v);System.out.println(i);System.out.println(s);//设置指针的位置raf.seek(13);String s2 = raf.readUTF();//设置后把它再读一遍System.out.println(s2);}private static void writeData() throws IOException {RandomAccessFile raf = new RandomAccessFile("e.txt", "rw");raf.writeBoolean(true);raf.writeDouble(3.14);raf.writeInt(1000);//首先,把两个字节从文件的当前文件指针写入到此文件,//类似于使用 writeShort 方法并给定要跟随的字节数raf.writeUTF("你好");raf.close();}
}

断点下载,断点复制

案例:


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;public class MyTest2 {public static void main(String[] args) {RandomAccessFile in = null;try {//断点复制in = new RandomAccessFile("E:\\疯狂动物城.mp4", "rw");RandomAccessFile out = new RandomAccessFile("E:\\疯狂动物城2.mp4", "rw");int len = 0;byte[] bytes = new byte[1];int num = 0;while ((len = in.read(bytes)) != -1) {num += len;//故意模拟了一个异常if (num > 20000) {System.out.println(1 / 0);}out.write(bytes, 0, len);}} catch (Exception e) {//记录指针位置 20000 位置要保存到一个文本文件中去try {long filePointer = in.getFilePointer();System.out.println(filePointer);} catch (IOException e1) {e1.printStackTrace();}e.printStackTrace();} finally {}}
}

序列化流和反序列化流的概述和使用

A:序列化流的概述
所谓的序列化:就是把对象通过流的方式存储到文件 对象---------硬盘 中 .注意:此对象 要重写Serializable 接口才能被序列化
反序列化:硬盘------内存 就是把文件中存储的对象以流的方式还原成对象
序列化流: ObjectOutputStream
反序列化流: ObjectInputStream
像这样一个接口中如果没有方法,那么这样的接口我们将其称之为标记接口(作用:给类打标记的,让JVM支持序列化)

一个对象可以被序列化的前提

  • 是这个对象对应的类必须实现Serializable接口
  • 给成员变量的前面加个ID public static final long serialVersionuID

如果不想被序列化
就在变量的前面加上transient

案例:

package day20190725.study01.杂七杂八流;import java.io.Serializable;/*** @description: 序列化的学生类* @author: @李小白* @create: 2019-07-25 15:41*/
public class Student implements Serializable {public static final long serialVersionUID = 42L;private String name;//transient 某个成员变量不想序列化可以使用transient来修饰一下public transient int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package day20190725.study01.杂七杂八流;import java.io.*;/*** @description: 序列化* @author: @李小白* @create: 2019-07-25 15:36*/
public class Demo04序列化 {public static void main(String[] args) throws IOException, ClassNotFoundException {getOut();ObjectInputStream in = new ObjectInputStream(new FileInputStream("a.txt"));Object o1 = in.readObject();Student student1= (Student) o1;System.out.println(student1.getAge()+student1.getName());Object o2 = in.readObject();Student student2= (Student) o2;System.out.println(student2.getAge()+student2.getName());Object o3 = in.readObject();Student student3= (Student) o3;System.out.println(student3.getAge()+student3.getName());}private static void getOut() throws IOException {Student student1 = new Student("zhang",18);Student student2 = new Student("lisi",91);Student student3 = new Student("wangwu",20);ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("a.txt"));out.writeObject(student1);out.writeObject(student2);out.writeObject(student3);out.close();}
}

对某个成员不想进行序列化

案例二:对案例一进行升级

package day20190725.study01.杂七杂八流;import java.io.*;
import java.util.ArrayList;/*** @description: 序列化* @author: @李小白* @create: 2019-07-25 15:36*/
public class Demo04序列化 {public static void main(String[] args) throws IOException, ClassNotFoundException {getOut();ObjectInputStream in = new ObjectInputStream(new FileInputStream("a.txt"));Object o = in.readObject();ArrayList<Student> obj= (ArrayList<Student>) o;//进行强制转换类型System.out.println(obj);Student student = obj.get(0);//可以进行指定索引System.out.println(student.getName());//        Object o1 = in.readObject();
//        Student student1= (Student) o1;//进行强制转换类型
//        System.out.println(student1.getAge()+student1.getName());
//        Object o2 = in.readObject();
//        Student student2= (Student) o2;
//        System.out.println(student2.getAge()+student2.getName());
//        Object o3 = in.readObject();
//        Student student3= (Student) o3;
//        System.out.println(student3.getAge()+student3.getName());}private static void getOut() throws IOException {Student student1 = new Student("zhang",18);Student student2 = new Student("lisi",91);Student student3 = new Student("wangwu",20);ArrayList<Student> students = new ArrayList<>();students.add(student1);students.add(student2);students.add(student3);ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("a.txt"));out.writeObject(students);
//        out.writeObject(student1);
//        out.writeObject(student2);
//        out.writeObject(student3);out.close();}
}

Properties的概述和作为Map集合的使用

A:Properties的概述
查看API
Properties 类表示了一个持久的属性集,属于双列集合。
Properties 可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
Properties父类是Hashtable
属于双列集合,这个集合中的键和值都是字符串 Properties不能指定泛型

  • 方法:
  • setProperly设值键和值
  • getProperly由键索值
  • load(new FileReader())读出文件
  • stor(new FileWriter())写入文件

B:案例演示: Properties作为Map集合的使用

回顾:
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;public class MyTest {public static void main(String[] args) {//序列化流://ObjectOutputStream//反序列化流://ObjectInputStream//克隆:浅克隆//深克隆:用序列化流和反序列化流可以实现对象的深克隆//ObjectOutputStream//ObjectInputStream//作业:A:案例演示//	需求:我有一个文本文件,我知道数据是键值对形式的,但是不知道内容是什么。//	      请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100”}
}

案例一:

package day20190725.study01.杂七杂八流;import java.util.Properties;/*** @description: 属性集合也是双列集合* @author: @李小白* @create: 2019-07-25 16:10*/
public class Demo05属性集合 {public static void main(String[] args) {//属性集合也就是双列集合,能共读写配置文件Properties properties = new Properties();//默认定义了键值类型Stringproperties.setProperty("章晗","李强娜");//自定义键值Object value1 = properties.get("章晗");//由键索值System.out.println(value1);//参数2,默认值,如果键没有找到对应的值,就返回默认值,键值输入错误String value2 = properties.getProperty("章晗2", "默认值");System.out.println(value2);}
}

Properties的load()和store()功能

A:Properties的load()和store()功能
Properties和IO流进行配合使用:

  • public void load(Reader reader): 读取键值对数据把数据存储到Properties中

public void store(Writer writer, String comments)把Properties集合中的键值对数据写入到文件中, comments注释
B:案例演示
Properties的load()和store()功能

案例二:

package day20190725.study01.杂七杂八流;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;/*** @description: 属性集合的两个特殊的方法* @author: @李小白* @create: 2019-07-25 16:23*/
public class Demo05属性集合的两个特殊的方法 {public static void main(String[] args) throws IOException {Properties properties = new Properties();//读出配置文件,从a.txt中读出来//要求配置文件键值用等号 =  连接properties.load(new FileReader("a.txt"));System.out.println(properties);properties.setProperty("李强娜","章晗");properties.store(new FileWriter("a.txt"),null);//键无法对应,可以设值默认值System.out.println(properties);}
}

SequenceInputStream 合并流

把所有的流传入到 Vector中用vector.element迭代器,由迭代器再产给SequenceInputStream

A:案例需求:将a.txt和b.txt两个文本文件的内容合并到c.txt

B:案例需求:采用SequenceInputStream来改进

C:作业:将一个music.mp3文件,拆分成多个小文件,再将多个小文件,合并成一个mp3文件

D:SequenceInputStream
表示其他输入流的逻辑串联。
它从输入流的有序集合开始,
并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,
依次类推,直到到达包含的最后一个输入流的文件末尾为止
a:构造方法
SequenceInputStream(InputStream s1, InputStream s2)
通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),
以提供从此 SequenceInputStream 读取的字节。
b:构造方法
SequenceInputStream(Enumeration<? extends InputStream> e)
通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数

案例一:传统方法
//A:案例需求:将a.txt和b.txt两个文本文件的内容合并到c.txt

package day20190725.study01.杂七杂八流;import java.io.*;
import java.util.ArrayList;/*** @description: 合并流 传统2合1* @author: @李小白* @create: 2019-07-25 16:37*/
public class Demo06合并流传统2合1 {public static void main(String[] args) throws IOException {FileInputStream in1 = new FileInputStream("a.txt");FileInputStream in2 = new FileInputStream("b.txt");FileOutputStream out = new FileOutputStream("c.txt");ArrayList<FileInputStream> in = new ArrayList<>();in.add(in1);in.add(in2);int len=0;byte[] bytes = new byte[1024 * 8];for (FileInputStream inputStream : in) {while ((len=inputStream.read(bytes))!=-1) {out.write(len);out.flush();}inputStream.close();}out.close();}
}

案例二:改进2合1

package day20190725.study01.杂七杂八流;import java.io.*;
import java.util.Enumeration;
import java.util.Vector;/*** @description: 合并流改进* @author: @李小白* @create: 2019-07-25 16:44*/
public class Demo06合并流改进 {public static void main(String[] args) throws IOException {FileInputStream in1 = new FileInputStream("a.txt");FileInputStream in2 = new FileInputStream("b.txt");SequenceInputStream in = new SequenceInputStream(in1,in2);FileOutputStream out = new FileOutputStream("c.txt");int len = 0;byte[] bytes = new byte[1024];while ((len = in.read(bytes)) != -1) {out.write(len);}in.close();out.close();}
}

案例三:同理3合1

package day20190725.study01.杂七杂八流;import java.io.*;/*** @description: 合并三合一* @author: @李小白* @create: 2019-07-25 16:51*/
public class Demo06合并流三合一 {public static void main(String[] args) throws IOException {FileInputStream in1 = new FileInputStream("a.txt");FileInputStream in2 = new FileInputStream("b.txt");FileInputStream in3 = new FileInputStream("d.txt");FileOutputStream out = new FileOutputStream("c.txt");//两个合并流进行其嵌套SequenceInputStream onein = new SequenceInputStream(in1,in2);SequenceInputStream allin = new SequenceInputStream(onein,in3);}
}

案例三:
多个合并流
将in好的对象放到Vector()中,vector.elelment,传到 SequenceInputStream 即可

package day20190725.study01.杂七杂八流;import java.io.*;
import java.util.Enumeration;
import java.util.Vector;/*** @description: 多个流* @author: @李小白* @create: 2019-07-25 16:54*/
public class Demo06合并多个流 {public static void main(String[] args) throws IOException {//SequenceInputStream(Enumeration < ? extends InputStream > e)//通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。FileInputStream in1 = new FileInputStream("a.txt");FileInputStream in2 = new FileInputStream("b.txt");FileInputStream in3 = new FileInputStream("d.txt");FileOutputStream out = new FileOutputStream("c.txt");Vector<FileInputStream> vector = new Vector<>();//返回此向量的组件的枚举。vector.add(in1);vector.add(in2);vector.add(in3);Enumeration<FileInputStream> elements = vector.elements();SequenceInputStream in = new SequenceInputStream(elements);int len=0;byte[] bytes = new byte[1024 * 8];while ((len=in.read(bytes))!=-1) {out.write(len);out.flush();}in.close();out.close();}
}

更多推荐

javaSE基础知识——day16 IO流:数据输入输出流,内存操作流,打印流,随机访问流,序列化流,Properties的使用

本文发布于:2024-02-27 19:16:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765849.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:基础知识   输入输出   内存   操作   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!