1.文件的序列化与反序列化
package com.bjsxt.io.others; /** * 空接口只是标识 * @author Administrator * */ public class Employee implements java.io.Serializable { //不需要序列化 private transient String name;//加了这个限定词就不会被序列化 private double salary; public Employee() { } public Employee(String name, double salary) { super(); this.name = name; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
package com.bjsxt.io.others; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Arrays; /** * 不是所有的對象都可以序列化 要实现才可以java.io.NotSerializableException * 不是所有的屬性都需要序列化 加了transient限定词就不会被序列话 * @author Administrator * */ public class ObjectDemo01 { /** * @param args * @throws ClassNotFoundException */ public static void main(String[] args) throws ClassNotFoundException { try { seri("e:/xp/test/ser.txt"); read("e:/xp/test/ser.txt"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //反序列化 public static void read(String destPath) throws IOException, ClassNotFoundException{ //创建源 File src =new File(destPath); //选择流 ObjectInputStream dis =new ObjectInputStream( new BufferedInputStream( new FileInputStream(src) ) ); //操作 读取的顺序与写出一致 必须存在才能读取 //不一致,数据存在问题 Object obj =dis.readObject(); if(obj instanceof Employee){ Employee emp=(Employee)obj; System.out.println(emp.getName()); System.out.println(emp.getSalary()); } obj =dis.readObject(); int[] arr=(int[])obj; System.out.println(Arrays.toString(arr)); dis.close(); } //序列化 public static void seri(String destPath) throws IOException{ Employee emp =new Employee("bjsxt",1000000); int[] arr ={1,2,3,45}; //创建源 File dest =new File(destPath); //选择流 ObjectOutputStream ObjectOutputStream dos =new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream(dest) ) ); //操作 写出的顺序 为读取准备 dos.writeObject(emp); dos.writeObject(arr); //释放资源 dos.close(); } }
注解:上面这两段代码属于是我引用的尚学堂的源码
2.文件的关闭方法:package FileUtils; import java.io.Closeable; import java.io.IOException; /** * * 我们在这里写一个关闭文件的类 * * @author Wang * */ public class CloseFile { public static void close(Closeable ... io) { //...是可变形参(可以传入无数个形参 但是必须是相同类型的) 处理方式与数组一样 只能在形参的最后那个位置 for(Closeable temp : io) { if(temp != null) { try { temp.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static <T extends Closeable> void close1(T ... io) {//使用泛型方法来关闭文件 for(T temp : io) { if(temp != null) { try { temp.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
3.文件的打印流(System)
package System; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.PrintStream; import java.util.Scanner; /** * 三个常量 * 1、System.in 输入流 标准的输入 即是 键盘输入 * 2、System.out 输出流 标准的输出 即是 控制台输出 * System.err 这个就是打印一个错误 他和上面这个基本没有区别 就是他但因出来的是红色的 在控制台上 * * ==>重定向 (就是指定上面三个常量的对应的位置) * setIn() * setOut() * setErr() * * FileDescriptor.in * FileDescriptor.out * * @author Wang * */ public class Demo01 { public static void main(String[] args) throws FileNotFoundException { //test1(); //test2(); //从键盘输入变为文件输入 //test3(); //将本该打印到控制台的文件打印到自己指定的文件中 System.out.println("请输入:");//这个是正常的输入方法 Scanner sc = new Scanner(System.in); sc.nextLine(); System.out.println(sc.nextLine()); } public static void test1(){ System.out.println("test"); System.err.println("err"); } public static void test2() throws FileNotFoundException{ //InputStream is =System.in; //键盘输入 InputStream is = new BufferedInputStream(new FileInputStream("F:/testIO/1.txt"));//文件的输入 Scanner sc = new Scanner(is); //System.out.println("请输入:"); System.out.println(sc.nextLine()); } public static void test3() throws FileNotFoundException { //重定向 把打印到控制台的东西 打印到你指定的文件夹里 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("F:/testIO/1.txt")),true));//在PrintStream()构造器中 后面加了一个True的意思是自动刷出 就是不用手动flush(); System.out.println("a"); //打印到文件 System.out.println("test"); //回控制台 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));//ileDescriptor.out这个代表是控制台文件 System.out.println("回来了"); } }
注意:
反序列话也是创建对象的一种方法
文件的关闭的方法还有一种就是 try--with--resource