Using FileInputstream and FileOutputStream in GUI (JAVA sample code)

this is a sample code on how to use FileInputStream and Fileoutput Stream in java.

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;


class Registration extends JFrame implements ActionListener, ItemListener{

JFrame Register = new JFrame("Registration");
String myGender;

Label namelbl = new Label("Name :");
TextField nametxt = new TextField();
Label agelbl = new Label("Age :");
TextField agetxt = new TextField();
Label SelectGenderlbl = new Label("Gender :");
Choice SelectGender = new Choice();
TextArea myInput = new TextArea();
Button btnSave = new Button("Save");
Button btnOpen = new Button("Open");
TextArea viewfiletxt = new TextArea();

public Registration(){

Register.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Register.setSize(400,300);
Register.setResizable(false);
Register.setBackground(Color.white);
Register.setVisible(true);

ActionListener listener;
Container content = Register.getContentPane();

//name

namelbl.setBounds(10,30,50,20);
namelbl.setVisible(true);
content.add(namelbl);


nametxt.setBounds(70,30,100,20);
nametxt.setVisible(true);
content.add(nametxt);

//age

agelbl.setBounds(10,55,50,20);
agelbl.setVisible(true);
content.add(agelbl);


agetxt.setBounds(70,55,50,20);
agetxt.setVisible(true);
content.add(agetxt);

//gender

SelectGenderlbl.setBounds(10,80,50,20);
SelectGenderlbl.setVisible(true);
content.add(SelectGenderlbl);

SelectGender.addItem(" ");
SelectGender.addItem("Male ");
SelectGender.addItem("Female");
SelectGender.addItemListener(this);
SelectGender.setBounds(70,80,80,20);
content.add(SelectGender);

//textarea
btnSave.setBounds(30,105,50,20);
btnSave.setVisible(true);
btnSave.addActionListener(this);
content.add(btnSave);

btnOpen.setBounds(85,105,50,20);
btnOpen.setVisible(true);
btnOpen.addActionListener(this);
content.add(btnOpen);

//textarea
myInput.setBounds(10,150,150,100);
myInput.setVisible(true);
content.add(myInput);



viewfiletxt.setBounds(200,30,150,50);
viewfiletxt.setVisible(true);
content.add(viewfiletxt);

}//public Registration

public void itemStateChanged(ItemEvent event){


myGender = SelectGender.getSelectedItem();

}//end itemStateChanged

public void actionPerformed(ActionEvent event){
JOptionPane msgbox = new JOptionPane();
//msgbox.showMessageDialog(null,"Select your gender","Error",JOptionPane.ERROR_MESSAGE);

String Name, Age , Gender;

if (event.getActionCommand().equals("Save")){

try{

File outFile = new File ("Registration.txt");
FileOutputStream outStream = new FileOutputStream(outFile);
//ObjectOutputStream outWriter = new ObjectOutputStream (outStream );
PrintWriter outWriter = new PrintWriter(outStream );

Name = nametxt.getText();

Age = agetxt.getText();

Gender = myGender;

String Register = "My Name is: " + Name + "\nI'm " + Age + "\nIm a " + Gender;
myInput.setText(String.valueOf(Register));
//outWriter.writeObject(persons);
outWriter.println(Register);
outWriter.close();
}//try
catch(IOException e){

}
catch(NullPointerException e){

}

}//Save


if (event.getActionCommand().equals("Open")){

FileReader fr;
try {
//BufferedReader br = new BufferedReader( new FileReader(new File("Person.txt")));
fr = new FileReader (new File("Registration.txt"));
BufferedReader br = new BufferedReader (fr);
String line = br.readLine();
while (line != null) {
viewfiletxt.setText(String.valueOf(line));
line = br.readLine();
}
br.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}//end actionPerformed


public static void main(String args[]) {

Registration main = new Registration();

}//main



}//class Registration

this code is just my work.
if you have any comments or suggestion on how to make this code more easy feel free to email me.

0 comments: