my dog learned polymorphism*
The moose likes Applets and the fly likes file write Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "file write" Watch "file write" New topic
Author

file write

dhanildas das
Greenhorn

Joined: Sep 04, 2009
Posts: 11
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 .

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41141
    
  45
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.


Ping & DNS - my free Android networking tools app
dhanildas das
Greenhorn

Joined: Sep 04, 2009
Posts: 11
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

Joined: Sep 04, 2009
Posts: 11
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
Marshal

Joined: Mar 22, 2005
Posts: 41141
    
  45
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.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

If you want do file operations, you must "get out of the sandbox". Please read this article: Link
dhanildas das
Greenhorn

Joined: Sep 04, 2009
Posts: 11
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,
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
dhanildas das
Greenhorn

Joined: Sep 04, 2009
Posts: 11
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

Joined: Sep 04, 2009
Posts: 11
I can't save the contents means ,the contents in the file get over written ,when I restart the program with the new one
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Sep 04, 2009
Posts: 11
sir, How it's run with explorer (IE)

please reply
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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

Joined: Sep 04, 2009
Posts: 11
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

Joined: Sep 04, 2009
Posts: 11
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
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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.
 
 
subject: file write
 
Similar Threads
character code
Setting focus to a dialog button?
Trapping keys in java applet
java code problem?
wriring code for an EDITOR big problem??