Monday, March 9, 2015

Java file using Applet



Read a file using Applet

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class readFileApplet extends Applet
{
   String fileToRead = "test1.txt";
   StringBuffer strBuff;
   TextArea txtArea;
   Graphics g;
   public void init()
   {
      txtArea = new TextArea(100, 100);
      txtArea.setEditable(false);
      add(txtArea, "center");
      String prHtml = this.getParameter("fileToRead");
      if (prHtml != null) fileToRead = new String(prHtml);  
      readFile();
   }
   public void readFile()
   {
      String line;
      URL url = null;
      try
      {
         url = new URL(getCodeBase(), fileToRead);
      }
      catch(MalformedURLException e){}
      try
        {
         InputStream in = url.openStream();
         BufferedReader bf = new BufferedReader
         (new InputStreamReader(in));
         strBuff = new StringBuffer();
         while((line = bf.readLine()) != null)
         {
            strBuff.append(line + "\n");
         }
         txtArea.append("File Name : " + fileToRead + "\n");
         txtArea.append(strBuff.toString());
      }
      catch(IOException e)
     {
      e.printStackTrace();
   }
 }
}

Write to a file using Applet

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.net.*;
public class WriteFile extends Applet
 {
   Button write = new Button("WriteToFile");
   Label label1 = new Label("Enter the file name:");
   TextField text = new TextField(20);
   Label label2 = new Label("Write your text:");
   TextArea area = new TextArea(10,20);
   public void init()
    {
      add(label1);
      label1.setBackground(Color.lightGray);
      add(text);
      add(label2);
      label2.setBackground(Color.lightGray);
      add(area);
      add(write,BorderLayout.center);
      write.addActionListener(new ActionListener ()
      {
        public void actionPerformed(ActionEvent e)
        {
           new WriteText();
        }
     }
   );
}
public class WriteText 
 {
   WriteText()
     {
      try 
         {
         String str = text.getText();
         if(str.equals(""))
          {
           JOptionPane.showMessageDialog(null,"Please enter the file name!");
            text.requestFocus();
         }
         else
           {
            File f = new File(str);
            if(f.exists())
            {
               BufferedWriter out = new 
               BufferedWriter(new FileWriter(f,true));
               if(area.getText().equals(""))
                {
                  JOptionPane.showMessageDialog(null,"Please enter your text!");
                  area.requestFocus();
               }
               else
               {
                  out.write(area.getText());
                  if(f.canWrite())
                  {
                  JOptionPane.showMessageDialog(null, "Text is written in "+str);
                     text.setText("");
                     area.setText("");
                     text.requestFocus();
                  }
                  else
                  {
              JOptionPane.showMessageDialog(null,"Text isn't written in "+str);
                  }
                  out.close();
                  }
               }
               else
                 {
                  JOptionPane.showMessageDialog
                  (null,"File not found!");
                  text.setText("");
                  text.requestFocus();
               }
            }
         }
         catch(Exception x)
         {
            x.printStackTrace();
         }
      }
   }
}

No comments:

Post a Comment