Java Blog

 
 
 
import java.io.*;
import java.awt.*;

class  StudentFile extends Frame
{
       //Defining window components
       TextField number, name, marks;
       Button enter,done;
       Label  lblnum, lblname, lblmarks;

       DataOutputStream dos;

       //Initialize the Frame
       public StudentFile(){
             super(\"Create Student File\");
        }

         // Setup the window
       public void setup()
       {
            resize(400,200);
            setLayout(new GridLayout(4,2));
            //Create the componenets of the Frame
            number = new TextField(25);
            name = new TextField(25);
            marks = new TextField(25);
            lblnum = new Label(\"Roll Number\");
            lblname = new Label(\"Student Name\");
            lblmarks = new Label(\"Marks\");
            enter = new Button(\"Enter\");
            done = new Button(\"Done\");

            //Add the components to the Frame
            add(lblnum);
            add(number);
            add(lblname);
            add(name);
            add(lblmarks);
            add(marks);
            add(enter);
            add(done);

            //show the Frame using show method of Frame
            show();

            //open the file
            try{
               dos = new DataOutputStream( 
                                 new FileOutputStream(\"Student.txt\"));
                } 
            catch(Exception e) {
                          System.err.println(e.toString());
                          System.exit(1);}

        }

        //Write to the file
        public void addRecord(){
            int num;
            Double d;
            num = (new Integer(number.getText())).intValue();
            try{
                dos.writeInt(num);
                dos.writeUTF(name.getText());
                 d = new Double(marks.getText());
                 dos.writeDouble(d.doubleValue());
             } catch (Exception e) {}

         //Clear the text fields
         number.setText(\" \");
         name.setText(\" \");
         marks.setText(\" \");
        }

        //Adding the record and clearing the TextFields
        public void cleanup(){
              if (!number.getText().equals(\" \"))
              {
                   addRecord();
              }
              try{
                  dos.flush();
                  dos.close();
                }catch(Exception e) {}
          }

        //Processing the event
        public boolean action(Event event, Object o){
              if (event.target instanceof Button)
              {
                  if (event.arg.equals(\"Enter\"))
                  {
                      addRecord();
                      return true;
                  }
              }
              return super.handleEvent(event);
        }

       public boolean handleEvent(Event event){
            if (event.target instanceof Button)
            {
                if (event.arg.equals(\"Done\"))
                {
                     cleanup();
                     System.exit(0);
                     return true;
                }
            }
            return super.handleEvent(event);
       }

     //Execute the Program
    public static void main(String[] args) 
    {
          StudentFile student = new StudentFile();
          student.setup();
    }
}

 
 
Didn't find what you were looking for? Find more on Program of creating a file using text fields in windows