/* * ReadFile.java * * Created on November 2, 2006, 1:44 PM * * This code was only lightly changed from an example in Physics Simulation with Java by Clark S. Lindsey * at http://www.particle.kth.se/~fmi/kurs/PhysicsSimulation/Lectures/12B/fileReadByApplet.html */ import java.applet.*; import java.awt.BorderLayout; import java.awt.TextArea; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.lang.Object; /** * * @author George Blank Object Oriented Prof */ public class ReadFile extends java.applet.Applet { StringBuffer buffer; String filename="data.txt"; TextArea tdisplay; /** Initialization method that will be called after the applet is loaded * into the browser. */ public void init() { tdisplay = new TextArea(60,20); tdisplay.setEditable(false); setLayout(new BorderLayout()); add(tdisplay, "Center"); readFile(); } public void readFile() { String line; URL url=null; try { // Combine URL from codebase with file name url = new URL (getCodeBase(), filename); } catch (MalformedURLException e ) { System.out.println("Malformed URL "); stop(); } try { // Open a stream to read file InputStream in = ReadFile.class.getResourceAsStream(filename); BufferedReader buinst = new BufferedReader(new InputStreamReader(in)); buffer = new StringBuffer () ; // Read until end of file while ((line = buinst.readLine()) != null) { tdisplay.append(line+"\n"); } // Close stream in.close(); } catch (IOException e ) { // Load the file into the TextArea. tdisplay.append("File Read Error"); } } }