Skip to main content
gamedev.net
🔒 Locked

[java] I do not understand what I am doing wrong.

Started by the_better_nick Dec 5, 2001 at 10:11 AM 3 replies 800+ views
Original Post
the_better_nick
the_better_nick
It is my understanding that the following code should dump something in a file, yet my applet states that the file can not be accessed. Any ideas? FileOutputStream fos; try{ fos = new FileOutputStream("work.txt"); String s = "THE END."; byte b[] = new byte[s.length()]; s.getBytes(0, s.length(), b, 0); FileDescriptor fd = fos.getFD(); if(fd.valid() == false) msg += "File Problem"; else fos.write(b); } catch(IOException e) { msg += "Write file failed"; } Thanks ·†§ Nick §† ·
·†§ Nick §† ·
WayfarerX
WayfarerX
You can't access files from an applet, it's a security measure.
Imagine if every applet on the net could create/edit/delete any file on your hard drive. Java would never have been let into the browser.
Looks like it should work if you try running it from an application.

"So crucify the ego, before it's far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that's imagined and all conceivable."
- Tool
------------------------------


Edited by - wayfarerx on December 5, 2001 12:03:44 PM
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
CaptainJester
CaptainJester
Applets can only access files from the server that they came from and only ones in the same directory.

I don''t know if this will work, but you can try it:

fos = new FileOutputStream(getDocumentBase()+"work.txt");


---
Make it work.
Make it right.
Make it fast.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
WayfarerX
WayfarerX
quote:
Original post by CaptainJester
Applets can only access files from the server that they came from and only ones in the same directory.

I don''t know if this will work, but you can try it:

fos = new FileOutputStream(getDocumentBase()+"work.txt");



Applets access files on the server via the HTTP protocall (just like your web browser), and HTTP doesn''t provide for writing to a file (well, actually it does, but I don''t know of a single server on the internet that allows you to do so).

If you want to save data on your server you have to set up some type of server-side program (ASP, PHP, Servlets, etc) that will accept the data from your browser and do something with it.

"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Jim_Ross
Jim_Ross
Applets are evil. Don''t ever write an applet. Use java webstart if you want to launch an Java app from a web page. And since konqueror is the only web browser to natively support Java2 (>=1.2), it''s pretty tough to develop an applet that doesn''t suck.

One other thing about your code
fos = new FileOutputStream("work.txt");

Where do you think the file work.txt would be saved? If an applet is running in a browser, what is the current working directory?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.