2017年4月12号 晴 空气质量:良
内容:输入输出流和序列化
一、java流的分类(图示)
二、FileInputStream(字节流输入流)
老师代码:
package cn.bdqn.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStream01 {
/**
* FileInputStream:是输入流!
* 把文件中的内容 读取到 内存中!
*/
public static void main(String[] args) {
FileInputStream fis = null;
try {
// 创建一个输入流对象
fis = new FileInputStream("e:/io.txt");
// 获取课读取的字节数
System.out.println(fis.available());
int num = 0;
while ((num = fis.read()) != -1) {
System.out.println((char)num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close(); // 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、FileOutputStream(字节流输出流)
老师代码:
package cn.bdqn.stream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStream02 {
/**
* FileOutputStream:是输出流!
* 把内存中的内容写入到 文件中!
*/
public static void main(String[] args) {
FileOutputStream fos = null;
try {
// 创建一个输出流对象
// fos = new FileOutputStream("e:/io.txt");
fos = new FileOutputStream("e:/io.txt", true);
// 在内存中创建一个字符串
String words = "7654321";
fos.write(words.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close(); // 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、FileReader(字符流输入流)
老师代码:
package cn.bdqn.stream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReader03 {
/**
* 字符流的基类是 Reader
* Reader下面有一个InputStreamReader
* InputStreamReader有一个FileReader
*/
public static void main(String[] args) {
// 查询当前类的编码格式
System.out.println(System.getProperty("file.encoding"));
FileReader fr = null;
try {
// 获取输入流对象 字符流对象
fr = new FileReader("e:/io.txt");
// 开始读取
int num = 0;
// 创建字符数组 数据的中转站
char[] words = new char[1024];
StringBuffer sb = new StringBuffer();
// 字符串的拼接
while ((num = fr.read(words)) != -1) {
sb.append(words);
}
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();// 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五、BufferedReader(有缓冲区的字符流输入流)
老师代码:
package cn.bdqn.stream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class BufferedReader04 {
/**
* 字符流的基类是 Reader
* Reader下面有一个BufferedReader
* BufferedReader:底层有缓冲区!
* 可以逐行读取!
*/
public static void main(String[] args) {
Reader reader = null;
BufferedReader br = null;
/**
* 01.创建BufferedReader对象! 但是参数需要Reader对象
* 因为Reader是抽象类,不能实例化,
* 我们只能传递它的子类!
*/
try {
reader = new FileReader("e:/io.txt"); // 02.
br = new BufferedReader(reader);
// 把每一行读取的内容给String对象
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 先开的后关闭
try {
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
六、FileWriter(字符流输出流)
老师代码:
package cn.bdqn.stream;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriter05 {
/**
* Writer有个子类叫 OutputStreamWriter
* OutputStreamWriter 有个子类叫 FileWriter
*/
public static void main(String[] args) {
FileWriter fw = null;
try {
// 获取输出流对象 并指定了 输出的位置
fw = new FileWriter("e:/io.txt", true);
fw.write("大家zhen辛苦了!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
七、BufferedWriter(有缓冲区的字符流输出流)
老师代码:
package cn.bdqn.stream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class BufferedWriter06 {
public static void main(String[] args) {
/**
* 01.创建BufferedWiter对象的时候,发现参数让传递 Writer对象
* Writer 是抽象类 只能传递 其子类
*/
Writer writer = null;
BufferedWriter bw = null;
try {
writer = new FileWriter("e:/io.txt", true);// 02.
bw = new BufferedWriter(writer);
// 开始写入数据
bw.write("稍等2分钟!");
bw.write("稍等2分钟!");
bw.newLine();// 换行
bw.write("稍等2分钟!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
八、InputStreamReader(可以指定编码格式的字符输入流)
老师代码:
package cn.bdqn.stream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReader07 {
/**
* FileInputStream:是输入流!
* 把文件中的内容 读取到 内存中!
*/
public static void main(String[] args) {
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
// 创建一个输入流对象 指定从哪个文件中读取数据
fis = new FileInputStream("e:/io.txt");
/**
* 创建InputStreamReader的时候需要传递一个InputStream对象
* InputStream是抽象类
* FileInputStream就是其子类
* 指定编码格式
*/
isr = new InputStreamReader(fis, "utf-8");
br = new BufferedReader(isr);
String w = null;
while ((w = br.readLine()) != null) {
System.out.println(w);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close(); // 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
九、DataInputOrOutputStream(二进制文件的读取与写入)
老师代码:
package cn.bdqn.stream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class DataInputOrOutputStream08 {
/**
* 把e盘下面的cat.jpg 文件(图片)放入到
* e:/java文件夹中 并且更改名称为 小猫咪.jpg
* @throws Exception 你别抛出!
*/
public static void main(String[] args) throws Exception {
/**
* 01.cat.jpg是不是文件?
*/
FileInputStream fis = new FileInputStream("e:/cat.jpg");
// 02.读取2进制文件 创建DataInputStream对象
DataInputStream dis = new DataInputStream(fis);
// 03.创建对应的输出流对象
FileOutputStream fos = new FileOutputStream("e:/java/小猫咪.jpg");
// 04.写入2进制文件 创建DataOutputStream对象
DataOutputStream dos = new DataOutputStream(fos);
int data = 0;
while ((data = dis.read()) != -1) { // 读
// 写
dos.write(data);
}
dos.close();
fos.close();
dis.close();
fis.close();
}
}
十、序列化
老师代码:
1)实体类Student:
package cn.bdqn.serializable;
import java.io.Serializable;
/**
* 持久化:把我们内存中的内容存储到一个存储介质中的过程!
*
* 序列化:只是持久化技术中的一种实现方式!
* 只能把内存中的对象保存到文件中!
*
* 反序列化:把文件中的对象放到内存中!
*
* Serializable:只是说当前类所创建的对象能否被序列化或者反序列化!
*/
public class Student implements Serializable {
private int id;
private int age;
private String name;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student() {
super();
}
public Student(int id, int age, String name, String sex) {
super();
this.id = id;
this.age = age;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + ", name=" + name
+ ", sex=" + sex + "]";
}
}
2)SerializableTest:
package cn.bdqn.serializable;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class SerializableTest {
public static void main(String[] args) throws Exception {
// 01.首先创建一个需要序列化的对象
Student stu = new Student(1, 50, "小白白", "女");
/**
* 02.想序列化 放到文件中
* 从内存 到 文件 输出流
*/
FileOutputStream fos = new FileOutputStream("e:/stu.txt");
// 03.创建序列化对象
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 04.开始序列化
oos.writeObject(stu);
// 05.关闭流
oos.close();
fos.close();
}
}
十 一、反序列化
老师代码:
重新建立一个项目
1)一样的包名一样的实体类Student:
package cn.bdqn.serializable;
import java.io.Serializable;
/**
* 持久化:把我们内存中的内容存储到一个存储介质中的过程!
*
* 序列化:只是持久化技术中的一种实现方式!
* 只能把内存中的对象保存到文件中!
*
* 反序列化:把文件中的对象放到内存中!
*
* Serializable:只是说当前类所创建的对象能否被序列化或者反序列化!
*/
public class Student implements Serializable {
private int id;
private int age;
private String name;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student() {
super();
}
public Student(int id, int age, String name, String sex) {
super();
this.id = id;
this.age = age;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + ", name=" + name
+ ", sex=" + sex + "]";
}
}
2)DeserializedTest
package cn.bdqn.serializable;
//反序列化测试
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class Test {
public static void main(String[] args) throws Exception {
// 从存储介质中 拿到内存中
FileInputStream fis = new FileInputStream("e:/stu.txt");
// 创建反序列化对象
ObjectInputStream ois = new ObjectInputStream(fis);
Student student = (Student) ois.readObject();
System.out.println(student);
ois.close();
fis.close();
}
}
十二、作业和考试
1、作业
1)看视频到XML
2)多线程的MindManager笔记
3)做题
4)练代码
2、考试
2017.04.12
15: 02开始,15:52结束;答题时间:(48-15)=33 分钟;检查时间:2 分钟;
成绩:87.5分
因为自习室座位紧张,所以回家答题,网络不稳定有较严重延时的问题。(答题时间可酌情减去15分钟)
十三、老师辛苦了!