2.使用方式
FileInputStream fis1 = new FileInputStream("a.txt"); //创建输入流对象,关联a.txt
FileInputStream fis2 = new FileInputStream("b.txt"); //创建输入流对象,关联b.txt
SequenceInputStream sis = new SequenceInputStream(fis1, fis2); //将两个流整合成一个流
FileOutputStream fos = new FileOutputStream("c.txt"); //创建输出流对象,关联c.txt
int b;
while((b = sis.read()) != -1) { //用整合后的读
fos.write(b); //写到指定文件上
}
sis.close();
fos.close();
FileInputStream fis1 = new FileInputStream("a.txt"); //创建输入流对象,关联a.txt
FileInputStream fis2 = new FileInputStream("b.txt"); //创建输入流对象,关联b.txt
FileInputStream fis3 = new FileInputStream("c.txt"); //创建输入流对象,关联c.txt
Vector v = new Vector<>(); //创建vector集合对象
v.add(fis1); //将流对象添加
v.add(fis2);
v.add(fis3);
Enumeration en = v.elements(); //获取枚举引用
SequenceInputStream sis = new SequenceInputStream(en); //传递给SequenceInputStream构造
FileOutputStream fos = new FileOutputStream("d.txt");
int b;
while((b = sis.read()) != -1) {
fos.write(b);
}
sis.close();
fos.close();
2.使用方式
FileInputStream fis = new FileInputStream("a.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int b;
while((b = fis.read()) != -1) {
baos.write(b);
}
//byte[] newArr = baos.toByteArray(); //将内存缓冲区中所有的字节存储在newArr中
//System.out.println(new String(newArr));
System.out.println(baos);
fis.close();
A:随机访问流概述
B:read(),write(),seek()
2.使用方式
写出: new ObjectOutputStream(OutputStream), writeObject()
public class Demo3_ObjectOutputStream {
/**
* @param args
* @throws IOException
* 将对象写出,序列化
*/
public static void main(String[] args) throws IOException {
Person p1 = new Person("张三", 23);
Person p2 = new Person("李四", 24);
// FileOutputStream fos = new FileOutputStream("e.txt");
// fos.write(p1);
// FileWriter fw = new FileWriter("e.txt");
// fw.write(p1);
//无论是字节输出流,还是字符输出流都不能直接写出对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));//创建对象输出流
oos.writeObject(p1);
oos.writeObject(p2);
oos.close();
}
}
读取: 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 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 list = (ArrayList)ois.readObject(); //泛型在运行期会被擦除,索引运行期相当于没有泛型
//想去掉黄色可以加注解
@SuppressWarnings("unchecked")
for (Person person : list) {
System.out.println(person);
}
ois.close();
2.使用方式
DataOutputStream(OutputStream), writeInt(), writeLong()
DataOutputStream dos = new DataOutputStream(new FileOutputStream("b.txt"));
dos.writeInt(997);
dos.writeInt(998);
dos.writeInt(999);
dos.close();
DataInputStream(InputStream), readInt(), readLong()
DataInputStream dis = new DataInputStream(new FileInputStream("b.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();
1.什么是打印流
System.out就是一个PrintStream, 其默认向控制台输出信息
PrintStream ps = System.out;
ps.println(97); //其实底层用的是Integer.toString(x),将x转换为数字字符串打印
ps.println("xxx");
ps.println(new Person("张三", 23));
Person p = null;
ps.println(p); //如果是null,就返回null,如果不是null,就调用对象的toString()
2.使用方式
打印流只操作数据目的
PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true);
pw.write(97);
pw.print("大家好");
pw.println("你好"); //自动刷出,只针对的是println方法
pw.close();
2.修改标准输入输出流(了解)
System.setIn(new FileInputStream("a.txt")); //修改标准输入流
System.setOut(new PrintStream("b.txt")); //修改标准输出流
InputStream in = System.in; //获取标准输入流
PrintStream ps = System.out; //获取标准输出流
int b;
while((b = in.read()) != -1) { //从a.txt上读取数据
ps.write(b); //将数据写到b.txt上
}
in.close();
ps.close();
System.setIn(new FileInputStream("IO图片.png")); //改变标准输入流
System.setOut(new PrintStream("copy.png")); //改变标准输出流
InputStream is = System.in; //获取标准输入流
PrintStream ps = System.out; //获取标准输出流
int len;
byte[] arr = new byte[1024 * 8];
while((len = is.read(arr)) != -1) {
ps.write(arr, 0, len);
}
is.close();
ps.close();
package com.heima.otherio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class Demo10_Properties {
/**
* @param args
* Properties是Hashtable的子类
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
//demo1();
//demo2();
Properties prop = new Properties();
System.out.println("读取前:"+prop);
prop.load(new FileInputStream("config.properties")); //将文件上的键值对读取到集合中
prop.setProperty("tel", "18988888888");
prop.store(new FileOutputStream("config.properties"), null);//第二个参数是用来描述文件列表的如果不描述,可以传null
System.out.println("读取后:"+prop);
}
public static void demo2() {
Properties prop = new Properties();
prop.setProperty("name", "张三");
prop.setProperty("tel", "18912345678");
//System.out.println(prop);
Enumeration en = (Enumeration) prop.propertyNames();
while(en.hasMoreElements()) {
String key = en.nextElement(); //获取Properties中的每一个键
String value = prop.getProperty(key); //根据键获取值
System.out.println(key + "="+ value);
}
}
public static void demo1() {
Properties prop = new Properties();
prop.put("abc", 123);
System.out.println(prop);
}
}