Solved

Applet write text to a file problem!

Posted on 1997-05-28
6
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 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