为什么我把流对象放在倒数第二行关闭这个程序就无法运行了,另外我还想请教各位高手即使放对了地方,但是运行之后为什么不能退出这个程序?拜托了/**
编写一个图形界面,使用户可以输入
学生姓名,性别,年龄,并把这些信
息写入到当前目录下的myBook.dat文件下
*/import java.io.*;
import javax.swing.*;public class StudentInfo
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos = new FileOutputStream("myBook.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
JFrame frame = new JFrame();
frame.setVisible(true);
String name = JOptionPane.showInputDialog(frame, "Your name?");
dos.writeUTF(name);
String sex = JOptionPane.showInputDialog(frame, "Your sex?");
dos.writeUTF(sex);
int age = Integer.parseInt(JOptionPane.showInputDialog(frame, "Your age?"));
dos.writeInt(age);
dos.close(); // !!!!!!!!!!
FileInputStream fis = new FileInputStream("myBook.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println("name: " + dis.readUTF());
System.out.println("sex: " + dis.readUTF());
System.out.println("age: " + dis.readInt());
// !!!!!
dis.close();
}
}
编写一个图形界面,使用户可以输入
学生姓名,性别,年龄,并把这些信
息写入到当前目录下的myBook.dat文件下
*/import java.io.*;
import javax.swing.*;public class StudentInfo
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos = new FileOutputStream("myBook.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
JFrame frame = new JFrame();
frame.setVisible(true);
String name = JOptionPane.showInputDialog(frame, "Your name?");
dos.writeUTF(name);
String sex = JOptionPane.showInputDialog(frame, "Your sex?");
dos.writeUTF(sex);
int age = Integer.parseInt(JOptionPane.showInputDialog(frame, "Your age?"));
dos.writeInt(age);
dos.close(); // !!!!!!!!!!
FileInputStream fis = new FileInputStream("myBook.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println("name: " + dis.readUTF());
System.out.println("sex: " + dis.readUTF());
System.out.println("age: " + dis.readInt());
// !!!!!
dis.close();
}
}
FileInputStream fis = new FileInputStream("myBook.dat");
myBook.dat有个流还没有释放,所以出错。
2.退出程序的话
结尾加上System.exit(0);