This discussion is archived
4 Replies Latest reply: Nov 25, 2008 5:56 AM by 843785 RSS

How to copy sqlite file from jar that I execute

843785 Newbie
Currently Being Moderated
Hi to all,

I have a little noobe question to ask.
I want my java program to copy sqlite file that is inside my projects jar file into the userdir on the hard drive. Is there a posibility to open this type of sourcefile as a InputStream? If so, how?

I tried something like this:
public boolean copy_source_file(String source, String destination){
        InputStream in1 = null;
        OutputStream out1 = null;
        
        try{
            in1 = this.getClass().getClassLoader().getResourceAsStream(source);
            File dest = new File(destination);
            if (dest.exists()==false) dest.createNewFile();
            out1 = new FileOutputStream(dest);
    
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in1.read(buf)) > 0) {
                out1.write(buf, 0, len);
            }
            out1.flush();
            
            return true;
        }
        catch (IOException e) {
           System.err.println("Error while copying a file\n Error message : " + e.getMessage());
           return false;
        }finally{
            try{
                in1.close();
                out1.close();
            }
            catch(IOException e){
                System.err.println("Error while copying a file\n Error message : " + e.getMessage());
 
            }
        }
but it gives me null pointer exception (so the in1 variable is set to null).

Could someone help me on this? Is there a way to do this in a static function?