---------------------- android培训、java培训、期待与您交流! ----------------------
包装类是很常用的,这部分是重点要记住的,张老师将的很详细,下面是一些知识点和实例:
14.DataOutputStream类提供了往各种输出流对象中写入各种类型的数据(包括浮点小数)。
15.缓冲流为I/O流增加了内存缓冲区,增加缓冲区有两个基本目的:
允许java程序一次不只操作一个字节,这样提高了程序的性能。
由于有了缓冲区,使得在流上执行skip、mark和reset方法都成为可能。
16.BufferedInputStream和BufferedOutputStream是java提供的两个缓冲区包装类。
17.包装类举例:分别使用DataOutputStream类的writeUTF、writeBytes和writeChars方法,比较这几个方法的差异。
关闭流栈中的最上层的流对象,将会自动关闭流栈中的所有底层流对象。
public class DataStreamTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileOutputStream fos = new FileOutputStream("xyt.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF("ab中国");
dos.writeBytes("ab中国");
dos.writeChars("ab中国");
dos.close();
FileInputStream fis = new FileInputStream("xyt.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readUTF());
byte[] buf = new byte[1024];
int len = dis.read(buf);
System.out.println(new String(buf,0,len));
fis.close();
}
}
18.PrintStream类提供了一系列的print和println方法,可以将基本数据类型的数据格式化成字符串输出。
19.ObjectInputStream和ObjectOutputStream这两个包装类,用于从底层输入流中读取对象类型的数据和将对象类型的数据写入到底层输出流。他们多读写的对象必须实现了Serializable接口,对象中的transient(表示是临时的,不被对象使用)和static类型的成员变量不会被读取和写入。
20.一个可以被序列化的MyClass的定义
Public class MyClass implements Serializable{
public transient Thread t;
private String customerID;
private int total;
}
21.变成举例:创建一个可序列化的学生对象,并用ObjectOutputStream类把它存储到一个文件(mytext.txt)中,然后再用ObjectInputStream类把存储的数据读取到一个学生对象中,即恢复保存的学生对象。
import java.io.*;
public class Serialization {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
Student stu1 = new Student(19,"zhangsan",25,"huaxue");
Student stu2 = new Student(20,"lisi",23,"wuli");
FileOutputStream fos = new FileOutputStream("student.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(stu1);
os.writeObject(stu2);
os.close();
FileInputStream fis = new FileInputStream("student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
stu1 = (Student)ois.readObject();
stu2 = (Student)ois.readObject();
ois.close();
System.out.println(stu1.id);
System.out.println(stu1.name);
System.out.println(stu1.age);
System.out.println(stu1.department);
System.out.println(stu2.id);
System.out.println(stu2.name);
System.out.println(stu2.age);
System.out.println(stu2.department);
}
}
22.InputStreamReader和OutputStreamWriter,是用于将字节流转换成字符流来读写的两个类,InputStreamReader可以将一个字节流中的字节解码成字符后读取,OutputStreamWriter将字符编码成字节后写入到一个字节流中。
23.避免频繁地在字符与字节间进行转换,最好不要直接使用InputStreamReader和OutputStreamWriter;来读写数据,应尽量使用BufferedWriter包装OutputStreamWriter,用BufferedReader包装InputStreamReader
24.编程举例:在TestInOut类中启动java.exe命令执行另外一个MyTest类,TestInOut和MyTest通过进程间的管道相互传递数据。
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) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestInOut tio = new TestInOut();
tio.send();
}
public void send(){
OutputStream ops = p.getOutputStream();
//int count = 0;
while(true){
try {
//System.out.println(count++);
ops.write("help\r\n".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void run(){
InputStream in = p.getInputStream();
BufferedReader bfr = new BufferedReader(new InputStreamReader(in));
try {
while(true){
String strLine = bfr.readLine();
if(strLine != null){
System.out.println(strLine);
}else{
return;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
while(true){
String str = bf.readLine();
if(str != null){
System.out.println("hi " + str);
}else{
return;
}
}
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima