Hej Søren,
tak for inspiration - din hjælp gjorde for et godt udgangspunkt. Til
andres evt. inspiration er koden her:
// handles creating a local directory to hold the application's
// underlying database, plus copying this from the jar to the
// dir.
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import javax.swing.JOptionPane;
public class SpssImageViewerDbConfigurator
{
String databaseName;
String applicationDir;
public String getDatabaseName() { return databaseName; };
public void setDatabaseName( String dbname) { databaseName = dbname; };
public String getApplicationDir() { return applicationDir; };
public void setApplicationDir( String applicationD) { applicationDir =
applicationD; };
public SpssImageViewerDbConfigurator() {}
public File getPathNameOfApplication()
{
// look for the 'program files' or 'programmer' catalogue
// on a local drive - return if found
File[] roots = new File("temp").listRoots();
File path = null;
for ( int i = 1; i < roots.length; i++)
{
File f1 = new File( roots[i] + "programmer\\" );
if ( f1.exists() )
{
//System.out.println( "have a programmer" );
if ( ! (roots[i].getAbsolutePath()).equals( "A:\\") &&
! (roots[i].getAbsolutePath()).equals( "B:\\") )
{
path = f1;
break;
}
}
File f2 = new File( roots[i] + "program files\\" );
if ( f2.exists() )
{
//System.out.println( "have a program files" );
if ( ! (roots[i].getAbsolutePath()).equals( "A:\\") &&
! (roots[i].getAbsolutePath()).equals( "B:\\") )
{
path = f2;
break;
}
}
}
return path;
}
public File generateDatabaseDirectory( File pathName )
{
File f = null;
if ( pathName != null )
{
f = new File( pathName + "\\" + applicationDir + "\\" );
if ( f.exists() )
{
// directory already exists - return
//System.out.println( f.getAbsolutePath() );
return f;
}
else
{
try
{
f.mkdir();
return f;
}
catch( SecurityException sex )
{
System.err.println( "Could not create application dir.");
return null;
}
}
}
return f;
}
public boolean configureDatabase(File appPath)
{
try
{
// retrieve database from jar
InputStream inputStream = getClass().getResourceAsStream(databaseName);
File f = new File(appPath.getAbsolutePath() + "\\" + databaseName);
applicationDir = f.getAbsolutePath();
// if db exists, exit
if ( f.exists() )
{
System.out.println( "database already exists - won't overwrite it" );
return true;
}
else
{
// configure output file
FileOutputStream outputStream = new FileOutputStream(appPath + "\\" +
databaseName);
// ensure other threads won't interfere by using 'synchronized'
synchronized( inputStream )
{
synchronized( outputStream )
{
byte bytes[] = new byte[256];
int read;
try
{
while (true)
{
read = inputStream.read( bytes);
if ( read == -1 ) break;
outputStream.write( bytes,0,read);
}
inputStream.close();
outputStream.close();
return true;
}
catch( IOException ioe )
{
System.err.println( "IOExeption");
return false;
}
}
}
} // end else
}
catch ( FileNotFoundException fnf )
{
fnf.printStackTrace();
return false;
}
catch ( Exception e )
{
e.printStackTrace();
return false;
}
}
}
"Soren Davidsen" <soren200303@tanesha.net> wrote in message
news:j3wuimfexd.fsf@tanesha.net...
> "Morten Nørgaard" <morten.norgaard_NOSPAM@uni-c.dk> writes:
>
> > Hej,
> >
> > jeg placerer et program i en jar-fil sammen med den access-database,
> > programmet refererer til. Når programmet startes vil jeg kopiere
databasen
> > fra jar'en til et katalog som jeg opretter på den maskine, jar'en køres
fra.
> > Men hvordan i alverden gør jeg det?
> >
> > TAK for inspiration,
>
> Lav en klasse til at kopiere den ud, og laeg database filen i samme
> pakke som denne..
>
> Klassen kan saa ligne noget ala dette:
>
> package mindb;
>
> public class DbConfigurator {
>
> public static void configureDatabase(String outputPath) throws .. {
>
> InputStream in = getClass().getResourceAsStream("min.db");
>
> FileOutputStream out = new FileOutputStream(outputPath);
>
> .. kopier in -> out og luk begge streams ..
> }
>
> }
>
> Din jarfil skal saa indeholde noget ala:
>
> mindb/DbConfigurator.class
> mindb/min.db
>
>
> Mvh,
>
> --
> ___
> Soren Davidsen / o\
> Math student, ICSMA (_____)
> __
http://www.tanesha.net/ _________________________________(___)_______