Input / Output

File    (File/Directory)
InputStream
OutputStream
FileReader
BufferedReader
InputStreamReader

Applet : Applet display TextField and TextArea. When user type a name of Directory (entire path is expected) in TextField, the contents of that Directory is displayed in the TextArea. If File name is typed then display “Only Directory Allowed”.
We will have to include java.io.*.

 
 import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FrameFileList implements ActionListener
{
    Frame f;
    TextField tf1;
    TextArea ta1;
   
    FrameFileList()
    {
        f=new Frame("Frame 1");
        tf1 = new TextField(10);
        ta1 = new TextArea(20,20);
      
        //Change in Layout Manager if necessary
        f.setLayout(new FlowLayout());
        f.add(tf1);
        f.add(ta1);

        tf1.addActionListener(this);

        f.setSize(400,400);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        File f1 = new File(tf1.getText());
        if(f1.isDirectory())
        {
            String s[] = f1.list();
            for(int i=0; i < s.length; i++)
            {
                ta1.append(s[i]+"\n");
            }
            tf1.setText(" ");
        }
        else
        if(f1.isFile())
        {
            ta1.setText("Only Directory Allowed");
            tf1.setText(" ");
        }
    }
    public static void main(String [] args)
    {
        FrameFileList x = new FrameFileList();
    }
}

Application : Applet display TextField and TextArea. When user type a name of File (entire path is expected) in TextField, the contents of that File is displayed in the TextArea. If Directory name is typed then display “Only File Allowed”.
We will have to include java.io.*.

 import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FrameFileContent implements ActionListener
{
    Frame f;
    TextField tf1;
    TextArea ta1;
   
    FrameFileContent()
    {
        f=new Frame("Frame 1");
        tf1 = new TextField(10);
        ta1 = new TextArea(20,20);
       
        //Change in Layout Manager if necessary
        f.setLayout(new FlowLayout());
        f.add(tf1);
        f.add(ta1);

        tf1.addActionListener(this);

        f.setSize(400,400);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent ae)
    {
        File f1 = new File(tf1.getText());
        if(f1.isFile())
        {
            try
            {
                FileInputStream fi = new FileInputStream(tf1.getText());
                int i;
                while((i = fi.read()) != -1)
                {
                    ta1.append(""+(char)i);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    public static void main(String [] args)
    {
        FrameFileContent x = new FrameFileContent();
    }
}

Application : Take source file name in first TextField and destination file name in second TextField. On clicking “COPY” Button another Button “SHOW” and TextArea appear on the Frame and at the same time source file contents are copied in the destination file. On clicking “SHOW” Button, display the file content of second TextField in the TextArea.


import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FrameFileContent2 implements ActionListener
{
    Frame f;
    TextField tf1,tf2;
    TextArea ta1;
    Button b1, b2;
   
    FrameFileContent2()
    {
        f=new Frame("Frame 1");
        tf1 = new TextField(10);
        tf2 = new TextField(10);
        b1 = new Button("COPY");
        b2 = new Button("SHOW");
        ta1 = new TextArea(20,20);

        //Change in Layout Manager if necessary
        f.setLayout(new FlowLayout());
        f.add(tf1);
        f.add(tf2);
        f.add(b1);

        b1.addActionListener(this);

        f.setSize(400,400);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == b1)
        {
            File f1 = new File(tf1.getText());
            File f2 = new File(tf2.getText());
            f.add(b2);
            f.add(ta1);
            b2.addActionListener(this);
            if(f1.isFile() && f2.isFile())
            {
                try
                {
                    FileInputStream fi = new FileInputStream(tf1.getText());
                FileOutputStream fo = new FileOutputStream(tf2.getText(),true);
                    int i;
                    while((i = fi.read()) != -1)
                    {
                        fo.write(i);
                    }
                }
                catch(Exception e)
                {   
                    e.printStackTrace();
                }
            }
        }
        if(ae.getSource() == b2)
        {
            try
            {
                FileInputStream fii = new FileInputStream(tf2.getText());
                int j;
                while((j = fii.read()) != -1)
                {
                    ta1.append(""+(char)j);
                }
                tf1.setText(" ");
                tf2.setText(" ");
                tf1.requestFocus();
            }
            catch(Exception e)
            {   
                e.printStackTrace();
            }
        }
        f.repaint();
        f.validate();
    }
    public static void main(String [] args)
    {
        FrameFileContent2 x = new FrameFileContent2();
    }
}

Application :     Take two numbers from user and display sum of them. This is repeated as long as user wants to continue.


import java.io.*;
class InputSum
{
    public static void main(String args[])
    {
        try
        {
            InputStreamReader i = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(i);
            while(true)
            {
                System.out.println("Enter First Number  : ");
                String s = br.readLine();
                System.out.println("Enter Second Number : ");
                String s1 = br.readLine();
                int x = Integer.parseInt(s);
                int y = Integer.parseInt(s1);
                int z = x+y;
                System.out.println("Total is : "+z);
                System.out.println("Do you wish to continue?[Yy/Nn] : ");
                String s2 = br.readLine();
                if(s2.equalsIgnoreCase("Y"))
                {
                    continue;
                }
                else
                {
                    System.out.println("Thank You");
                    break;
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}