• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

file write

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have searched a lot about the file write operation in java applet , here is my code I want to write the characters to the specified file.
Please help me how to write the each character entered to a specified file .

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

In the future, please http://faq.javaranch.com/java/UseCodeTags if you post code of any length; it makes it much easier to read it. I've edited your post accordingly.

As to your question, I'm not sure what "the specified file" is - there's nothing at all about files in the code ... ? But be aware that applets can't normally write files at all due to security restrictions; see http://faq.javaranch.com/java/HowCanAnAppletReadFilesOnTheLocalFileSystem for more details.
 
dhanildas das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Welcome to JavaRanch.

In the future, please http://faq.javaranch.com/java/UseCodeTags if you post code of any length; it makes it much easier to read it. I've edited your post accordingly.

As to your question, I'm not sure what "the specified file" is - there's nothing at all about files in the code ... ? But be aware that applets can't normally write files at all due to security restrictions; see http://faq.javaranch.com/java/HowCanAnAppletReadFilesOnTheLocalFileSystem for more details.



*
 
dhanildas das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sir, thanks for your reply

Where should i put the "file write code" in my applet code ?

Is there any way to write the file in applet ? I have experienced the same security reason during writing the file .

please help me.

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to write characters to the file as they are typed, then the code should go into the keyTyped method. Remember to open the file in "append" mode (unless you're planning to keep the file open).

As to working around the security issue, read the link I provided (and the pages it links to); there's plenty of information about what you need to do.
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want do file operations, you must "get out of the sandbox". Please read this article: Link
 
dhanildas das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ,Now i bypass the security problem in "applet file write" , but i don't know how to " create a file " in applet .

please reply,
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dhanildas das wrote:Thanks ,Now i bypass the security problem in "applet file write" , but i don't know how to " create a file " in applet .

please reply,



I'm a bit confused. If you know how to deal with the security issue relating to writing a file, how come you don't know how to write a file? After all, how do you understand or test security without actually writing a file? Or do you simply mean that you now know that there is an issue, but have not read any of the articles provided yet?

Anyway... take a look at the java.io.File and java.io.FileOutputStream class. It may also be a good idea to read up on the java.io package, as there are other file related classes too.

Henry
 
dhanildas das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply sir,
Now I have corrected my code(It's a "key logger program") , but it has a lot of inefficiencies

1. I can't write the character's when i use the explorer (IE) instead of applet viewer

2. I can't save the contents in the file ,here comes the code

[

public class WriteFile1 extends Applet implements KeyListener
{

String c="";
String da="";
String myFile = "das.txt";
File f = new File(myFile);

DataOutputStream dos;

public void init ()
{

addKeyListener(this);
}


public void keyTyped(KeyEvent ke)
{

char c = ke.getKeyChar();
da+=c;
repaint();


}

public void paint (Graphics g)
{

try
{
dos = new DataOutputStream(new BufferedOutputStream
(new FileOutputStream(myFile), 1));
dos.writeChars(da);
dos.flush();
g.drawString("Successfully wrote to the file named " + myFile
+ " -- go take a look at it!", 10, 10);
}

catch (SecurityException e)
{
g.drawString("writeFile: caught security exception: " + e, 10, 10);
}

catch (IOException ioe)
{
g.drawString("writeFile: caught i/o exception", 10, 10);
}


}

}

[/code]

The policy code to write the file is,
[code]
grant {
permission java.io.FilePermission "<<ALL FILES>>","write";
};
[/code]

applet code used to run the file is appletviewer -J-Djava.security.policy=WriteFile1.policy WriteFile1.java

please reply

















 
dhanildas das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't save the contents means ,the contents in the file get over written ,when I restart the program with the new one
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please check the constructors for your File writing classes; some of them have a flag allowing you to append material to your file. Otherwise the file will be overwritten.
 
dhanildas das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sir, How it's run with explorer (IE)

please reply
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you not declaring your writers as local variables? Why have you got the file writing code in the paint method? Why are you not closing the writers?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dhanildas das wrote:sir, How it's run with explorer (IE)

please reply



You do know that it isn't a simple command line parameter right?

I suggest you google "signed applet" to understand the process. And then later, google "signed applets" along with "IE" or "browser" to understand how it is installed with browsers that support signed applets.

Henry
 
dhanildas das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have the correct solution to your question ,sorry for that but

1.it's possible to close writer after the program is terminated.(I have checked it).

2.It's possible to declare writers as local variables(I have checked it).

3.The program is not working if i write the code outside the paint method.(I am not sure )

I am expecting your comments.
 
dhanildas das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you sir,
I will search for it , I have got another doubt ,

(1) Is there any way to run a java program on "start up" (in windows).?

(2) Is there any way to build an "invisible" applet?

(3) Any way to convert a java to "exe" form?

please reply
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dhanildas das wrote:I don't have the correct solution to your question ,sorry for that but

1.it's possible to close writer after the program is terminated.(I have checked it).

. . .

You need to get into the habit of closing writers in your code. "Close after termination" is no excuse for poor design.

No longer a beginner's question. Moving.
reply
    Bookmark Topic Watch Topic
  • New Topic