Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Applet write text to a file problem!

Posted on 1997-05-28
6
Medium Priority
?
253 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
Industry Leaders: 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 40 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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