2 Replies Latest reply: Oct 9, 2006 8:21 AM by 807607 RSS

    How to write file from excutable jar?

    807607
           public void saveHiScore() {
                
                try{
                     FileOutputStream fos = new FileOutputStream(FILE_NAME);
                     ObjectOutputStream oos = new ObjectOutputStream(fos);
                     System.out.println("Writing current hi-score into "+FILE_NAME);
                     oos.writeObject(this);
                }catch(FileNotFoundException fnfe){
                     System.err.println("FOF!!");
                     fnfe.printStackTrace();
                }catch(IOException ioe){
                     System.err.println("IOException!!");
                     ioe.printStackTrace();
                }
                System.out.println("Save complete");
           }
      
           public static HiScoreManager loadHiScore(){
                HiScoreManager hsm = null;
                try{
                     URL url = HiScoreManager.class.getResource("./"+FILE_NAME);
                     System.out.println(url);
                     FileInputStream fis = new FileInputStream(url.toString());
                     ObjectInputStream ois = new ObjectInputStream(fis);
                     hsm = (HiScoreManager)ois.readObject();
                     //System.out.println("Reading "+hsm+" from "+FILE_NAME);
                }catch(FileNotFoundException fnfe){
                     System.err.println("FOF!!, going creating new File after game end");
                     return new HiScoreManager();
                }catch(IOException ioe){
                     System.err.println("IOException!!");
                     ioe.printStackTrace();
                }catch(ClassNotFoundException cnfe){
                     System.err.println("ClassNotFoundException!!");
                     cnfe.printStackTrace();
                }
                return hsm;
           }
           
      This code work without problem, but when I exported it into Excutable Jar-File, it seems like this doesn't work.

      I've tried to use URL url = getClass().getResource(FILENAME); but it doesn't work, i don't see any OutputStream accept URL as their argument.

      thank you very much
        • 1. Re: How to write file from excutable jar?
          807607
          If it's possible to write a file into a jar on the fly, it's not easy. That's not really what it's for, and I think generally it would be a bad idea. Think about deployment units. If you create a resource for deployment (e.g., a jar file) the last thing you want is for that resource to change at run time. It would make support impossible. (Imagine this in a large-scale enterprise environment.)

          I think for a high score file, there are two good options. For a file that's shared between users, you can contact a central server and send scores there. For a single player, you can do
          System.getProperty("user.home") 
          (I think so; check the API docs to make sure) to get the user's home directory, and then write the file there or under there (for example on a Windows system you might want to get the home directory and then look for the "Application Data" folder, or something.
          • 2. Re: How to write file from excutable jar?
            807607
            That's very helpful, thank you very much ^^ I've finished my project.