Writing Text Files in Java

hello!

i am creating an Applet game and I would like to create Rankings.

So, i need to take data from java and transfer them into HTML. At my

university's book says that everytime we open a file to write in it, if there are

already data, Java removes them and then writes the new ones. But I want

a static file where Java can write the updated rankings and HTML can take

them and use them in site. Is there any way to achive that guys?

If i wasn't clear, i just want a way to make Java not to delete any data in a file

everytime it needs to write something in it!

thanks alot!

[639 byte] By [StorM_GmAa] at [2007-11-27 6:01:22]
# 1

> hello!

>

> i am creating an Applet game and I would like to

> create Rankings.

> So, i need to take data from java and transfer them

> into HTML. At my

> university's book says that everytime we open a file

> to write in it, if there are

> already data, Java removes them and then writes the

> new ones. But I want

> a static file where Java can write the updated

> rankings and HTML can take

> them and use them in site. Is there any way to achive

> that guys?

>

> If i wasn't clear, i just want a way to make Java not

> to delete any data in a file

> everytime it needs to write something in it!

>

> thanks alot!

There are a number of problems here.

First of all you can always append to a file, but that's not actually what you want to do.

Second you can read the whole file in, find the values you want, change them and write the whole file back. This is more of what you want to do but HTML is not the best choice for doing this.

But the most fundamental problem is that you do not write to files of any kind in an Applet.

cotton.ma at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 2
[url] http://java.sun.com/javase/6/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String, boolean)[/url]~
yawmarka at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 3

oh...i can't write files in an Applet? it sounds weird to me! there are lots of

Java games over the internet with Rankings and Scores and stuff like that...

somehow they are writing files...or maybe they use a way I don't know?

i am not an expert in Java (i'm new in fact) so i might don't know the correct

way to do what i want to do. But I strongly believe that there should be a way

because it's a common thing!

StorM_GmAa at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 4

> oh...i can't write files in an Applet?

No*

>it sounds

> weird to me! there are lots of

> Java games over the internet with Rankings and Scores

> and stuff like that...

> somehow they are writing files

No.

>...or maybe they use a

> way I don't know?

Yes.

They are sending data back to the webserver through a Socket.

This would be the preferred way of doing it.

* - Technically you can write to files in an Applet BUT you must sign your applet and request permission to do so, this is involved. Further I get the strong feeling it doesn't do what you want anyway. If you want to save a high score you will not be writing to the HTML file. This is not the way the internet works.

cotton.ma at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 5
my book didn't mention that boolean indicating whether or not append thedata written in file...so that boolean takes True to keep data and write the newafter them and False to delete everything and write the new data? or the opposite?
StorM_GmAa at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 6
i will not write in an HTML file! just a .txt and then i will have HTML code thatwill go and read that .txt! everything is gonna happen localy...it's just anAssignment for my university...that game will not go online or something!
StorM_GmAa at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 7

> my book didn't mention that boolean indicating

> whether or not append the

> data written in file...so that boolean takes True to

> keep data and write the new

> after them and False to delete everything and write

> the new data? or the opposite?

Append will not help you.

Append true - to keep data and add (append) new data.

But it won't help. Think about what appending data to an HTML file means.... not changing. Appending/adding

cotton.ma at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 8

> i will not write in an HTML file! just a .txt and

> then i will have HTML code that

> will go and read that .txt! everything is gonna

> happen localy...it's just an

> Assignment for my university...that game will not go

> online or something!

Then don't use an Applet!

Or post the data back to the server using HttpURLConnection (recommended).

cotton.ma at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 9
> Then don't use an Applet!unfortunately i have to use Applet...it's Professors request... =\
StorM_GmAa at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 10
> > Then don't use an Applet!> > unfortunately i have to use Applet...it's Professors> request... =\post the data back to the server using HttpURLConnection (recommended).
cotton.ma at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 11
ok mate i will try to do that!thanks alot for your time & help!
StorM_GmAa at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...
# 12
> ok mate i will try to do that!> It;s not too hard really. See this http://java.sun.com/docs/books/tutorial/networking/urls/index.html> thanks alot for your time & help!No problem. Good luck.
cotton.ma at 2007-7-12 16:41:06 > top of Java-index,Java Essentials,Java Programming...