HOWTO: Reading a File on a Web Server from an Applet

Last reviewed: February 10, 1998
Article ID: Q180707
The information in this article applies to:
  • Microsoft Visual J++, versions 1.0, 1.1
  • SDK for Java, versions 2.0, 2.01

SUMMARY

The following sample code shows how to read a file on a Web Server from an applet.

MORE INFORMATION

The class below shows how to read a text file named Readme.txt from the server. NOTE: Place all of the files in the same folder on the Web server.

The following steps give an example of how an applet can read a file on a Web Server:

  1. Create the following file, naming it ReadFile.java:

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

          public class ReadFile extends Applet {
    
             public void start () {
                TextArea textArea=new TextArea();
                setLayout(new BorderLayout());
                add("Center",textArea);
                String text=null;
                try {
                   URL url = new URL(getDocumentBase(),"readme.txt");
                   DataInputStream stream = new
                   DataInputStream(url.openStream());
                   do {
                      text = stream.readLine();
                      if (text!=null) textArea.appendText(text+"\r\n");
                   } while (text!=null);
                }
                catch (IOException e) {
                   e.printStackTrace();
                }
             }
          }
    
    

  2. Compile the ReadFile.java class, creating a ReadFile.class.

  3. Create a text file with at least one line of text and name it Readme.txt.

  4. Create an HTML file named ReadFile.html with the following code:

          <html>
          <head>
          <title>readfile</title>
          </head>
          <body>
          <hr>
          <applet
    
              code=ReadFile
              name=readfile
              width=320
              height=240 >
          </applet>
          <hr>
          <a href="ReadFile.java">The source.</a>
          </body>
          </html>
    
    

  5. Put ReadFile.java, ReadFile.class, ReadFile.html, and Readme.txt in the same folder on your Web Server.

  6. Open ReadFile.html from Internet Explorer.

You should be able to see your text from Readme.txt on the applet.

Keywords          : kbcode
Technology        : kbInetDev internet
Version           : WINDOWS:1.0,1.1,2.0,2.01
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 10, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.