Solved

Applet write text to a file problem!

Posted on 1997-05-28
6
218 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 38
Java array passed to SQL where clause 23 62
javap bin 2 27
xampp tool 12 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

912 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

24 Experts available now in Live!

Get 1:1 Help Now