简体   繁体   English

Java Applet输出文件

[英]Java Applet Output File

I am trying to create a Java Applet that outputs information to a text file located in the same directory as the java applet. 我正在尝试创建一个Java小程序,该程序将信息输出到与Java小程序位于同一目录中的文本文件。 I understand Java Applets are not ideal, but I have spent a great deal of time on this and if possible want to solve this through applets. 我知道Java Applet并不理想,但是我花了很多时间在这上面,如果可能的话,希望通过Applet解决。 Here is some of my code on how I could read code from a file into a text box. 这是一些有关如何将文件中的代码读取到文本框中的代码。 I assume it would be something similar to this, but outputted. 我认为这将与此类似,但已输出。

public void readFile() {
  String line;
  URL url = null;
  try {
     url = new URL(getCodeBase(), fileToRead);
  }
  catch(MalformedURLException e) {
  }
  try {
     InputStream in = url.openStream();
     BufferedReader bf = new BufferedReader
     (new InputStreamReader(in));
     strBuff = new StringBuffer();
     while((line = bf.readLine()) != null){
        strBuff.append(line + "\n");
     }
     a1.append("File Name : " + fileToRead + "\n");
     a1.append(strBuff.toString());
  }
  catch(IOException e) {
     e.printStackTrace();
  }
}

If you want an applet to store data on the local machine, from 6u10 the javax.jnlp.PersistenceService is available. 如果希望applet将数据存储在本地计算机上,则从6u10 javax.jnlp.PersistenceService可以使用javax.jnlp.PersistenceService

or on your local machine... 或在您的本地计算机上...

java.io.File file = new java.io.File(System.getProperty("user.home"), "yourfile.txt");

You must have it signed, otherwise... 必须先签名,否则...

Keep in mind that from an Applet, you cannot directly write to the server's file system. 请记住,您无法从Applet直接写入服务器的文件系统。 You can issue a request to the server that causes the server to write to its own file system, but an Applet does not have a way to write to a file system on a remote machine. 您可以向服务器发出请求,使服务器写入其自己的文件系统,但是Applet无法写入远程计算机上的文件系统。

A signed Applet has every right to write to the local file system of the person running the Applet. 签名的Applet有权写入运行Applet的人员的本地文件系统。 If you are writing to the "current directory" (rather than an absolute full path), then make sure you know what directory the Applet is running in. Otherwise you may indeed create a file, but not be able to find it! 如果要写入“当前目录”(而不是绝对完整路径),请确保您知道Applet在其中运行的目录。否则,您可能确实创建了一个文件,但找不到它!

EDIT 编辑

Signed Applet Tutorial 签署的Applet教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM