JAVA高级视频02_IO输入与输出日记5(02-13到15)

本文介绍了Java中对象的序列化过程,包括如何使用ObjectInputStream和ObjectOutputStream进行对象的读写操作。此外,还探讨了Java程序与其他进程进行通信的方法,涉及Process类的使用及其实例之间的输入输出流交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

02-13

ObjectOutputStream

ObjectInputStream

用于从底层流中读取对象类型的数据和将对象类型的数据写入底层输出流,这连个类必须实现Serializable接口。对象中的transient(临时)和Static(全局)类型的变量不会被读取和写入。

例子:定义学生类

import java.io.Serializable;

 

 

public class Student2 implements Serializable {

 

    int id;

    String name;

    int age;

    String department;

    public Student2(int id, String name, int age, String department) {

       super();

       this.id = id;

       this.name = name;

       this.age = age;

       this.department = department;

    }

   

}

 

利用ObjectOutputStreamObjectInputStream对对象进行读写

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

 

 

public class Serialization {

 

   

    public static void main(String[] args) throws IOException, Exception {

 

       Student2 st1=new Student2(1,"zhangsan",38,"shuxue");

       Student2 st2=new Student2(2,"lisi",22,"lishi");

       Student2 st3=new Student2(3,"wangwu",39,"english");

       Student2 st4=new Student2(4,"lideguo",32,"yuwen");

 

       FileOutputStream fos=new FileOutputStream("student2.txt");

       BufferedOutputStream bos=new BufferedOutputStream(fos);

       ObjectOutputStream dos=new ObjectOutputStream(bos);

      

       dos.writeObject(st1);

       dos.writeObject(st2);

       dos.writeObject(st3);

       dos.writeObject(st4);

      

       dos.close();

      

       FileInputStream fis=new FileInputStream("student2.txt");

       BufferedInputStream bis=new BufferedInputStream(fis);

       ObjectInputStream dis=new ObjectInputStream(bis);

      

       st1=(Student2)dis.readObject();

       st2=(Student2)dis.readObject();

       st3=(Student2)dis.readObject();

       st4=(Student2)dis.readObject();

      

       dis.close();

      

       System.out.print("stu1 id="+st1.id);

       System.out.print(" "+st1.name);

       System.out.print(" "+st1.age);

       System.out.println(" "+st1.department);

      

       System.out.print("stu2 id="+st2.id);

       System.out.print(" "+st2.name);

       System.out.print(" "+st2.age);

       System.out.println(" "+st2.department);

      

       System.out.print("stu3 id="+st3.id);

       System.out.print(" "+st3.name);

       System.out.print(" "+st3.age);

       System.out.println(" "+st3.department);

      

       System.out.print("stu4 id="+st4.id);

       System.out.print(" "+st4.name);

       System.out.print(" "+st4.age);

       System.out.print(" "+st4.department);

    }

 

}

 

 

字节流和字符流的转换

InputStreamReaderOutputStreamWriter用于字节流和字符流转换,这两个最好不要直接使用,而是用BufferedReaderBufferedWriter进行包装后使用。

 

02-14

Java程序和其他进程的通信

A:java中可以用Process类的实例对象表示子进程,子进程的输入和输出不再连接到键盘和显示器,而是以管道流的形式连接到父进程的一个输出流和输入流对象上。

B:调用Process类的getOutputStreamgetInputStream方法可以获得连接到子进程的输出流和输入流对象

C:process类的destory()方法结束子进程的运行。

 

例子:MyTest

import java.io.*;

public class MyTest {

 

 public static void main(String[] args) {

  BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));

 

  while(true)

  {

   try{     

     String str = bi.readLine();

     if(str!=null)

     {

     System.out.println("TestMy:"+str);    

     }

     else

     {

      return;

     }

   }

    catch(Exception e){

    }

  }

 }

}

 

 

运行类:

import java.io.*;

public class TestInOut  implements Runnable  {

 Process p = null;

  public TestInOut()

  {

   try{

   p =Runtime.getRuntime().exec("java MyTest");

   new Thread(this).start();

   }catch(Exception e){}

    

  }

 public static void main(String[] args)

  {

  TestInOut t = new TestInOut();

  t.send();

  }

 public  void run()

 {

  InputStream in = p.getInputStream();

  BufferedReader br = new BufferedReader(new InputStreamReader(in));

  while(true)

  {  

     try

     {

      String str = br.readLine();

      if(str!=null)

      {

      System.out.println(str);   

      }

      else

      {

       return;

      } 

     }

     catch(Exception e)

     {

        }

  }

 

 

 }

 public void send()

 {

 

  while(true)

  {

   OutputStream out = p.getOutputStream();

   try

   {

  

   out.write("help/r/n".getBytes());

   }

   catch(Exception e)

   {

   }

 }

 

 

}

}

 

 

02-15

 

 

 

 

Decorator设计模式:用一个对象包装另一个对象

这个类需要继承FilterXXXX命名的类,

例子:

import java.io.PrintWriter;

import java.io.StringWriter;

 

 

public class TestPrintWriter {

 

   

    public static void main(String[] args) {

 

       try{

           throw new Exception("test,ce shi");

       }catch(Exception e){

           StringWriter aa=new StringWriter();

           PrintWriter bb=new PrintWriter(aa);

           e.printStackTrace(bb);

           System.out.println(e.getMessage());

           System.out.println(bb.toString());

 

       }

      

    }

 

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值