Take a specified file from JAR

xiaoxiao2021-03-06  60

Import java.io. *;

Import java.util.jar. *;

Import java.util.zip. *;

Public class extractfromjar {

Public void extractMydbfromjar (String Dest) {

Try {

String home = getClass (). GetProtectionDomain ().

GetCodesource (). getLocation (). Tostring ().

Substring (6);

Jarfile jar = new jarfile (home);

ZIPENTRY Entry = jar.geTEntry ("mydb.mdb");

File Efile = New File (DEST, Entry.getName ());

InputStream IN = New BufferedInputStream (jar.getinputstream (entry);

OutputStream out = new bufferedoutputstream (new fileoutputstream (es);

BYTE [] Buffer = New byte [2048];

For (;;) {

INT nbytes = in.read (buffer);

IF (nbytes <= 0) Break;

Out.write (buffer, 0, nbytes);

}

Out.flush ();

Out.close ();

In.Close ();

}

Catch (Exception E) {

E.PrintStackTrace ();

}

}

Public static void main (string args []) {

New extractfromjar (). ExtractMydbfromjar (".");

}

}

Run, test

Java -jar mydb.jar

转载请注明原文地址:https://www.9cbs.com/read-112799.html

New Post(0)