File f= new File(XX.txt);
FileOutputStream fs=new FileOutputStream(f);
DataOutputStream ds=new DataOutputStream(fs);
ds.WriteChars(name);
自己学学不难

解决方案 »

  1.   

    还是不对啊!会有很多的错!不是语法的,是运行时的!
    if(source==save)
         {
         int id;
         String name;
         float score;
         id=Integer.parseInt(text1.getText());
         name=text2.getText();
         score=Float.parseFloat(text3.getText());
         //File file=new File("I:\\data.dat");
         File f= new File("data.txt");
         try
         {
    FileOutputStream fs=new FileOutputStream(f);
     try{
      DataOutputStream ds=new DataOutputStream(fs);
        ds.write(id);
               }catch (Exception e){}
                
                }catch (Exception e){}
    }
      

  2.   

    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.event.*;public class stu_manager extends JFrame implements ActionListener{
    JLabel text1=new JLabel("学号");
    JLabel text2=new JLabel("姓名");
    JLabel text3=new JLabel("成绩");
    JButton save=new JButton("保存");
    JButton load=new JButton("读取");
    JTextField id=new JTextField(10);
    JTextField name=new JTextField(10);
    JTextField score=new JTextField(10);
    JLabel text=new JLabel("平均分");
    JTextField average=new JTextField(10);
    JButton conculete=new JButton("计算平均");
    stu[] student;
    int count;//numbers of student
    stu_manager()
    {
    super.setSize(450,100);
    id.setHorizontalAlignment(JTextField.RIGHT);
    name.setHorizontalAlignment(JTextField.RIGHT);
    score.setHorizontalAlignment(JTextField.RIGHT);
    average.setHorizontalAlignment(JTextField.RIGHT);
    JPanel pane=new JPanel();
    pane.setLayout(new FlowLayout());
    pane.add(text1);
    pane.add(id);
    pane.add(text2);
    pane.add(name);
    pane.add(text3);
    pane.add(score);
    pane.add(text);
    pane.add(average);
    pane.add(save);
    pane.add(load);
    pane.add(conculete);
    save.addActionListener(this);
    load.addActionListener(this);
    conculete.addActionListener(this);
    setContentPane(pane);

        }
        public void actionPerformed(ActionEvent evt)
        {
         Object source=evt.getSource();
         if(source==save)
         {
         int ids;
         String names;
         float scores;
         ids=Integer.parseInt(id.getText());  //这里数字id和文本框id弄混了,其他也是,都给你改了
         names=name.getText();
         scores=Float.parseFloat(score.getText());
         stu people=new stu(ids,names,scores);
         //在这里添加写入文件代码,可是我不会,谢谢指教
         File f= new File("data.txt");
         try{
    FileOutputStream fs=new FileOutputStream(f);
        DataOutputStream ds=new DataOutputStream(fs);
        System.out.println(ids);
        ds.writeBytes(ids+"");
                }catch (Exception e){
                 e.printStackTrace();
                }     }
         if(source==load)
         {
         }
         if(source==conculete)
         {
         }
        }
        public static void main(String arg[])
        {
         JFrame frame=new stu_manager();
         ExitWindow exit=new ExitWindow();
    frame.addWindowListener(exit);
         frame.show();
        }
    }class stu{
    public int id;
    public String name;
    public float score;
    stu(int a,String b,float c)
    {
    id=a;
    name=b;
    score=c;
    }
    }class ExitWindow extends WindowAdapter{
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }