Day22
- IO流(序列流)
(1) 什么是序列流
序列流可以把多个字节输入流整合成一个,从序列流中读取数据时,将从整合的第一个流开始读,读完一个后继续读第二个,以此类推。
(2) 使用方法
整合两个:SequenceInputStream(InputStream, InputStream)
public class Demo1_SquenceInputStream {
public static void main(String[] args) throws 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();
}
private static void demo1() throws IOException {
FileInputStream fis1 = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("c.txt");
int b1;
while ((b1 = fis1.read()) != -1){
fos.write(b1);
}
fis1.close();
FileInputStream fis2 = new FileInputStream("b.txt");
int b2;
while ((b2 = fis2.read()) != -1){
fos.write(b2);
}
fis2.close();
fos.close();
}
}
- IO流(序列流整合多个)
整合多个:SequenceInputStream(Enumeration)
public class Demo1_SquenceInputStream {
/*
整合两个输入流
SequenceInputStream(InputStream s1,InputStream s2)
整合多个输入流
SequenceInputStream(Enumeration<? extends InputStream> e)
*/
public static void main(String[] args) throws IOException {
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();
}
}
- IO流(内存输出流)
(1) 什么是内存输出流:
该输出流可以向内存中写数据,把内存当作一个缓冲区,写出之后可以一次性获取出所有数据。
(2) 使用方式
创建对象:new ByteArrayOutputStream()
写出数据:write(int),write(byte[])
获取数据:toByteArray()
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("a.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();//在内存中创建可以增长的内存数组
int b;
while ((b = fis.read()) != -1) {
baos.write(b); //将读取的数组逐个写到内存中
}
//方法1
//byte[] arr = baos.toByteArray(); //将缓冲区的数据全部获取出来,并赋值给arr
//System.out.println(new String(arr));
//方法2
System.out.println(baos.toString());//将缓冲区的内容转换为了字符串,在输出语句中可以省略调用toString方法
fis.close();
}
}
- IO流(内存输出流之黑马面试题)
定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)
public class Test1 {
/*
定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)
分析:
1:read(byte[] b)是字节流中的方法,创建FileInputStream,关流a.txt
2:创建内存输出流,将读到的数据写到内存输出流中
3:创建字节数组,长度为5
4:将内存输出流的数据全部转换成字符串关闭
5:关闭输入流
*/
public static void main(String[] args) throws IOException {
//1:read(byte[] b)是字节流中的方法,创建FileInputStream,关流a.txt
FileInputStream fis = new FileInputStream("a.txt");
//2:创建内存输出流,将读到的数据写到内存输出流中
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 3:创建字节数组,长度为5
byte[] arr = new byte[5];
int len;
while ((len = fis.read(arr)) != -1){
baos.write(arr,0,len);
// System.out.println(new String(arr,0,len)); //会乱码
}
// 4:将内存输出流的数据全部转换成字符串关闭
System.out.println(baos); //即使没有调用,底层也会默认调用
// 5:关闭输入流
fis.close();
}
}
- IO流(对象操作流objectOutputStream)
(1) 什么是对象操作流
该流可以将一个对象写出,或者读取一个对象到程序中,也就是执行了序列化和反序列化的操作。
(2) 使用方式:
写出:new objectOutputStream(OutputStream),writeobject()
public class Demo3_ObjectOutputStream {
/*
序列化:将对象写到文件上
*/
public static void main(String[] args) throws IOException {
Person p1 = new Person("张三",23);
Person p2 = new Person("李四",24);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));
oos.writeObject(p1);
oos.writeObject(p2);
oos.close();
}
}
- IO流(对象操作流objectInputStream)
读取:new objectInputStream(InputStream),readobject()
public class Demo4_ObjectIntputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream obis = new ObjectInputStream(new FileInputStream("e.txt"));
Person p1 =(Person) obis.readObject();
Person p2 = (Person) obis.readObject();
// Person p3 = (Person) obis.readObject(); //当文件读到了末尾时出现EOFException
System.out.println(p1);
System.out.println(p2);
obis.close();
}
}
- IO流(对象操作流优化)
将对象存储在集合中写出
把整个集合对象一次写出
public static void main(String[] args) throws IOException {
Person p1 = new Person("张三",23);
Person p2 = new Person("李四",24);
Person p3 = new Person("王五",25);
Person p4 = new Person("赵六",26);
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();
}
把整个集合对象一次读取
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream obis = new ObjectInputStream(new FileInputStream("e.txt"));
ArrayList<Person> list = (ArrayList<Person>) obis.readObject();
for(Person person : list) {
System.out.println(person);
}
obis.close();
}
- IO流(加上id号)
注意:
要写出的对象必须实现Serializable接口才能被序列化
System.out就是一个PrintStream,其默认向控制台输出信息 - IO流(打印流的概述和特点)
(1) 什么是打印流
该流可以很方便的将对象的toString()结果输出,并且自动加上换行,而且可以使用自动刷出的模式。
System.out就是一个PrintStream,其默认向控制台输出信息
public class Demo5_PrintStream {
public static void main(String[] args) {
System.out.println("aaa");
PrintStream ps = System.out; //获取标注输出流
ps.println(97); //底层通过Integer.toString()将97转换成字符串并打印
ps.write(97); //查找码表,找到对应的a并打印
Person p1 = new Person("张三",23);
ps.println(p1); //调用p1的toString()方法
Person p2 = null; //打印引用数据类型,如果是null就打印null,如果不是就调用对象的toString()方法
ps.println(p2);
ps.close();
}
}
(2) 使用方式
打印:print(),println()
自动刷出:PrintWriter(OutputStream,Boolean autoFlush,String encoding)
打印流只操作数据目的
public class Demo5_PrintStream {
/*
PrintStream和PrintWriter分别是打印的字节流和字符流,值操作数据目的的
*/
public static void main(String[] args) throws FileNotFoundException {
PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"),true);
pw.println(97); //自动刷出功能,只针对println方法
// pw.write(97);
// pw.close();
}
}
- IO流(标准输入输出流概述和输出语句)
(1) 什么是标准输入输出流
System.in是InputStream,标准输入流,默认可以从键盘输入读取字节数据
System.out是PrintStream,标准输出流,默认可以向Console中输出字符和字节数据。
(2) 修改标准输入输出流
修改输入流:System.setIn(InputStream)
修改输出流:System.setOut(OutputStream)
public class Demo6_SystemInOut {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("a.txt")); //改变标准输入流
System.setOut(new PrintStream("b.txt")); //改变标准输出流
InputStream is = System.in; //获取标准的键盘输入流,默认指向键盘,改变之后指向文件
PrintStream ps = System.out; //获取标准输出流,默认指向控制台,改变后指向文件
int b ;
while ((b = is.read()) != -1) {
ps.write(b);
}
is.close();
ps.close();
}
private static void demo1() throws IOException {
InputStream is = System.in;
int x = is.read();
System.out.println(x);
is.close();
InputStream is2 = System.in;
int y = is2.read();
System.out.println(y);
}
}
- 修改标准输入输出流和拷贝图片
public class Test2 {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("01.jpg")); //改变标准输入流
System.setOut(new PrintStream("02.jpg"));//改变标准输出流
InputStream is = System.in;
PrintStream ps = System.out;
byte[] arr = new byte[1024];
int len;
while ((len = is.read()) != -1) {
ps.write(arr,0,len);
}
is.close();
ps.close();
}
}
- IO流(两种方式实现键盘录入)
(1) BufferedReader的readLine方法。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)
(2) Scanner
public class Demo7_SystemIn {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
System.out.println(line);
sc.close();
}
private static void demo1() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//InputStreamReader转换流
String line = br.readLine();
System.out.println(line);
br.close();
}
}
- IO流(随机访问流概述和写出数据)
(1) 随机访问流概述:
RandomAccesFile概述
RandomAccesFile类不属于流,是object类的子类。但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入。
(2) read(),write(),seek()
public class Demo8_RandomAccessFile {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("g.txt","rw");
// raf.write(97);
//int x = raf.read();
//System.out.println(x);
raf.seek(10); //在指定位置设置指针
raf.write(98);
raf.close();
}
}
- IO流(数据输入输出流)
(1) 数据输入输出流
DataInputStream, DataOutputStream可以按照基本数据类型大小读写数据
例如按Long大小写出一个数字,写出时该数据占8字节,读取的时候也可以按照Long类型读取,一次读取8个字节。
(2) 使用方式
DataOutputStream(OutputStream),writeInt(),writeLong()
DataInputStream(InputStream),readInt(),readLong()
public class Demo9_Data {
/*
00000000 00000000 00000011 11100101 int类型997
11100101
*/
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("h.txt"));
int x = dis.readInt();
int y = dis.readInt();
int z = dis.readInt();
System.out.println(x);
System.out.println(y);
System.out.println(z);
dis.close();
}
private static void demo3() throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("h.txt"));
dos.write(997);
dos.write(998);
dos.write(999);
dos.close();
}
private static void demo2() throws IOException {
FileInputStream fis = new FileInputStream("h.txt");
int x = fis.read();
int y = fis.read();
int z = fis.read();
System.out.println(x);
System.out.println(y);
System.out.println(z);
fis.close();
}
private static void demo1() throws IOException {
FileOutputStream fos = new FileOutputStream("h.txt");
fos.write(997);
fos.write(998);
fos.write(999);
fos.close();
}
}
- IO流(Properties的概述和作为Map集合的使用)
(1) Properties的概述
Properties类表示了一个持久的属性集。
Properties可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
(2) 案例演示
Properties作为Map集合的使用
public class Demo10_Properties {
/*
Properties是Hashtable的子类
*/
public static void main(String[] args) {
Properties pro = new Properties();
pro.put("abc",123);
System.out.println(pro);
}
}
- IO流(Properties的特殊功能使用)
(1) Properties的特殊功能
public Object setProperty(String key,String value)
public String getProperty(String key)
public Enumeration stringPropertyNames()
(2) 案例演示
Properties的特殊功能
public class Demo10_Properties {
/*
Properties是Hashtable的子类
*/
public static void main(String[] args) {
//demo1();
Properties prop = new Properties();
prop.setProperty("name","张三");
prop.setProperty("tel","12233334444");
//System.out.println(prop);
Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
while (en.hasMoreElements()){
String key = en.nextElement(); //获取Properties中的每一个键
String value = prop.getProperty(key); //根据键获取值
System.out.println(key + "=" + value);
}
}
private static void demo1() {
Properties pro = new Properties();
pro.put("abc",123);
System.out.println(pro);
}
}
- IO流(Properties的load()和store()功能)
(1) Properties的load()和store()功能
(2) 案例演示
Properties的load()和store()功能
public class Demo10_Properties {
/*
Properties是Hashtable的子类
*/
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));//将文件的键读取到集合中
prop.setProperty("tel","1892222333");
prop.store(new FileOutputStream("config.properties"),null);//第二个参数是对列表参数的描述,可以给值,也可以给null
System.out.println(prop);
}
}