对象流:ObjectInputStream 和 ObjectOutputStream 用于保存数据的值和数据类型,是一种包装流
即 实现序列化【保存数据时保存数据的值和数据类型】和反序列化【恢复数据时,恢复数据的值和数据类型】,前提是对象流所保存的对象的类实现了序列化接口Serializable【一个标记接口,没有方法】或者Externalizable【该接口有方法要实现,一般使用前者】
package com.edu.ObjStream; import com.edu.reader_.dog; import java.io.*; /** * @author mtl121 * @version 1.0 * 将基本数据类型和一个对象保存到“data.dat”文件中 */ public class objStream_ { public static void main(String[] args) throws IOException, ClassNotFoundException { float f = 1.01f; double d = 1.08d; dog jack = new dog(8, "jack"); String filename = "data.dat"; ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(filename)); os.writeObject(jack); os.writeFloat(f); os.writeDouble(d); os.writeDouble(2.04f); os.close(); //反序列化的顺序要和序列化的顺序一致,否则会报异常 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename)); //返回的是Object类型,需要向下转型 dog dog01 = (dog)ois.readObject(); float f00 = ois.readFloat(); double d00 = ois.readDouble(); float f001= ois.readFloat(); System.out.println(dog01.getClass() +"xxx"+ f001 +"xxx"+ d00); ois.close(); } }
使用对象流的注意事项:
①读写顺序保持一致
②要求实现序列化或反序列化对象,其类需要实现Serializable接口,
③序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性,如果在类中多添加一个属性,则在处理时不会认为这是一个新的类,而认为这是原本类的升级
④序列化对象时,默认将里面所有的属性都进行序列化,但除了static和transient修饰的成员
⑤序列化对象时,要求里面属性的类型也需要实现序列化接口
⑥序列化接口具有可继承性,也就是如果某类已经实现了序列化,则他的所有子类也默认实现了序列化接口
标准输入输出
标准输入System.in 编译类型InputStream 实际运行类型为BufferedInputStream 默认设备:键盘
标准输出System.out 编译类型OutputStream 实际运行类型为PrintStream 默认设备:显示屏
转换流
inputStreamReader OutputStreamWriter
package com.edu.ObjStream; import java.io.*; /** * @author mtl121 * @version 1.0 * 转换流 InputStreamReader解决乱码问题 */ public class InputStreamreader_ { public static void main(String[] args) throws IOException { String filename = "e:/note.txt"; //将FileInputStream转换成Reader InputStreamReader isr = new InputStreamReader(new FileInputStream(filename), "gbk"); //把InputStreamReader放入到BufferedReader里面 BufferedReader br = new BufferedReader(isr); String s = br.readLine(); System.out.println(s); br.close(); //OutputStreamWriter String writefile = "e:/h.txt"; OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(writefile), "utf-8"); osw.write("qifei 起飞"); osw.close(); } }
打印流:PrintStream和PrintWriter 用来包装输出流,方便输出信息
只有输出流,没有输入流,
System.out、System.err是PrintStream的实例变量
/** * @author mtl121 * @version 1.0 */ public class PrintStream_ { public static void main(String[] args) throws IOException { // 默认情况下,PrintStream输出数据位置为显示屏, //可以通过System.setOut()修改输出位置 /* print()源码 public void print(String s) { if (s == null) { s = "null"; } write(s); } * */ System.setOut(new PrintStream("e:/k.txt"));//修改打印流位置 PrintStream out = System.out; out.print("john, hell");//底层调用write(),因此也可以直接调用write输出 out.write("john, hell".getBytes()); System.out.println("lalal拉拉"); out.close(); } }
public static void main(String[] args) throws FileNotFoundException { // PrintWriter printWriter = new PrintWriter(System.out);默认打印在显示屏 //修改打印位置 PrintWriter printWriter = new PrintWriter("e:/aa.txt"); printWriter.print("hei,集"); printWriter.close(); }
//读入文件的内容并在每行加入行号,输出在显示屏上
public static void main(String[] args) throws IOException, FileNotFoundException {
String filepath = "e:/mytemp/hello.txt";
//如果将文件的编码改为gbk,则需要使用转换流指定编码格式
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath),"gbk"));
PrintWriter pw = new PrintWriter(System.out);
String content = null;
int count = 1;
while((content = br.readLine()) != null) {
pw.print((count++) + content + '\n');
}
pw.close();
br.close();
}
/** * @author mtl121 * @version 1.0 */ public class home03 { public static void main(String[] args) throws IOException, ClassNotFoundException { Properties properties = new Properties(); properties.load(new FileReader("src/com/edu/a.properties")); dog dog = new dog(properties.getProperty("name"), Integer.valueOf(properties.getProperty("age")),properties.getProperty("color")); System.out.println(dog.toString()); String filepath = "e:/d.dat"; ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filepath)); oos.writeObject(dog); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filepath)); System.out.println((dog)ois.readObject());; } } class dog implements Serializable { String name; int age; String color; public dog(String name, int age, String color) { this.name = name; this.age = age; this.color = color; } @Override public String toString() { return "dog{" + "name='" + name + '\'' + ", age=" + age + ", color='" + color + '\'' + '}'; } }