Solved

Applet write text to a file problem!

Posted on 1997-05-28
6
209 Views
Last Modified: 2011-09-20
Dear all,
  Can anyone tell me how to use applet to write text or several characters to a file?  I have implemented a method, it fail!

  here is my code :

  publi void fileop extends Applet {
  URLConnection urlcon;
  URL url;
  DataOutputStream BufOut;

      public void init() {
       try {
       url = new URL(getDocumentBase, "test.dat");
       urlcon = url.openConnection();
       BufOut = new DataOutputStream(urlcon.getOutputStream);
       } catch(IOEXCEPTION e) {};
      }

      public void paint(Graphics g) {
       try {
         BufOut.writeBytes("Hello! World");
         ButOut.close();
       } catch (IOEXception e) {}
      }
}


  Pls help me to point out that what the problem is?

Thanks!

Benson
     
0
Comment
Question by:benson031397
6 Comments
 

Expert Comment

by:hosseinakhlaghpour
ID: 1220994
First of all applet can not write to a file. Either you should run it as an applet or run it with appletviewer only in your machine.It is the security violation.Second you should open a file, BufOut is not even connected to a file.Third if you really want to write to a file from applet you should run an application as server which have the socket connectivity to your applet and perform the save or load operation for appleto. (or use RMI)For 20 points I am sure you don't expect more.Hossein Akhlaghpour513-475-1435
0
 

Author Comment

by:benson031397
ID: 1220995
I mean I would like to write text to the server side where the applet is downloaded from.  From the spec. of applet, applet can read or write to a file which is located in the server where the applet come from., right?  Your answer is just correct that applet want to write or read client harddisk.
0
 

Author Comment

by:benson031397
ID: 1220996
Edited text of question
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:benson031397
ID: 1220997
Pls help me again!  The answeris wrong!
0
 
LVL 1

Expert Comment

by:mlimotte
ID: 1220998
Hossein's answer is not wrong.  Perhaps a little trite, and a little brief.  But that's what 20 points will buy you.  I'll expand on that answer just a little

An applet can not write directly to a file, but it can write to the server where the applet came from.  This can be done in a number of ways: through sockets, with RMI, as a POST/GET method to a cgi (server-side) application, etc.

With more details and more points perhaps someone would help you choose an appropriate method and give you details on how to implement it.
 
0
 

Accepted Solution

by:
istiles earned 20 total points
ID: 1220999
There is a better way than to use RMI or POST/GET. RMI has the problem of requiring a JVM on the host server. POST/GET do not provide much control for non-whole file writes and there is no support for listing directories or other file system queries.

What is a better solution is to use another protocol that your host server most likely already supports, like FTP or NFS. Rather that roll-your-own, Jentec provides a free product called JENI that allows applets/applications to perform file I/O, printing, directory services, and e-mail support to any server.

JENI handles the differences of the various protocols for you and provides a single interface. Currently supported protocols include NFS, FTP, LDAP, POP, SMTP, and local (if in application mode). Servers supported include NetWare 3.x, 4.x, IntranetWare, NT Server, Sun Solaris, Netscape Directory server, and University of Michigan SLAPD Server.

And now for some code. After you download JENI from "http://www.jentec.com", use the following code to create and write to a file:

try {
     Branch parent = (Branch) TreeFactory.getObject ("some_domain.com/path");
     Leaf newfile = parent.add (new MemoryLeaf ("newFileName"));
     RandomAccessStream stream = newfile.getAttributeValue ("data stream");
     stream.open (RandomAccessStream.MODE_WRITE);
     stream.writeBytes ("Now is the time for an evolution.");
     stream.close();
     System.out.println ("Created: " + newfile.getName());
} catch (IOException except) {
     System.out.println (except.toString());
}

Hope this helps,

Ian.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now