------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
###15.01_IO流(序列流)
* 1.什么是序列流
* 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.
* 就是把 对象 存储到流中
* ObjectOutputStream
*反序列化流:
* 就是从流中 读取对象
* ObjectInputStream
* 需要注意的是:无论使用的是序列化流还是反序列化流,都需要当前对象先进行序列化操作
* java.io.NotSerializableException: cn.itcast_06_SerializableStream.Person
* NotSerializableException - 某个要序列化的对象不能实现 java.io.Serializable 接口
* 说明当前Person类 没有实现序列化操作, 需要实现序列化
* public interface Serializable
* 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
序列化接口没有方法或字段,仅用于标识可序列化的语义
关键字 transient 临时的 暂时的
作用: 使用transient修饰的成员,在序列化操作时,可以不参与序列化操作
* 2.使用方式
* 整合两个: SequenceInputStream(InputStream, InputStream)
<span style="font-size:14px;"> //通过反序列化流,从流中 读取Person对象
private static void read() throws IOException, ClassNotFoundException {
//创建 对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("序列化.txt"));
//从流中读取对象
Object obj = ois.readObject();
System.out.println(obj.toString());
//关闭流对象
ois.close();
}
//通过序列化流,把Person对象写入到流中
private static void write() throws IOException {
//创建 对象输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("序列化.txt"));
//写对象到流中
Person p = new Person("虎哥", 20);
oos.writeObject(p);
//关闭流对象
oos.close();</span>
###15.02_IO流(内存输出流及输入流*****)
* 1.什么是内存输出流
* 该输出流可以向内存中写数据, 把内存当作一个缓冲区, 写出之后可以一次性获取出所有数据
数据输出流: 把java中基本数据类型数据 写到流中
* DataOutputStream
* 方法: writeXxx(Xxx obj) 把Xxx类型的元素 写到流中
数据输入流 : 把java中的基本数据类型数据在流中读取
* DataInputStream
* 方法: readXxx() 把Xxx类型的元素 从流中读取
* 2.使用方式
* 创建对象: new ByteArrayOutputStream()
* 写出数据: write(int), write(byte[])
* 获取数据: toByteArray()
<span style="font-size:14px;"> 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();
</span>
###15.03_IO流(内存输出流的常见面试题)
* 定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)
<span style="font-size:14px;">FileInputStream fis = new FileInputStream("a.txt"); //创建字节输入流,关联a.txt
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //创建内存输出流
byte[] arr = new byte[5]; //创建字节数组,大小为5
int len;
while((len = fis.read(arr)) != -1) { //将文件上的数据读到字节数组中
baos.write(arr, 0, len); //将字节数组的数据写到内存缓冲区中
}
System.out.println(baos); //将内存缓冲区的内容转换为字符串打印
fis.close();
</span>
###15.04_IO流(对象操作流ObjecOutputStream)
* 1.什么是对象操作流
* 该流可以将一个对象写出, 或者读取一个对象到程序中. 也就是执行了序列化和反序列化的操作.
* 2.使用方式
* 写出: new ObjectOutputStream(OutputStream), writeObject()
<span style="font-size:14px;"> 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();
}
}
</span>
###15.05_IO流(打印流的概述和特点)
* 1.什么是打印流
* 该流可以很方便的将对象的toString()结果输出, 并且自动加上换行, 而且可以使用自动刷出的模式
* System.out就是一个PrintStream, 其默认向控制台输出信息
练习: 复制文本文件, 采用打印流
* 数据源: UIUtil.java -- 输入流 -- 字符输入流 -- 字符缓冲输入流 -- BufferedReader
* 目的地: Copy.java -- 输出流 -- 字符输出流 -- 打印流 -- PrintWriter
分析:
* 1: 封装数据源,为了读取数据,输入流
* 2:封装目的地.为了写数据
* 3:读
* 4:写
* 5:关闭流
* 2.使用方式
* 打印: print(), println()
* 自动刷出: PrintWriter(OutputStream out, boolean autoFlush, String encoding)
* 打印流只操作数据目的
<span style="font-size:14px;">public class CopyText {
public static void main(String[] args) throws IOException {
//1: 封装数据源,为了读取数据,输入流
BufferedReader br = new BufferedReader(new FileReader("UIUtil.java"));
//2:封装目的地.为了写数据
PrintWriter pw = new PrintWriter(new FileWriter("Copy.java"), true);//开启自动刷新
//3:读
String line = null;
while ((line = br.readLine()) != null) {
//写
//bw.write(line);
//bw.newLine();
//bw.flush();
pw.println(line);
}
//5:关闭流
br.close();
pw.close();
}
}
</span>
###15.06_IO流(标准输入输出流概述和输出语句)
* 1.什么是标准输入输出流
* System.in是InputStream, 标准输入流, 默认可以从键盘输入读取字节数据
* System.out是PrintStream, 标准输出流, 默认可以向Console中输出字符和字节数据
* 系统输入输出流:
* public static final InputStream in “标准”输入流 System.in
* 此流对应于键盘输入
* InputStream in = System.in;//字节输入流
* public static final PrintStream out “标准”输出流 System.out
* 此流对应于显示器输出
* PrintStream out = System.out; // 字节打印输出流
* 2.修改标准输入输出流
* 修改输入流: System.setIn(InputStream)
* 修改输出流: System.setOut(PrintStream)
<span style="font-size:14px;"> //2:通过java命令,运行.class文件的时候 在后面添加想要输入的内容
System.out.println("长度为:"+args.length);
for (String s : args) {
System.out.println(s);
}
*/
//3: System.in
//InputStream in = System.in;//标准输入流
//Reader r = new InputStreamReader(in);//转换流, 把字节流转换成 字符流
//BufferedReader br = new BufferedReader(r); //基本字符流,包装成高效的流
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
System.out.println(line);
</span>
###15.07_IO流(修改标准输出流)
数据输出的方式:
* 方式1:System.out.println() (推荐)
*
* 方式2:System.out 标准输出流
代码演示:
<span style="font-size:14px;">//方式1:System.out.println()
System.out.println("abcde");
//方式2:System.out 标准输出流
//PrintStream out = System.out;//字节打印输出流
//Writer w = new OutputStreamWriter(out);
//BufferedWriter bw = new BufferedWriter(w);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("abcde");
bw.newLine();
bw.flush();
bw.close();
</span>
###15.08_IO流(两种方式实现键盘录入)
* A:BufferedReader的readLine方法。
* BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
* B:Scanner
###15.09_IO流(随机访问流概述和写出数据)
* A:随机访问流概述
* RandomAccessFile概述
* RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
* 支持对随机访问文件的读取和写入。
RandomAccessFile: 随机访问文件流
* 特点: 它不是一个流,它是一个对象,它的父类是Object
* 它里面包含输入流与输出流,通过这两个流,实现对文件进行随机的读或写操作
* 构造方法:
* RandomAccessFile(File file, String mode)
* RandomAccessFile(String name, String mode)
* 参数2:
* mode 参数指定用以打开文件的访问模式。允许的值及其含意为: 值 含意
"r" 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
"rw" 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。 EOFException - 如果在读取两个字节之前此文件已到达末尾
方法:
public void seek(long pos)
设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
<span style="font-size:14px;">public class RandomAccessFileDemo {
public static void main(String[] args) throws IOException {
//创建一个随机访问流对象
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
//写数据
raf.writeChar('a');
//raf.write(100);
//raf.writeUTF("中国");
//移动指针
raf.seek(0);
//取数据
// boolean readBoolean = raf.readBoolean();//EOFException 访问到了流的末尾还继续访问
// System.out.println(readBoolean);
char readChar = raf.readChar();
System.out.println(readChar);
// int readInt = raf.readInt();
// System.out.println(readInt);
// String readUTF = raf.readUTF();
// System.out.println(readUTF);
}
}
</span>
对于随机访问流我们用图例表示:
###15.10_IO流(数据输入输出流)
* 1.什么是数据输入输出流
* DataInputStream, DataOutputStream可以按照基本数据类型大小读写数据
* 例如按Long大小写出一个数字, 写出时该数据占8字节. 读取的时候也可以按照Long类型读取, 一次读取8个字节.
* 2.使用方式
* DataOutputStream(OutputStream), writeInt(), writeLong()
<span style="font-size:14px;"> 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();
</span>
###15.11_IO流(Properties的概述和作为Map集合的使用)
* A:Properties的概述
* Properties 类表示了一个持久的属性集。
* Properties 可保存在流中或从流中加载。
* 属性列表中每个键及其对应值都是一个字符串。
特点:
* 它是Hashtable的子类
* 它的键和值都是String类型
* 它可以从流中读取数据,也可以把数据写到流中
* B:案例演示
* Properties作为Map集合的使
<span style="font-size:14px;">public class PropertiesDemo {
public static void main(String[] args) {
//创建Properties集合
Properties prop = new Properties();
//添加元素到集合
prop.put("itcast001", "杨彪");
prop.put("itcast002", "杨虎");
//遍历集合
//Set<String> keys = prop.keySet();
Set<String> keys = prop.stringPropertyNames(); // 相当于 keySet()方法, 返回键的集合
for (String key : keys) {
Object value = prop.get(key);
System.out.println(key +"--"+value);
}
}
}
</span>
###15.12_IO流(Properties的特殊功能使用)
* A:Properties的特殊功能
public String getProperty(String key) -- get(E key)
* 键找值方法
* public String getProperty(String key, String defaultValue)
* 键找值方法, 如果当前的键,没有找到,会返回指定的默认值
* public Object setProperty(String key, String value)
* 调用 Hashtable 的方法 put。
* public Set<String> stringPropertyNames()
* 返回Properties集合中所有键的集合
* B:案例演示
* Properties的特殊功能
<span style="font-size:14px;">public class PropertiesDemo2 {
public static void main(String[] args) {
Properties prop = new Properties();
//添加元素到集合
prop.setProperty("itcast001", "张三");
prop.setProperty("itcast002", "李四");
//键找值的方式遍历
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
String value = prop.getProperty(key);
System.out.println(key + "--" + value);
}
//public String getProperty(String key, String defaultValue)
//String name = prop.getProperty("itcast001", "默认名字");
String name = prop.getProperty("itcast003", "默认名字");
System.out.println(name);
//显示
//System.out.println(prop);
}
}
</span>
###15.13_IO流(Properties的load()和store()功能)
* A:Properties的load()和list()功能
把集合存储到流所对应的文件中
* 不能添加描述信息
* list(PrintStream out)
* list(PrintWriter out)
* 可以添加描述信息
* void store(OutputStream out, String comments)
* void store(Writer writer, String comments)
* 从流中读取数据到集合中
* public void load(Reader reader)
public void load(InputStream inStream)
* B:案例演示
* Properties的load()和list()功能
<span style="font-size:14px;"> private static void read() throws IOException {
//创建空的properties集合,用来存储数据
Properties prop = new Properties();
//从流中读取数据,存储到空的集合中
FileReader fr = new FileReader("properties.txt");
prop.load(fr);
//关闭流
fr.close();
//显示集合中的数据
System.out.println(prop);
}
public static void write() throws IOException {
//创建properties集合
Properties prop = new Properties();
prop.setProperty("01", "haha");
prop.setProperty("02", "hehe");
FileWriter fw = new FileWriter("properties.txt");
prop.store(fw, "helloworld");
fw.close();</span>
jdk7中的类
*
* Files : 文件工具类
* 文件的复制
* public static long copy(Path source, OutputStream out)
* 把指定的文件中的数据 写到 指定的输出流所对应的文件中
*
* public static Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options)
* 把指定的集合元素 写到 流所对应的文件中
* 参数:
* path : 把数据存储到哪个路径文件中
* lines: 集合
* cs : 字符编码表