How do I let an applet read a file?

longbow 2001-01-06 08:57:00
以下是sun的faq的说明,那该死的文件在哪?
Applets loaded into a Java-enabled browser can't read files.

Sun's appletviewer allows applets to read files that are named on the access control list for reading. The access control list for reading is null by default, in the JDK. You can allow applets to read directories or files by naming them in the acl.read property in your ~/.hotjava/properties file.


Note: The "~" (tilde) symbol is used on UNIX systems to refer to your home directory. If you install a web browser on your F:\ drive on your PC, and create a top-level directory named .hotjava, then your properties file is found in F:\.hotjava\properties.
For example, to allow any files in the directory home/me to be read by applets loaded into the appletviewer, add this line to your ~/.hotjava/properties file.

acl.read=/home/me

You can specify one file to be read:
acl.read=/home/me/somedir/somefile

Use ":" to separate entries:
acl.read=/home/foo:/home/me/somedir/somefile

Allowing an applet to read a directory means that it can read all the files in that directory, including any files in any subdirectories that might be hanging off that directory.
...全文
71 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mars_lee 2001-01-06
  • 打赏
  • 举报
回复

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

/**
* <strong>ReadFromURLAppletExample</strong> -- reading the file
* specified by a URL.
*/
public class ReadFromURLAppletExample extends java.applet.Applet {

TextArea messageLog = new TextArea(4, 40);

/** Builds the applet's interface. */
public void init() {
setLayout(new BorderLayout());
add("Center", messageLog);
}

/**
* Called when the applet is started or when the browser
* returns to the applet's page.
*/
public void start() {

URL url = getDocumentBase();
URLConnection connection;
String inputLine;
DataInputStream inStream;

/* Create the URL connection and get its input stream.
* Wrap a DataInputStream around the input stream in order to
* read from it one line at a time.
* Print out only lines that contain the specified string.
* Close the input stream when done.
*/
try {
connection = url.openConnection();
messageLog.appendText("File read from URL " + url + ":\n");
inStream = new DataInputStream (connection.getInputStream());
while (null != (inputLine = inStream.readLine())) {
messageLog.appendText(inputLine + "\n");
}
inStream.close();
} catch (IOException e) {
System.err.println("Exception: " + e);
}
}

/** Handles action events. */
public boolean action(Event evt, Object what) {
return super.action(evt, what);
}

/** Draws the applet. */
public void paint(Graphics g) {
}

}

This one???

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧