⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datastream.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
import java.io.*;
public class Datastream
{
    public static void main(String arg[])
    {
        String fname = "student1.dat";
        new Student1("Wang").save(fname);
        new Student1("Li").save(fname);
        Student1.display(fname);
    }
}    
class Student1 
{
    static int count=0;
    int number=1;
    String name;
    Student1(String n1)
    {
        this.count++;                        //编号自动加1
        this.number = this.count;
        this.name = n1;
    }
    Student1()
    {
        this("");
    }
    void save(String fname)
    {
        try
        {                                    //添加方式创建文件输出流
            FileOutputStream fout = new FileOutputStream(fname,true);
            DataOutputStream dout = new DataOutputStream(fout);
            dout.writeInt(this.number);
            dout.writeChars(this.name+"\n");
            dout.close();
        }
        catch (IOException ioe){}
    }
    static void display(String fname)
    {
        try
        {
            FileInputStream fin = new FileInputStream(fname);
            DataInputStream din = new DataInputStream(fin);
            int i = din.readInt();
            while (i!=-1)                    //输入流未结束时
            {
                System.out.print(i+"  ");
                char ch ;
                while ((ch=din.readChar())!='\n') //字符串未结束时
                    System.out.print(ch);
                System.out.println();
                i = din.readInt();
            }
            din.close();
        }
        catch (IOException ioe){}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -