adsense


Showing posts with label JAVA EXAMPLES READ A FILE USING APPLET. Show all posts
Showing posts with label JAVA EXAMPLES READ A FILE USING APPLET. Show all posts

Monday, 2 November 2015

JAVA EXAMPLES READ A FILE USING APPLET PAGE 208

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES READ A FILE USING APPLET







Java Examples - Read a file using Applet



Problem Description:
How to read a file using Applet?
Solution:
Following example demonstrates how to read a file using an Applet using openStream() method of URL.
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();
   }
}
}
Result:
The above code sample will produce the following result in a java enabled web browser.
View in Browser.