Java_log2000_IO2

IO篇entry2

关键词: 文件操作; 流的装配与串行化;

文件操作

File类

例 打开为文件

程序说明

本例每次从源程序文件OpenFile.java中读取512B,存储在缓冲区buffer中,再根据buffer中实际读到的字节数量将它们构造成字符串显示在屏幕上。

import java.io.*;
public class OpenFile 
{
    public static void main(String args[]) throws IOException
    {
        try
        {                                          //创建文件输入流对象
            FileInputStream  rf = new FileInputStream("OpenFile.java");
            int n=512,c=0;
            byte buffer[] = new byte[n];
            while ((c=rf.read(buffer,0,n))!=-1 )   //读取输入流
            {
                System.out.print(new String(buffer,0,c));               
            }            
            rf.close();                            //关闭输入流
        }
        catch (IOException ioe)
        { System.out.println(ioe);}
        catch (Exception e)
        { System.out.println(e);}
    }
}

例 写入文件

程序说明

本例用System.in.read(buffer)从键盘输入一串字符,存储在缓冲区buffer中,再以FileOutStream的write(buffer)方法,将buffer中内容写入文件Write1.txt
中;写入文件时,有两种方式,一种是覆盖,一种是追加,在FileOutputStream的构造方法中指出。默认为false,即从头开始写。

import java.io.*;
public class Write1 
{
    public static void main(String args[])
    {
        try
        {   System.out.print("Input: ");
            int count,n=512;
            byte buffer[] = new byte[n];
            count = System.in.read(buffer);        //读取标准输入流
            FileOutputStream  wf = new FileOutputStream("Write1.txt");
                                                   //创建文件输出流对象
            wf.write(buffer,0,count);              //写入输出流
            wf.close();                            //关闭输出流
            System.out.println("Save to Write1.txt!");
        }
        catch (IOException ioe)
        { System.out.println(ioe);}
        catch (Exception e)
        { System.out.println(e);}
    }
}

流的装配与串行化

import java.io.*;
 public class PrintScreen {
     public static void main(String[] args) throws Exception {
      PrintWriter out = new PrintWriter(new 
OutputStreamWriter(System.out), true);
        out.println("Hello");
     }
 }
对象串行化与持续性

对象串行化的含义 :对象的串行化(Serialization)是指通过写出描述自己状态的数值来记录自己的过程。

对象持续性的含义:指能够通过对象的串行化以便将来再生的能力。
程序说明

本例声明Student为序列化的类,该类Save方法创建对象输出流out,并以添加方式向文件直接写入当前对象。display方法中,创建对象输入流in,从文件中直接读取一个对象,获得该对象的类名、接口名等属性,并显示其中的域变量值。

u1.getClass().getName()是得到该对象类的类名;

u1.getClass().getInterfaces()[0])是得到该对象类的第一个实现接口

student

import java.io.*;
public class Student implements Serializable    //序列化
{
    int number=1;
    String name;
    Student(int number,String n1)
    {   this.number = number;
        this.name = n1;
    }
    Student()
    { this(0,""); }
    void save(String fname)
    {
        try
        {
            FileOutputStream fout = new FileOutputStream(fname);
            ObjectOutputStream out = new ObjectOutputStream(fout);
            out.writeObject(this);               //写入对象
            out.close();
        }
        catch (FileNotFoundException fe){}
        catch (IOException ioe){}
    }
    void display(String fname)
    {
        try
        {
            FileInputStream fin = new FileInputStream(fname);
            ObjectInputStream in = new ObjectInputStream(fin);
            Student u1 = (Student)in.readObject();  //读取对象
            System.out.println(u1.getClass().getName()+"  "+
                                 u1.getClass().getInterfaces()[0]);
            System.out.println("  "+u1.number+"  "+u1.name);
            in.close();
        }
        catch (FileNotFoundException fe){}
        catch (IOException ioe){}
        catch (ClassNotFoundException ioe) {}
    }
    public static void main(String arg[])
    {
        String fname = "Student.obj"; //文件名
        Student s1 = new Student(1,"Wang");
        s1.save(fname);
        s1.display(fname);
    }
}

student2

import java.io.*;
public class Student2 implements Serializable    //序列化
{
    int number=1;
    String name;
    Student2(int number,String n1)
    {   this.number = number;
        this.name = n1;
    }
    Student2()
    { this(0,""); }
    void save(String fname)
    {
        try
        {
            FileOutputStream fout = new FileOutputStream(fname);
            ObjectOutputStream out = new ObjectOutputStream(fout);
            out.writeObject(this);               //写入对象
            out.close();
        }
        catch (FileNotFoundException fe){}
        catch (IOException ioe){}
    }
    void display(String fname)
    {
        try
        {
            FileInputStream fin = new FileInputStream(fname);
            ObjectInputStream in = new ObjectInputStream(fin);
            Student2 u1 = (Student2)in.readObject();  //读取对象
            System.out.println(u1.getClass().getName()+"  "+
                                 u1.getClass().getInterfaces()[0]);
            System.out.println("  "+u1.number+"  "+u1.name);
            in.close();
        }
        catch (FileNotFoundException fe){}
        catch (IOException ioe){}
        catch (ClassNotFoundException ioe) {}
    }
    public static void main(String arg[])
    {
        String fname = "student2.obj"; //文件名
        Student2 s1 = new Student2(1,"Wang");
        s1.save(fname);
        s1.display(fname);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值