?? datastream.java
字號(hào):
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++; //編號(hào)自動(dòng)加1
this.number = this.count;
this.name = n1;
}
Student1()
{
this("");
}
void save(String fname)
{
try
{ //添加方式創(chuàng)建文件輸出流
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) //輸入流未結(jié)束時(shí)
{
System.out.print(i+" ");
char ch ;
while ((ch=din.readChar())!='\n') //字符串未結(jié)束時(shí)
System.out.print(ch);
System.out.println();
i = din.readInt();
}
din.close();
}
catch (IOException ioe){}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -