数据过滤流
import java.io.*;
import java.util.*;
class datainput_output{
public static void main(String args[])throws IOException{
FileOutputStream fos=new FileOutputStream("aa.txt");
DataOutputStream dos=new DataOutputStream(fos);
try{
dos.writeBoolean(true);
dos.writeByte((byte)123);
dos.writeChar('j');
dos.writeBoolean(true);
}
finally{
dos.close();
}
FileInputStream fin=new FileInputStream("aa.txt");
DataInputStream sin=new DataInputStream(fin);
try{
System.out.println(sin.readBoolean());
System.out.println(sin.readByte());
System.out.println(sin.readChar());
System.out.println(sin.readBoolean());
}
finally{
sin.close();
}
}
}
//当前目录下创建aa.txt,绝对目录记得把\改成/,不然就老提示illegal escape character
对象流
import java.io.Serializable;
public class Student implements Serializable
{
int id;
String name;
int age;
String department;
public Student(int id,String name,int age,String department){
this.id=id;
this.name=name;
this.age=age;
this.department=department;
}
void show(){
System.out.println(id+","+name+","+age+","+department);
}
}//可以串行化的对象
import java.io.*;
class ObjectSeriWrite{
public static void main(String args[]){
Student stu1=new Student(200501,"li ming",16,"compute science");
Student stu2=new Student(200502,"zhang bi",17,"chineses");
try{
FileOutputStream fo=new FileOutputStream("student.dat");
ObjectOutputStream so=new ObjectOutputStream(fo);
so.writeObject(stu1);
so.writeObject(stu2);
so.close();
FileInputStream fi=new FileInputStream("student.dat");
ObjectInputStream si=new ObjectInputStream(fi);
stu1=(Student)si.readObject();
stu2=(Student)si.readObject();
si.close();
stu1.show();
stu2.show();
}
catch(Exception e){
System.out.println(e);
}
}
}
/*他创建了个bat文件,用记事本打开全是
sr Student?Rt鮱 I ageI idL
departmentt Ljava/lang/String;L nameq ~ xp 5t compute sciencet li mingsq ~ 6t chinesest zhang bi
但是要记得静态的东西保留不了*/
评论