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;
}
}
利用ObjectOutputStream和ObjectInputStream对对象进行读写
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);
}
}
字节流和字符流的转换
InputStreamReader和OutputStreamWriter用于字节流和字符流转换,这两个最好不要直接使用,而是用BufferedReader和BufferedWriter进行包装后使用。
02-14
Java程序和其他进程的通信
A:在java中可以用Process类的实例对象表示子进程,子进程的输入和输出不再连接到键盘和显示器,而是以管道流的形式连接到父进程的一个输出流和输入流对象上。
B:调用Process类的getOutputStream和getInputStream方法可以获得连接到子进程的输出流和输入流对象
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());
}
}
}