Results 1 to 4 of 4
  1. #1
    JamieG is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Applet reading/writing to server side file

    Hi Java Forums,
    I hope you guys can help me, I'm new to the forums and relatively new to Java. I have written a game which extends Canvas, and I am currently trying to write methods to save/read high scores to a file in the same directory the game is stored in. When I run the game in a JPanel it works fine, however when I upload to web space and run the game in an applet through the browser, the methods literally do nothing, they do not throw exceptions or anything. Ive done some reading around and suspect I need to add a policy file, but can't find any specific instructions to help with my problem. Here are the methods I have written to read/write to a file

    Java Code:
    public void saveHighScores() {
            try {
                FileWriter fs = new FileWriter("./out.txt");
                BufferedWriter out = new BufferedWriter(fs);
                for (int i = 0; i < 5; i++) {
                    out.write(names.get(i) + " ");
                    out.write(scores.get(i).toString() + " ");
                }
                out.close();
            }catch (Exception e) {
                System.err.println(e);
            }
        }
    Java Code:
    public void loadHighScores() {
            try {
                Scanner in = new Scanner(new File("./out.txt"));
                while (in.hasNext()) {
                    names.add(in.next());
                    scores.add(Integer.parseInt(in.next()));
                }
                in.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Applet reading/writing to server side file

    To read/write to a server side file you will need to communicate with the server. The classes for communicating with a server include: Socket, HTTPURLConnection and URLConnection.
    Your code uses Files which are for the local PC.

    Where is it you want to read/write the files: locally on the PC with the browser or on the server the applet was loaded from?

  3. #3
    JamieG is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Applet reading/writing to server side file

    I want the file to be saved on the server the applet is hosted on, so that the high score list is maintained for every user who plays the game on different machines. could you possibly direct me to a tutorial or some example code which demonstrates how to do this?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Applet reading/writing to server side file

    Sorry, I don't have a link to a tutorial myself.
    Try Google

Similar Threads

  1. updating server side file using java bean
    By tskarthic in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-30-2009, 03:53 PM
  2. How to open a file located in server from client side
    By Malathi in forum Web Frameworks
    Replies: 4
    Last Post: 04-20-2009, 10:10 PM
  3. [SOLVED] Applet reading from file on server
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-27-2009, 12:33 AM
  4. Replies: 2
    Last Post: 10-05-2008, 07:13 PM
  5. Reading/Writing files through Applet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-15-2007, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •