12 Replies Latest reply: May 31, 2007 6:56 AM by 807606 RSS

    Writing Text Files in Java

    807606
      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!
        • 1. Re: Writing Text Files in Java
          807606
          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.
          • 3. Re: Writing Text Files in Java
            807606
            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!
            • 4. Re: Writing Text Files in Java
              807606
              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.
              • 5. Re: Writing Text Files in Java
                807606
                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?
                • 6. Re: Writing Text Files in Java
                  807606
                  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!
                  • 7. Re: Writing Text Files in Java
                    807606
                    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
                    • 8. Re: Writing Text Files in Java
                      807606
                      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).
                      • 9. Re: Writing Text Files in Java
                        807606
                        Then don't use an Applet!
                        unfortunately i have to use Applet...it's Professors request... =\
                        • 10. Re: Writing Text Files in Java
                          807606
                          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).
                          • 11. Re: Writing Text Files in Java
                            807606
                            ok mate i will try to do that!

                            thanks alot for your time & help!
                            • 12. Re: Writing Text Files in Java
                              807606
                              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.