Microsoft KB Archive/180707

= How To Reading a File on a Web Server from an Applet =

Article ID: 180707

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual J++ 1.0 Standard Edition
 * Microsoft Visual J++ 1.1 Standard Edition
 * Microsoft Software Development Kit for Java 2.02
 * Microsoft Software Development Kit for Java 2.01
 * Microsoft Software Development Kit for Java 2.02
 * Microsoft Software Development Kit for Java 3.0
 * Microsoft Software Development Kit for Java 3.1
 * Microsoft Software Development Kit for Java 3.2

-



This article was previously published under Q180707



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:

  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; }  } }                         Compile the ReadFile.java class, creating a ReadFile.class. Create a text file with at least one line of text and name it Readme.txt.  Create an HTML file named ReadFile.html with the following code: readfile  The source.  Put ReadFile.java, ReadFile.class, ReadFile.html, and Readme.txt in the same folder on your Web Server.</li> Open ReadFile.html from Internet Explorer.</li></ol>

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

Keywords: kbhowto kbcode KB180707

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.