Program installation, upgrade, and unload core part (Java writing)

xiaoxiao2021-03-06  47

The work of the installer: Copy the source file to the corresponding directory. The work of the upgrade program: Overwate the file in the source file override the destination file, the added file is copied to the corresponding directory. Uninstalling the program: Delete the contents of the program folder. For the above, write a simple installer (main file: installsheildimpl, using a recursive way to folder traversal) / ********************* ********************************************************** * Module: Installsheild.java * Author: Administrator * Created: 2004-12-13 22:37:53 * Purpose: Installer interface for unified call mode. *********************************************************** ******************** / pack org.heixue.test.install;

/ ** * @author Administrator * / public interface InstallSheild {public final static int INSTALL = 1; public final static int UPDATE = 2; public final static int UNINSTALL = 3; public void install (int type, String srcFold, String destFold) THROWS Installexception;} / ************************************************************************************************************************************************************************************************************************************************************************** ************************* * Module: InstallsheildImpl.java * Author: administrator * create: 2004-12-13 22:48:20 * Purpose: Implementation of the installer ******************************************************* *********************************** / package org.heixue.test.install;

Import java.io. *;

// Import org.heixue.util.log; import org.heixue.util.filelog; import org.heixue.util.file.filecopy;

/ ** * @Author administrator * / public class installsheildimpl Implements installsheild {private string srcfold = null; private string destFold = null; private filelog log = null;

/ ** * * / public installsheildimpl () {}

/ * * @See org.heixue.test.update.InstallSheild # install (java.lang.String, java.lang.String, int) * / public void install (int type, String srcFold, String destFold) throws InstallException {this .srcfold = srcfold; this.Destfold = destfold; if (config.get ()! = null) log = new filelog (config.get (); else log = new filelog (system.out); if (destfold == NULL) THROW NEW INSTALLEXCEPTION ("You do not set a destination folder location!"); switch (type) {copy installsheild.install: if (srcfold == null) throw new installexception ("You do not set the source folder location!") DOINSTALL (); Break; Case Installsheild.Update: IF (SrcFold == Null) Throw new installexception ("You don't have a source folder location!"); doupdate (); break; case installsheild.unInstall: DounInstall (); Break; default: throw new installexception ("No this action!");}} / ** *: * #Prpose: The installer, the main file copy. * / public void doinstall () throws installexception {ix (srcfold == NULL) Throw new installexception ("You do not set the source folder location!"); if (destfold == null) Throw new installexception ("You do not set the destination folder location!"); file file1 = new file (srcfold Fil e file2 = new file (destfold); if (! file2.exists ()) file2.mkdir (); installfiles (",", ");} private void installfiles (String src, string dest) throws installexception {file file1 = New file (srcfold, src); file file2 = new file (destFold, dest); if (file1.isfile ()) {log.info (file2.getpath ()); filecopy.copybyfile (file1, file2);} else IF (file1.Indirectory ()) {if (! file2.exists ()) file2.mkdir (); log.info (file2.getpath ()); file [] fs = file1.listfiles (); for (int i) = 0; I

}}} Else {throw new installexception ("Does not exist the file or directory!");}} / ** *: * #Prpose: Upgrade program {If (srcfold == null) throw new installexception ("You don't have a source folder location!"); If (destfold == null) throw new installexception ("You do not set the destination folder location!"); File file1 = New file (srcfold); file file2 = new file (destfold); if (! file2.exists ()) file2.mkdir (); UpdateFiles ("," ");} private void updatefiles (String src, string dest) Throws installexception {file file1 = new file (srcfold, src); file file2 = new file (destFold, dest); if (file1.isfile ()) {if (! file2.exists () || file1.lastmodified ()> File2.lastmodified ()) {log.info (file2.getpath ()); filecopy.copybyfile (file1, file2);}} else}} else if (file1.exists ()) file2. Mkdir (); log.info (file2.getpath ()); file [] fs = file1.listfiles (); for (int i = 0; i

File1.delete (); log.info (file1.getpath ());} else {throw new installexception ("No file or directory!");}} Public static void main (String [] args) throws installexception {InstallsheildImpl ISI = New InstallsheildIMPL (); isi.install (Installsheild.Install, "D: // Test", "D: // Test2"); //iisi.install (INSTALLSHEILD.UPDATE, "D: /TEST "," D: // Test2 "); //isi.install (INSTALLSHEILD.Uninstall, "D: //test");}} / ********* *********************************************************** *********** * MODULE: config.java * Author: administrator * create: 2004-12-14 10:24:06 * purpose: Some configuration information ********* *********************************************************** *********** / pack org.heixue.test.Install; import java.io.filenotfoundException; import java.io.fileoutputstream; import java.io.outputstream;

/ ** * @author Administrator * / public class Config {private static int installType = 0; private static String srcFold = null; private static String destFold = null; private static OutputStream out = null; private static Config _config = null;

/ ** * * / public void initialize (int type, String src, String dest, String logPath) throws FileNotFoundException {if (logPath = null!) Out = new FileOutputStream (logPath); initialize (type, src, dest, out) } / ** * @Param Type: Installation Type * @Param SRC: Source Folder * @Param Dest: Target Folder * @Param O: Log Stream Output * / Public Void Initialize (int Type, String src, string Dest) OutputStream O) {InstallType = Type; SrcFold = src; destfold = dest; out = o;

/ ** * @Return: * #Perpose: * / public static string getDestfold () {return destfold;}

/ ** * @Return: * #Perpose: * / public static int getInstallType () {return installtype;} / ** * @return: * #perpose: * / public static outputstream getout () {return out;}

/ ** * @return: * #perpose: * / public static string getsrcfold () {return srcfold;}

/ ** * @Return: * #perpose: * / public static config getinstance () {if (_config == null) _config = new config (); returnfig;}

} / ******************************************************* ********************** * Module: installexception.java * Author: administrator * create: 2004-12-13 22:53:25 * purpose: Installation Process is abnormal ********************************************************************************************************************************************************************************************************************************************************************************** ********************** / package org.heixue.test.install;

/ ** * @Author administrator * / public class installexception extends Exception {

/ ** * * / public installexception () {super ();} public installexception (String Reason) {Super (REASON);

} / ******************************************************* *********************** * Module: install.java * Author: administrator * create: 2004-12-14 10:53:28 * purpose: Installation Class of files, input parameters: installation type, source folder, target folder, log file ******************************************* ************************************************************ / package org.heixue.test.install ;

Import java.io.filenotfoundexception;

/ ** * @Author administrator * / public class install {

/ ** * * / public Install () {super (); // TODO Auto-generated constructor stub} public static void main (String [] args) throws FileNotFoundException, InstallException {int installType = 0; String srcFold = null; String Destfold = null; string logpath = null; if (args.length == 4) {if (args [0] .Equals ("install") {InstallType = installsheild.install;} else if (args [0] .Equals ( "update")) {installType = InstallSheild.UPDATE;} else if (args [0] .equals ( "uninstall")) {installType = InstallSheild.UNINSTALL;} else {} srcFold = args [1]; destFold = args [2]; logPath = args [3]; if (logPath.equals ( "null")) logPath = null; Config.getInstance () initialize (installType, srcFold, destFold, logPath);. InstallSheild is = new InstallSheildImpl () ; is.install (installType, srcFold, destFold);} else {System.out.println ( "command line: java Install Type srcFold destFold logPath"); System.out.println ( "eg: java Install install d: // TEST D: // Test2 D: //install.log ");}}} / under the two tools / ********************* **** ***************************************************** * Module: FILECOPY. Java * Author: administrator * create: 2004-12-6 22:20:15 * purpose: file copy *********************************** ***************************************************** / package org.heixue. Util.file;

Import java.io. *;

/ ** * @Author administrator * / public class filecopy {

/ ** * * / public filecopy () {} public static boolean copy (String src, string dest) {Try {// instance the file as file_in and file_out java.io.file file_in = new java.io.file (src) ); java.io.File file_out = new java.io.File (dest); FileInputStream in1 = new FileInputStream (file_in); FileOutputStream out1 = new FileOutputStream (file_out); byte [] bytes = new byte [1024]; int c WHILE ((c = in1.read (bytes))! = - 1) Out1.write (bytes, 0, c); in1.close (); out1.close (); return (TRUE); // if success THEN RETURN TRUE} Catch (Exception E) {System.Out.println ("Error!"); Return (false); // if Fail the return false}} public static boolean copybyfile (file src, file dest) {Try { // instance the File as file_in and file_out FileInputStream in1 = new FileInputStream (src); FileOutputStream out1 = new FileOutputStream (dest); byte [] bytes = new byte [1024]; int c; while ((c = in1.read ( BYTES))))))! = - 1) Out1.write (bytes, 0, c); in1.close (); out1.close (); return (TRUE); // if success1 Retu Rn true} catch (eXception e) {system.out.println (e.tostring ()); return (false); // if fail dam}}}

} / ******************************************************* ********************** * Module: filelog.java * author: administrator * create: 2004-12-6 22:20:15 * purpose: generated Document log ******************************************************* ******************************* / pack org.heixue.util;

Import java.io.filenotfoundexception; import java.io.ioException;

/ ** * @Author Heixue * * / public class filepath = "/usr/tomcat_log.txt"; // "c: //tomcat_log.txt" java.io.outputstream outputstream = null;

/ ** * * / public filelog () {super ();} / ** * @Param Stream: Output stream * / public filelog (java.io.outputstream stream) {OutputStream = stream;} / ** * @Param filepath: file path * / public fileLog (String filepath) {super (); try {outputStream = new java.io.FileOutputStream (filepath, true);} catch (FileNotFoundException e) {e.printStackTrace ();}} public void Error (String Str) {java.util.date Date = new java.util.date (); string time = (Date.GetyEAR () 1900) "-" (Date.getMonth () 1) " - " Date.GetDate () " " Date.getHours () ": Date.getMinutes () ": Date.getSeconds (); str =" ERROR: " Time " -> " Str " / n "; if (outputstream! = null) Try {this.outputStream.write (Str.getBytes ()); this.outputstream.flush ();} catch (ooException e) {E.PrintStackTrace () }} Public void info (string str) {java.util.date date = new java.util.date (); string time = (Date.GetyEAR () 1900) "-" (Date.getMonth () 1) "-" Date.getdate () "" Date.GetHours () ": Date.getminutes () ": " Date.GetSeconds (); Str =" Info: " TIME "->" Str "/ n"; if (outputstream! = null) Try {this.outputStream.write (Str.getBytes ()); this.outputstream.flush ();} catch (ooException e) {E.PrintStackTrace ();} }

Public static void main (string [] args) {// filelog fl = new filelog (); //fl.setOutputStream(System.out); //fl.error ("error occours! against the situation"); filelog fl = New filelog ("c: / log.txt"); fl. error ("Error Occours! Anti-potential");} / ** * @return * / public string getlogfilepath () {return logfilepath;} / ** * @ Param string * / public void setlogfilepath (string string) {logfilepath = string;

/ ** * @Return * / public java.io.outputstream getOutputStream () {Return OutputStream;

/ ** * @Param stream * / public void setputstream (java.io.outputstream stream) {OutputStream = stream;}

}

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

New Post(0)