打印流
- package IO流.特殊操作流;
- /*
- 打印流分类:
- 字节打印流:PrintStream
- 字符打印流:PrintWriter
- 打印流特点:
- 只负责输出数据,不负责读取数据
- 有自己的特有方法
- 字节打印流
- PrintStream(String fileName)使用指定的文件名创建新的打印流
- 使用继承父类的方法写数据,查看的时候会转码
- 使用字节的特有方法写数据,查看的数据原样输出
- */
-
- import java.io.*;
-
- public class 打印流 {
- public static void main(String[] args) throws IOException {
- /* //PrintStream(String fileName)使用指定的文件名创建新的打印流
- PrintStream ps = new PrintStream("idea_text\\ps.txt");
- //写数据
- //字节输出流特有方法
- ps.write(97);
- //使用特有方法写数据
- ps.print(97);
- ps.print(98);
- ps.println(97);
- ps.println(98);
- //释放资源
- ps.close();*/
-
-
-
- /* //字符打印流PrintWriter的构造方法
- PrintWriter(String fileName)
- 使用指定的文件名创建一个新的PrintWriter,而不需要自动执行行刷新。
- PrintWriter(Writer out, boolean autoFlush)
- 创建一个新的PrintWriter。
- out字符输出流,autoFlush布尔值,如果为真,则print,println,或format方法将刷新输出缓冲区
- */
- PrintWriter pw=new PrintWriter(new FileWriter("idea_text\\ps.txt"));
- pw.println("hello");
- pw.println("world");
-
-
-
-
-
- }
- }
复制java文件打印流改进版
- package IO流.特殊操作流;
-
- import java.io.*;
-
- public class 复制java文件打印流改进版 {
- public static void main(String[] args) throws IOException {
- //根据数据源创建字符输入流对象
- BufferedReader br = new BufferedReader(new FileReader("1"));
- //根据目的地创建字符输出流对象
- PrintWriter pw=new PrintWriter(new FileWriter("2"));
-
- //读写数据,复制文件
- String line;
- while ((line = br.readLine()) != null) {
- pw.println(line);
- }
-
- //释放资源
- pw.close();
- br.close();
-
- }
- }
Properties作为Map集合的使用
- package IO流.特殊操作流.Properties;
- /*
- Properties概述:
- 1,是一个Map体系的集合类
- 2,Properties可以保存到流中或从流中加载
- */
-
- import java.util.Properties;
- import java.util.Set;
-
- public class Properties作为Map集合的使用 {
- public static void main(String[] args) {
- //创建集合对象--Properties不是泛型
- // Properties<String, String> properties = new Properties<String, String>();
- Properties prop = new Properties();
- //存储元素
- prop.put("001", "z");
- prop.put("002", "x");
- prop.put("003", "c");
- //遍历结合
- Set<Object> keySet = prop.keySet();
- for (Object key : keySet) {
- Object value = prop.get(key);
- System.out.println(key + "," + value);
- }
-
- }
- }
Properties特有方法
- package IO流.特殊操作流.Properties;
- /*
- Object setProperty(String key, String value)
- 调用 Hashtable方法 put 。
- String getProperty(String key)
- 使用此属性列表中指定的键搜索属性。
- Set<String> stringPropertyNames()
- 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
- */
-
- import java.util.Properties;
- import java.util.Set;
-
- public class Properties特有方法 {
- public static void main(String[] args) {
-
- Properties prop=new Properties();
-
- prop.setProperty("001", "z");
- prop.setProperty("002", "x");
- prop.setProperty("003", "c");
-
- System.out.println(prop.getProperty("001"));
-
- Set<String> names = prop.stringPropertyNames();
- for (String key : names) {
- String value = prop.getProperty(key);
- System.out.println(key + "=" + value);
- }
-
- }
-
- }
Properties和IO流结合的方法
- package IO流.特殊操作流.Properties;
- /*
- void load(InputStream inStream) 从输入字节流读取键值对
- void store(OutputStream out,String comments)
- 将键值对写入Properties,以适合于使用load(InputStream)方法的格式写入输出字节流
- void load(Reader reader) 从输入字符流读取键值对
- void store(Writer writer,String comments)
- 将键值对写入Properties,以适合于使用load(InputStream)方法的格式写入输出字符流
- */
-
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Properties;
-
- public class Properties和IO流结合的方法 {
- public static void main(String[] args) throws IOException {
- //把集合中的数据保存文件中
- myStore();
-
- //把文件中的数据加载到集合
- myload();
- }
-
- private static void myload() throws IOException {
- Properties prop = new Properties();
- //void load(Reader reader)
- FileReader fr= new FileReader("idea_text\\fw.txt");
- prop.load(fr);
- fr.close();
- System.out.println(prop);
- }
-
- private static void myStore() throws IOException {
- Properties prop = new Properties();
-
- prop.setProperty("001","z");
- prop.setProperty("002","x");
- prop.setProperty("003","c");
- //void store(Writer writer,String comments)
- FileWriter fw = new FileWriter("idea_text\\fw.txt");
- prop.store(fw,null);
- fw.close();
-
- }
- }
对象序列化流
- package IO流.特殊操作流.对象序列化流;
-
- import java.io.Serializable;
-
- public class Student implements Serializable {
- private String name;
- private int age;
-
- public Student() {
- }
-
- 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;
- }
- }
- package IO流.特殊操作流.对象序列化流;
- /*
- 对象序列化流:ObjectOutputStream
- 将Java对象的原始数据类型和图形写入OutputStream,可以使用ObjectInputStream读取(重构)对象。
- 可以通过使用流的文件来实现对象的持久存储。如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象
- 构造方法:
- ObjectOutputStream(OutputStreamout):
- 创建一个写入指定的OutputStream的ObjectOutputStream
- 序列化对象的方法:
- void writeObject(Object obj):将指定的对象写入ObjectOutputStream
- 注意:
- 一个对象要想被序列化,该对象所属的类必须必须实现Serializable接口
- Serializable是一个标记接口,实现该接口,不需要重写任何方法
- */
-
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
-
- public class 对象序列化流 {
- public static void main(String[] args) throws IOException {
- //创建一个写入指定的OutputStream的ObjectOutputStream
- ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("idea_text\\oos.txt"));
- //创建对象
- Student s=new Student("zxz",30);
- //void writeObject(Object obj):将指定的对象写入ObjectOutputStream
- oos.writeObject(s);
- //释放资源
- oos.close();
-
-
- }
-
-
-
- }
-
对象反序列化流
- package IO流.特殊操作流.对象序列化流;
- /*
- 对象反序列化流:ObjectInputStream
- ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象
- 构造方法:
- ObjectInputStream(InputStream in):创建从指定的IutStream读取的ObjectInputStream
- 反序列化对象的方法:
- Object readObject0:从ObjectInputStream读取一个对象
- */
-
- import java.io.*;
-
- public class 对象反序列化流 {
- public static void main(String[] args) throws IOException, ClassNotFoundException {
- //ObjectInputStream(InputStream in):创建从指定的IutStream读取的ObjectInputStream
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("idea_text\\oos.txt"));
- //Object readObject0:从ObjectInputStream读取一个对象
- Object obj=ois.readObject();
-
- Student s=(Student) obj;
- System.out.println(s.getName()+","+s.getAge());
-
- ois.close();
-
-
- }
- }
对象序列化流的问题
- package IO流.特殊操作流.对象序列化流.对象序列化流的问题;
-
- import java.io.Serializable;
-
- public class Student implements Serializable {
- private static final long serialVersionUID =42L;
- private String name;
- // private int age;
- private transient int age; //age=null;
- public Student() {
- }
-
- 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;
- }
-
- @Override
- public String toString() {//所改变的对象所属的类文件
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
-
- package IO流.特殊操作流.对象序列化流.对象序列化流的问题;
- /*
- 1,用对象序列化了一个对象后,假如我们修改了对象所属的类文件,读取数据会出现问题吗?
- 会出现问题,抛出 InvalidClassException 异常
- 2,出问题了,如何解决?
- 给对象所属的类加一个SerialVersionUID
- private static final long serialVersionUID =42L;
- 3,如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?
- 给该成员变量加transient关键字修饰,该关键字标记的成员变量不再参与序列化过程
- */
-
- import java.io.*;
-
- public class Demo {
- public static void main(String[] args) throws IOException, ClassNotFoundException {
- // write();
- read();
- }
- //反序列化
- private static void read() throws IOException, ClassNotFoundException {
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("idea_text\\oos.txt"));
- Object obj = ois.readObject();
- Student s=(Student) obj;
- System.out.println(s.getName()+","+s.getAge());
- ois.close();
-
- }
- //序列化
- private static void write() throws IOException {
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("idea_text\\oos.txt"));
- Student s= new Student("程序员",20);
- oos.writeObject(s);
- oos.close();
-
-
- }
-
- }