正文

[程序之巢]几个IO程序2006-05-17 09:15:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/14238.html

分享到:

数据过滤流

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

但是要记得静态的东西保留不了*/

阅读(4984) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册