HOWTO: Reading a File on a Web Server from an AppletID: Q180707
|
The following sample code shows how to read a file on a Web Server from an applet.
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:
//
// 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();
}
}
}
<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>
Additional query words:
Keywords : kbcode kbSDKJava300 kbSDKJava310 kbSDKJava320
Version : WINDOWS:1.0,1.1,2.0,2.01,2.02,3.0,3.1,3.2
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 9, 1999