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

来源:互联网 发布:js里getjson 编辑:程序博客网 时间:2024/06/14 20:31

02-13

ObjectOutputStream

ObjectInputStream

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

例子:定义学生类

import java.io.Serializable;

 

 

publicclass Student2implements Serializable {

 

   intid;

   String name;

   intage;

   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;

 

 

publicclass Serialization {

 

   

   publicstaticvoid 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.*;

publicclassMyTest {

 

 publicstaticvoid 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.*;

publicclass TestInOut implements Runnable {

 Processp =null;

 public TestInOut()

 {

  try{

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

  new Thread(this).start();

  }catch(Exception e){}

    

 }

 publicstaticvoid 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)

    {

       }

 }

 

 

 }

 publicvoid 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;

 

 

publicclass TestPrintWriter {

 

   

   publicstaticvoid main(String[] args) {

 

      try{

          thrownew 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());

 

      }

      

   }

 

}

 

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 酷派手机玩游戏竖屏怎么办 身上起红疙瘩很痒怎么办越挠越多 苹果6s指纹解锁坏了怎么办 案子结了网上追逃的怎么办 贷款买的手机不还了会怎么办 支付宝手机号没用了登陆不了怎么办 支付宝绑定的手机号注销了怎么办 考勤机进水了不能识别指纹怎么办? 网商银行人脸识别失败怎么办 电脑网页上的字变小了怎么办 把光驱换成固态硬盘后不识别怎么办 相机内存卡电脑读不出来怎么办 sd卡在电脑上无法格式化怎么办 内存卡突然读不出来了怎么办 怀孕两个月胎儿死在腹中怎么办 香港公司在大陆卖地皮资金怎么办 结婚证上的身份证号码错了怎么办 身份证快过期了人在外地怎么办 邮政电话银行登录密码忘记了怎么办 如果欠了3w不敢和家里说怎么办 大四学生欠了3w该怎么办 房子付了首付贷款贷不下来怎么办 浙江嵊泗人在金华丢了身份证怎么办 一证5号够了怎么办新卡 微信号被盗实名认证是自己的怎么办 苹果微信登录显示被盗风险怎么办 在诈骗公司上班被公安抓了怎么办 在国外护照不小心撕坏了怎么办 在俄罗斯护照超期拉黑应该怎么办 俄罗斯五年定居护照丢了怎么办 百家号文章质量分一直在下降怎么办 如果在韩国把护照弄丢了怎么办 坐亲戚的车出了车祸受伤怎么办 出了车祸受伤对方不拿医药费怎么办 如果找你买保险的不在了保单怎么办 赴美生子父母一方是绿卡怎么办 农保报销需要居住证过期了怎么办 有上海户口但没有户口本怎么办护照 签证用的旧护照丢失了英签怎么办 买的动迁房房东不肯过户怎么办 身份信息在qq邮箱泄露了怎么办