📄 通过建立dataoutputstream对象和datainputstream对象,分别将三名学生的基本信息写入到文件当中和从相应的文件当中读取出来.txt
字号:
import java.io.*;
public class DataStreamDemo
{
public static void main(String[] args) throws IOException {
String [] name = {"Liu","Wang","Zhao" };
int [] age = {20,19,21};
char [] sex = {'F','M','M'};
DataOutputStream dos = new DataOutputStream(new FileOutputStream("StudInfo.dat"));
DataInputStream dis = new DataInputStream(new FileInputStream("StudInfo.dat"));
try{
for(int i = 0; i < name.length;i++){ //写入学生信息
dos.writeBytes(name[i]+"\n");
dos.writeInt(age[i]);
dos.writeChar(sex[i]);
}
dos.close();
String sname;
int sage;
char ssex;
char ch;
while(true){//读取学生信息
sname = dis.readLine();
sage = dis.readInt();
ssex = dis.readChar();
System.out.println(sname+" "+sage+" "+ssex);
}
}catch (EOFException e) {
dis.close();
}
}//end main
}//end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -