数据输入输出流的概述和使用
A:数据输入输出流的概述
通过API查看
数据输入和输出流:
数据输入流: DataInputStream
数据输出流: DataOutputStream
特点: 可以写基本数据类型,可以读取基本数据类型
B:案例演示: 数据输入输出流的使用
案例:
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
//字节流
//数据输入输出流,此流最大的特点,是能够读写基本数据类型
// DataInputStream
// DataOutputStream
//writeData();
//你怎么写的就怎么读取,顺序不要乱
DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));
boolean b = in.readBoolean();
double v = in.readDouble();
char c = in.readChar();
int i = in.readInt();
System.out.println(b);
System.out.println(v);
System.out.println(c);
System.out.println(i);
}
private static void writeData() throws IOException {
DataOutputStream out = new DataOutputStream(new FileOutputStream("a.txt"));
out.writeBoolean(true);
out.writeDouble(3.14);
out.writeChar('a');
out.writeInt(2000);
out.close();
}
}
内存操作流的概述和使用
A:内存操作流的概述
a:操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
此流关闭无效,所以无需关闭
b:操作字符数组
CharArrayWrite
CharArrayReader
c:操作字符串
StringWriter
StringReader
B:案例演示: 内存操作流的使用
// 构造方法: public ByteArrayOutputStream()
案例:
import java.io.*;
public class MyTest2 {
public static void main(String[] args) throws IOException {
//内存操作流:此流不关联文件,不直接读写文件,就在内存中操作数据
//A:
//内存操作流的概述
//a:
//操作字节数组
// ByteArrayOutputStream
//ByteArrayInputStream
//此流关闭无效,所以无需关闭
//b:
//操作字符数组
// CharArrayWrite
//CharArrayReader
//c:
//操作字符串
// StringWriter
// StringReader
//ByteArrayOutputStream
//ByteArrayInputStream
// ByteArrayOutputStream 此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray () 和 toString () 获取数据。
//关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。
//ByteArrayOutputStream()
//创建一个新的 byte 数组输出流。
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write("英雄儿女".getBytes());
outputStream.write("上甘岭".getBytes());
outputStream.write("歌唱我们美丽的祖国".getBytes());
//取出这个内存操作流,所维护的缓冲区
byte[] bytes = outputStream.toByteArray();
//System.out.println(new String(bytes));
// System.out.println(outputStream.toString());
//outputStream.close(); //此流无需关闭
//ByteArrayInputStream( byte[] buf)
//创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
byte[] bytes1 = new byte[1024];
int len = byteArrayInputStream.read(bytes1);
System.out.println(new String(bytes1, 0, len));
}
}
案例二:
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
public class MyTest3 {
public static void main(String[] args) throws IOException {
//操作字符数组
// CharArrayWrite
//CharArrayReader
CharArrayWriter charArrayWriter = new CharArrayWriter();
charArrayWriter.write(new char[]{'a','b','c'});
charArrayWriter.write("好好学习,天天向上,爱生活,爱Java");
char[] chars = charArrayWriter.toCharArray();
System.out.println(String.valueOf(chars));
String s1 = new String(chars);
System.out.println(s1);
String s = charArrayWriter.toString();
System.out.println(s);
}
}
案例三:
import java.io.StringWriter;
public class MyTest4 {
public static void main(String[] args) {
// StringWriter
// StringReader
StringWriter stringWriter = new StringWriter();
stringWriter.write("abc");
stringWriter.write("ddd");
String s = stringWriter.toString();
System.out.println(s);
}
}
案例四:
多首mp3合成一首
//将多个文件合成一个文件
//多首mp3,合并成一首mp3
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class MyTest {
public static void main(String[] args) throws IOException {
FileInputStream in1 = new FileInputStream("许巍-曾经的你.mp3");
FileInputStream in2 = new FileInputStream("许巍-蓝莲花.mp3");
FileOutputStream out = new FileOutputStream("许巍专辑大联唱.mp3");
//方式1
ArrayList<FileInputStream> list = new ArrayList<>();
list.add(in1);
list.add(in2);
//遍历集合往出写
int len = 0;
byte[] bytes = new byte[1024 * 8];
for (FileInputStream in3 : list) {
while ((len = in3.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
in3.close();
}
out.close();
}
}
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import java.io.*;
import java.util.ArrayList;
public class MyTest2 {
public static void main(String[] args) throws IOException {
FileInputStream in1 = new FileInputStream("许巍-曾经的你.mp3");
FileInputStream in2 = new FileInputStream("许巍-蓝莲花.mp3");
FileOutputStream out = new FileOutputStream("E:\\许巍专辑大联唱.mp3");
//方式2
ArrayList<FileInputStream> list = new ArrayList<>();
list.add(in1);
list.add(in2);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int len = 0;
byte[] bytes = new byte[1024 * 8];
for (FileInputStream in3 : list) {
while ((len = in3.read(bytes)) != -1) {
bout.write(bytes, 0, len);
}
in3.close();
}
//取出两首歌的数据
byte[] allBytes = bout.toByteArray();
System.out.println(allBytes.length);
ByteArrayInputStream bin = new ByteArrayInputStream(allBytes);
int len2 = 0;
byte[] bytes1 = new byte[1024 * 8];
while ((len2 = bin.read(bytes1)) != -1) {
out.write(bytes1, 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的子类使用
案例:
import java.io.IOException;
import java.io.PrintStream;
public class MyTest {
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("b.txt");
//ps.write("一行数据".getBytes());
//ps.print(100);
ps.println("asfdasdfasdf");
ps.println("asfdasdfasdf");
ps.println("asfdasdfasdf");
ps.println("asfdasdfasdf");
ps.println("asfdasdfasdf");
ps.println("asfdasdfasdf");
ps.println("asfdasdfasdf");
ps.println("asfdasdfasdf");
ps.close();
}
}
案例二:
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class MyTest2 {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter printWriter = new PrintWriter("c.txt");
printWriter.write("abc");
printWriter.println("cccc");
printWriter.flush();
printWriter.close();
}
}
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 的其中一个方法时才可能完成此操作,
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class MyTest3 {
public static void main(String[] args) throws FileNotFoundException {
//PrintWriter(OutputStream out, boolean autoFlush)
PrintWriter printWriter = new PrintWriter(new FileOutputStream("d.txt"),true);
//printWriter.write("abc");
//printWriter.write("\r\n");
//
//printWriter.write("abc");
//printWriter.write("\r\n");
//
//printWriter.write("abc");
//printWriter.write("\r\n");
//printWriter.write("abc");
//printWriter.write("\r\n");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.println("asddsfasdf");
printWriter.close();
}
}
打印流复制文本文件
A:案例演示: 打印流复制文本文件
分析:
- 这个打印流只能进行写数据,不能进行读取数据,
那么我们应该找一个可以读取文本文件中的的数据的流对象进行读取操作. - 而我们非常喜欢高效的流对象,于是我们可以使用BufferedReader进行读取数据.
*
案例:
使用 PrintWriter 跟一个输入流配合,完成一个文本文件的复制
//使用 PrintWriter 跟一个输入流配合,完成一个文本文件的复制
import java.io.*;
public class MyTest4 {
public static void main(String[] args) throws IOException {
BufferedReader bfr = new BufferedReader(new FileReader("MyTest.java"));
PrintWriter printWriter = new PrintWriter(new FileOutputStream("test.txt"), true);
String line=null;
while ((line=bfr.readLine())!=null){
printWriter.println(line);
}
bfr.close();
printWriter.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));
案例:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class MyTest {
public static void main(String[] args) throws IOException {
//键盘录入的第二种方式
//Scanner scanner = new Scanner(System.in);
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
while (true){
System.out.println("请输入数据");
String s = bfr.readLine();
if("886".equals(s)){ //自定义一个结束标记
break;
}
System.out.println(s);
}
}
}
随机访问流概述和写出数据
A:随机访问流概述 RandomAccessFile概述 最大特点 能读能写 RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。 支持对随机访问文件的读取和写入。 RandomAccessFile的父类是Object , 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据.
我们可以通过getFilePointer方法获取文件指针,并且可以通过seek方法设置文件指针
B:案例演示: 随机访问流写出数据
案例:
//随机访问流:此流最大的特点是能读能写,而来可以设置文件指针的位置
//此类的实例支持对随机访问文件的读取和写入。
//随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。
//存在指向该隐含数组的光标或索引,称为文件指针;
//输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。
package org.westos.demo6;
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 {
// 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 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
像这样一个接口中如果没有方法,那么这样的接口我们将其称之为标记接口(用来给类打标记的,相当于猪肉身上盖个章)
一个对象可以被序列化的前提是这个对象对应的类必须实现Serializable接口
案例:
import java.io.Serializable;
//Serializable 序列化接口 标记接口
public class Student implements Serializable {
private static final long serialVersionUID = 5455576216538966789L;
//定义一个UID
public String name;
//transient 修饰的这个字段,不需要保存到,文件中去
// public transient int age;
public int age;
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;
}
}
import java.io.*;
import java.util.ArrayList;
public class MyTest {
public static void main(String[] args) throws IOException {
// 序列化流和反序列化流的概述和使用
//序列化:将一个对象,保存到文件中
//方序列化:将对象再读取到内存中
//序列化流:
//ObjectOutputStream
//反序列化流:
//ObjectInputStream
//要保证一个对象,能被正确的序列化的硬盘上,需要该对象对应类实现一个序列化接口
Student student = new Student("张三", 23);
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("student.txt"));
objOut.writeObject(student);
objOut.close();
}
}
案例二:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class MyTest2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//序列化注意事项:1.需要该类实现一个序列化接口Serializable
// 2.再加一个静态常量的UID
//3.使用transient关键字声明不需要序列化的成员变量
//反序列化:
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("student.txt"));
Object obj = inputStream.readObject();
Student student = (Student) obj;
System.out.println(student.name + "==" + student.age);
}
}
案例三:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class MyTest3 {
public static void main(String[] args) throws IOException {
Student student1 = new Student("张三", 23);
Student student2 = new Student("李四", 24);
Student student3 = new Student("王五", 25);
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("student.txt"));
objOut.writeObject(student1);
objOut.writeObject(student2);
objOut.writeObject(student3);
}
}
案例四:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class MyTest3 {
public static void main(String[] args) throws IOException {
Student student1 = new Student("张三", 23);
Student student2 = new Student("李四", 24);
Student student3 = new Student("王五", 25);
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("student.txt"));
objOut.writeObject(student1);
objOut.writeObject(student2);
objOut.writeObject(student3);
}
}
案例五:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class MyTest5 {
public static void main(String[] args) throws IOException {
Student student1 = new Student("张三", 23);
Student student2 = new Student("李四", 24);
Student student3 = new Student("王五", 25);
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("student.txt"));
ArrayList<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
objOut.writeObject(list);
objOut.close();
}
}
案例六:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class MyTest6 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("student.txt"));
Object obj = inputStream.readObject();
ArrayList<Student> list = (ArrayList<Student>) obj;
Student student = list.get(2);
System.out.println(student.name + "===" + student.age);
inputStream.close();
}
}
Properties的概述和作为Map集合的使用
A:Properties的概述
查看API
Properties 类表示了一个持久的属性集。
Properties 可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
Properties父类是Hashtable
- 属于双列集合,这个集合中的键和值都是字符串 Properties不能指定泛型
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”
}
}
案例二:
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class MyTest2 {
public static void main(String[] args) {
// Properties 属性集合 双列集合 键值默认都是字符串类型
Properties properties = new Properties();
//properties.put("武二", "嫂嫂");
//properties.keySet();
//Set<Map.Entry<Object, Object>> entries = properties.entrySet();
//Object obj = properties.get("武二");
//用它特有的方法,来存取数据
properties.setProperty("武二", "嫂嫂");
String v = properties.getProperty("武二2");
//通过这个键没有找到这个值,会返回默认值
String v2 = properties.getProperty("武督头", "金莲");
System.out.println(v);
System.out.println(v2);
}
}
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()功能
案例:
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
public class MyTest3 {
public static void main(String[] args) throws IOException {
//将双列集合中的数据,存到文本文件中
//HashMap<String, String> hashMap = new HashMap<>();
//hashMap.put("武二", "嫂嫂");
//hashMap.put("西门大官人", "李瓶儿");
Properties properties = new Properties();
properties.setProperty("武二", "嫂嫂");
properties.setProperty("西门大官人", "李瓶儿");
//将属性集合中的键值对数据,存到文本文件中
//参2 文件的注释
properties.store(new FileWriter("sh.properties"),null);
}
}
案例二:
import jdk.nashorn.internal.runtime.regexp.joni.constants.EncloseType;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class MyTest4 {
public static void main(String[] args) throws IOException {
//把一个配置文件中的键值对数据,读取到双列集合中
//HashMap<String, String> hashMap = new HashMap<>();
//Scanner scanner = new Scanner(new FileInputStream("pz.properties"));
//String s = scanner.nextLine();
//String[] split = s.split("=");
//hashMap.put(split[0],split[1]);
//System.out.println(hashMap);
Properties properties = new Properties();
properties.load(new FileInputStream("pz.properties"));
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key+"==="+value);
}
}
}
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 型参数
案例一:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class MyTest {
public static void main(String[] args) throws IOException {
//A:案例需求:将a.txt和b.txt两个文本文件的内容合并到c.txt
FileInputStream in1 = new FileInputStream("test.txt");
FileInputStream in2 = new FileInputStream("test2.txt");
FileOutputStream outputStream = new FileOutputStream("all.txt");
ArrayList<FileInputStream> list = new ArrayList<>();
list.add(in1);
list.add(in2);
int len = 0;
byte[] bytes = new byte[1024];
for (FileInputStream inputStream : list) {
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
inputStream.close();
}
outputStream.close();
}
}
案例二:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
public class MyTest2 {
public static void main(String[] args) throws IOException {
//SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
//SequenceInputStream(InputStream s1, InputStream s2)
//通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),以提供从此 SequenceInputStream 读取的字节。
FileInputStream in1 = new FileInputStream("test.txt");
FileInputStream in2 = new FileInputStream("test2.txt");
FileOutputStream outputStream = new FileOutputStream("all2.txt");
SequenceInputStream allIn = new SequenceInputStream(in1, in2);
int len = 0;
byte[] bytes = new byte[1024];
while ((len = allIn.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
allIn.close();
outputStream.close();
}
}
案例三:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
public class MyTest3 {
public static void main(String[] args) throws IOException {
//把三个文本文件,合并成一个文件
FileInputStream in1 = new FileInputStream("test.txt");
FileInputStream in2 = new FileInputStream("test2.txt");
FileInputStream in3 = new FileInputStream("all.txt");
FileOutputStream outputStream = new FileOutputStream("all2.txt");
SequenceInputStream allIn = new SequenceInputStream(in1, in2);
SequenceInputStream maxIn = new SequenceInputStream(allIn, in2);
}
}
案例四:
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 MyTest4 {
public static void main(String[] args) throws IOException {
//SequenceInputStream(Enumeration < ? extends InputStream > e)
//通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。
FileInputStream in1 = new FileInputStream("test.txt");
FileInputStream in2 = new FileInputStream("test2.txt");
FileInputStream in3 = new FileInputStream("all.txt");
FileOutputStream outputStream = new FileOutputStream("allall.txt");
Vector<FileInputStream> vector = new Vector<>();
//Enumeration<E> elements ()
//返回此向量的组件的枚举。
vector.add(in1);
vector.add(in2);
vector.add(in3);
Enumeration<FileInputStream> enumeration = vector.elements();
SequenceInputStream allIn = new SequenceInputStream(enumeration);
int len = 0;
byte[] bytes = new byte[1024];
while ((len = allIn.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
allIn.close();
outputStream.close();
}
}