Solved

Applet write text to a file problem!

Posted on 1997-05-28
6
230 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 37
javap not working 8 46
How to execute a Python program and gather return output in Java 2 31
pagenation logic how it is working in my code 1 32
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

832 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