21BCM075 Csi0403 Practical 4
21BCM075 Csi0403 Practical 4
setVisible(true);
add(b1);
add(b2);
add(b3);
add(b4);
setSize(400, 400);
setLayout(null);
setBackground(Color.pink);
b1.setActionCommand("b1");
b2.setActionCommand("b2");
b3.setActionCommand("b3");
b4.setActionCommand("b4");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "b1") {
Frame f = new Frame();
Button B5 = new Button("LOAD");
B5.setActionCommand("B5");
f.add(B5);
B5.setBounds(250, 90, 80, 30);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
f.setBackground(Color.red);
Label l = new Label("ENTER FILE NAME ");
Label m1 = new Label("CONTENT OF THE FILE");
f.add(m1);
m1.setBounds(100, 170, 150, 30);
f.add(l);
l.setBounds(100, 60, 150, 30);
f.setTitle("READ A TEXT FILE");
TextField t = new TextField();
t.setBounds(100, 90, 150, 30);
f.add(t);
TextArea ta = new TextArea();
ta.setBounds(100, 200, 300, 280);
f.add(ta);
B5.addActionListener(new ActionListener() {
String s = "";
int a;
@Override
public void actionPerformed(ActionEvent e) {
try {
InputStream i = new FileInputStream(t.getText());
while ((a = i.read()) != -1) {
s = s + (char) a;
}
} catch (Exception m) {
ta.setText("FILE NOT FOUND!");
}
ta.setText(s);
}
});
}
if (e.getActionCommand() == "b2") {
Frame f2 = new Frame();
Button B6 = new Button("SAVE");
B6.setActionCommand("B6");
f2.add(B6);
f2.setSize(500, 500);
f2.setLayout(null);
f2.setVisible(true);
f2.setBackground(Color.cyan);
B6.setBounds(320, 100, 80, 30);
TextArea t1 = new TextArea();
t1.setBounds(100, 200, 300, 250);
f2.add(t1);
f2.setTitle("SAVE CONTENT TO A TEXT FILE");
Label ta1 = new Label();
Label za1 = new Label();
za1.setText("ENTER FILE NAME");
f2.add(za1);
ta1.setBounds(100, 170, 250, 30);
za1.setBounds(100, 70, 130, 30);
f2.add(ta1);
ta1.setText("ENTER TEXT TO BE SAVED IN THE FILE");
TextField z1 = new TextField();
f2.add(z1);
z1.setBounds(100, 100, 150, 30);
B6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
char[] str = new char[100];
String s = t1.getText();
int l = s.length();
str = s.toCharArray();
String file = z1.getText();
try {
OutputStream o = new FileOutputStream(file);
for (int i = 0; i < l; i++) {
o.write(str[i]);
}
} catch (Exception E) {
}
}
});
}
if (e.getActionCommand() == "b3") {
Frame f3 = new Frame();
Button B7 = new Button("COPY");
B7.setActionCommand("B6");
f3.add(B7);
f3.setSize(500, 500);
f3.setLayout(null);
f3.setVisible(true);
f3.setTitle("COPY A FILE");
f3.setBackground(Color.orange);
B7.setBounds(320, 350, 80, 30);
TextField t2 = new TextField();
t2.setBounds(100, 130, 190, 30);
f3.add(t2);
Label ta2 = new Label();
Label xa2 = new Label();
f3.add(xa2);
xa2.setBounds(100, 220, 250, 30);
ta2.setBounds(100, 100, 250, 30);
f3.add(ta2);
ta2.setText("ENTER SOURCE NAME OF FILE");
TextField x1 = new TextField();
xa2.setText("ENTER DESTINATION NAME OF FILE");
f3.add(x1);
x1.setBounds(100, 250, 220, 30);
B7.addActionListener(new ActionListener() {
String s = "";
int a;
@Override
public void actionPerformed(ActionEvent e) {
try {
InputStream i = new
FileInputStream(t2.getText());
OutputStream o = new
FileOutputStream(x1.getText());
while ((a = i.read()) != -1) {
o.write(a);
}
} catch (Exception E) {
System.out.println(E);
}
}
});
}
if (e.getActionCommand() == "b4") {
Frame f4 = new Frame();
Button B8 = new Button("DELETE");
B8.setActionCommand("B6");
f4.add(B8);
f4.setSize(500, 500);
f4.setLayout(null);
f4.setVisible(true);
f4.setTitle("DELETE A FILE");
f4.setBackground(Color.magenta);
B8.setBounds(300, 200, 80, 30);
TextField t3 = new TextField();
t3.setBounds(100, 130, 220, 30);
f4.add(t3);
Label ta3 = new Label();
ta3.setBounds(100, 100, 250, 30);
f4.add(ta3);
ta3.setText("ENTER FILE YOU WANT TO DELETE");
B8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File f = new File(t3.getText());
f.delete();
}
});
}
}
OUTPUT :