Lecture 12B
Cloning
File Read by Applet
RandomIO,Append
DecimalFormat
DecimalFormat-IBM
Precision Class-KI
Format - Core Java
Output to String

Console Demo
Exercises

Lectures

1A: Introduction
1B: Java Intro
2A: BuildingBlocks
2B: Objects
3A: More Objects
3B: Exceptions
       & Threads
4A: Waves
4B: Nuclear &
       Particle
5A: AWT
5B: More AWT
      & Graphics
6A:Detectors &
      Simulation
6B: LHC/Atlas &
     RandomSims    
7A: Swing
7B: Java2D
8A: Java Apps
8B: Dialogs &
    MoreClasses
9A: Java I/O
9B: Utilities,
        Unicode
10A: More
    Threading
10B: File
    Handling
11A: Array,Print,
    Images
11B: SimplePhysics
    Simulation
12A: Tips &
    Techniques
12B: More Tips &
    Techniques
13A: Satellite
    
Simulations
13B: Intro to Java
    Networking
14A: Java Servers
14B: HTTP Server
15A: ServerClient
15B: ServerClient
   Expt.Simulation
16A: Course
          Review
16B: ExerciseTest
        Discussion

    Contacts
    Description
    Exercises
    Index
    Outline
    Q&A
    Resources
    StudentInfoForm
    Student Pages
    What's New

 

Home : Lectures : Lecture 12B : Reading a File from an Applet
Reading a File from an Applet

Often one of the first applets that new Java programmers want to write in their own work involves reading a file of some sort.

This could be simple database info, e.g. a list or table of values and descriptions, in text format.

We have learned to read image files and audio files from an applet but have not yet read a text file.

This is fairly easy to do. However, keep in mind that the SecurityManager restricts access to only those files accessible from the location of the html and java files.

Below we show the code to read a text file from an applet.

Note that we use the URL class that is convenient for accessing web resources. This class provides various methods and fields, e.g. separate fields for the domain, path and file, that can be useful.

In this case, it also provides a method to obtain file access through an InputStream.

Then we can wrap this stream with an InputStreamReader to provide proper character handling.

Finally, a BufferedReader is wrapped around the stream to provide buffering to smooth out the stream flow and also to get the readLine() method for grabbing a whole line at once and returning it in a string.

ReadFile.java
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class ReadFile extends Applet {

  StringBuffer buf;
  String FileToRead="message.txt";
  TextArea ta;

//--------------------------------------------------------
  public void init() {

    ta = new TextArea(40, 40);
    ta.setEditable(false);
    setLayout(new BorderLayout());
    add(ta, "Center");

    // Get setup parameters from applet html
    String param = getParameter("FileToRead");
    if ( param != null){
      FileToRead = new String(param);
    }
    // Now read the file.
    readFile();
  }
//--------------------------------------------------------
  public void readFile(){
    String line;
    URL url=null;

    try {
     url = new URL (getCodeBase(), FileToRead );
    }
    catch (MalformedURLException  e ) {
       System.out.println("Malformed URL ");
       stop();
    }
 
    try {
      InputStream in=url.openStream();
      BufferedReader dis =
          new BufferedReader(new InputStreamReader(in));

      buf = new StringBuffer () ;

      while ((line = dis.readLine()) != null){
        buf.append(line + "\n");
      }

      in.close();
    }
    catch (IOException e ) {}
    // Load the file into the TextArea.
    ta.append(buf.toString ());
  }
}

 

Home Lectures Resources Index Contacts Students


Physics Simulations with JavaTM
KTH, Kurskod: 5A1418
Curator: Clark S. Lindsey