⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysinfo.java

📁 一个简单的visio程序。
💻 JAVA
字号:

package hc.util; 
 
import java.io.*;
import java.util.*;
import java.net.*;
       
public class SysInfo {
    
    public static final int WINDOWS =	    0;

    public static final int WINDOWS_NT =    1;

    public static final int LINUX =	    2;

    public static final int SOLARIS =	    3;

    public static final int AIX =	    4;

    public static final int OS =	    5;
	
	public static final int OS_2 = 5;

    public static final int NOVELL =	    6;
	
	public static final int OS_390 = 7;

    // expected more systems ID here.


    static String osName = System.getProperty("os.name");
    
    static String javaHome = _getJavaHome();
    

    public static String getOsName() {

	return osName;

    }

    
    /**

     * return -1    os name unknown

     */

    public static int getSysType() {

		if(osName == null) return -1;
	
		String os = osName.toLowerCase();
	
		if(os.indexOf("windows") != -1) {
	
			if(os.indexOf("nt") == -1) return 0;
	
			else return 1;
	
		}

		else if(os.indexOf("linux") != -1) return 2;
	
		else if(os.indexOf("solaris") != -1) return 3;
	
		else if(os.indexOf("aix") != -1) return 4;
	
		else if(os.indexOf("os/2") != -1) return 5;
	
		else if(os.indexOf("netware") != -1) return 6;
	
		else if(os.indexOf("os/390") != -1) return 7;
	
		// expected code change here if more os ID added
	
		
	
		else return -1;

    }  
    
    public static String getDefaultJVM()
    {
		int type = getSysType();
		if(type == NOVELL) {
			return "java";
		}
		String path = getJavaHome();
		path += File.separator + "bin" + File.separator;
	
		String ext = "";
		if(type == WINDOWS || type == WINDOWS_NT || type == OS_2) {
			ext = ".exe";
		}
		if((new File(path + "javaw" + ext)).exists())
		{
			path += "javaw" + ext;
		}
		else if((new File(path + "java" + ext)).exists())
		{
			path += "java" + ext;
		}
		else if((new File(path + "javaw_g" + ext)).exists())
		{
			path += "javaw_g" + ext;
		}
		else if((new File(path + "java_g" + ext)).exists())
		{
			path += "java_g" + ext;
		}
		else if((new File(path + "jrew" + ext)).exists())
		{
			path += "jrew" + ext;
		}
		else if((new File(path + "jre" + ext)).exists())
		{
			path += "jre" + ext;
		}
		else if((new File(path + "jrew_g" + ext)).exists())
		{
			path += "jrew_g" + ext;
		}
		else if((new File(path + "jre_g" + ext)).exists())
		{
			path += "jre_g" + ext;
		}
		return path;
    }

    public static String quoteLongPath(String longPath) {

        String tmpStr = longPath;
        if(tmpStr.indexOf(" ") != -1 && !(longPath.startsWith("\"") && longPath.endsWith("\"")))
        {
    	tmpStr = "\"" + tmpStr + "\"";
        }
        return tmpStr;
    }
    
    public static String getINIString(String session, String keyword, String  iniFile) {
	File ini = new File(iniFile);
	if(!ini.exists()) {
	    return null;
	}
	boolean hasSession = false;
	    try {
		RandomAccessFile file = new RandomAccessFile(ini, "r");
		file.seek(0);
		String line = null;
		for(int i = 0; (line = file.readLine()) != null; i++) {
		    line = line.trim();
		    if(line.equals("["+ session + "]")) {
			hasSession = true;
		    }
		    if(hasSession) {
			if(!line.equals("["+ session + "]") && line.startsWith("[") && line.endsWith("]")) {
			    hasSession = false;
			}
			else if(line.startsWith(keyword + "=")) {
			    return line.substring(keyword.length() + 1);
			}
		    }
		}
		file.close();
	    }
	    catch(Exception e) {
		System.out.println("Can not read the file " + iniFile);
//		e.printStackTrace();
		return null;
	    }
	return null;
    }
    
    public static void setINIString(String session, String keyword, String value, String  iniFile) {
	Vector strArray = new Vector();
	File ini = new File(iniFile);
	int id = -1;
	int sessionId = -1;
	boolean found = false;
	boolean sessionFound = false;
	boolean inSession = false;
	    try {
		RandomAccessFile file = new RandomAccessFile(ini, "rw");
		file.seek(0);
		String line = null;
		for(int i = 0; (line = file.readLine()) != null; i++) {
		    line = line.trim();
		    if(!found) {
			if(line.equals("["+ session + "]")) {
			    inSession = true;
			    if(!sessionFound) {
				sessionId = i;
				sessionFound = true;
			    }
			}
			if(inSession) {
			    if(!line.equals("["+ session + "]") && line.startsWith("[") && line.endsWith("]")) {
				inSession = false;
			    }
			    else if(line.startsWith(keyword + "=")) {
				id = i;
				found = true;
			    }
			}
		    }
		    strArray.addElement(line);
		}
		file.close();
	    }
	    catch(Exception e) {
		System.out.println("Can not read the file " + iniFile);
//		e.printStackTrace();
		return;
	    }
	    
	ini.delete();
	
	PrintWriter file;
    	try {
    	    FileWriter fout = new java.io.FileWriter(iniFile);
    	    file = new PrintWriter(fout);
    	} catch (Exception e) {
    	    System.err.println("Can not open file " + iniFile);
//	    e.printStackTrace();
    	    return;
    	}
	    
	try {
	    int size = strArray.size();
	    if(id != -1) {
		for(int i = 0; i < id; i++) {
		    file.println((String)strArray.elementAt(i));
		}
		file.println(keyword + "=" + value);
		for(int i = id + 1; i < size; i++) {
		    file.println((String)strArray.elementAt(i));
		}
	    }
	    else {
		if(sessionFound) {
		    for(int i = 0; i <= sessionId; i++) {
			file.println((String)strArray.elementAt(i));
		    }
		    file.println(keyword + "=" + value);
		    for(int i = sessionId + 1; i < size; i++) {
			file.println((String)strArray.elementAt(i));
		    }
		}
		else {
		    file.println("[" + session + "]");
		    file.println(keyword + "=" + value);
		    file.println("");
		    for(int i = 0; i < size; i++) {
			file.println((String)strArray.elementAt(i));
		    }
		}
	    }
	       
	    file.close();
	}
	catch(Exception e) {
	    System.out.println("Can not write to file " + iniFile);
//	    e.printStackTrace();
	    return;
	}
	return;
    }
    
    /** 
     * value = null	return keyword value from .ini file
     * value != null	add value to keyword in .ini file, return this value
     */
    public static String updateINI(String keyword, String value, String fileName) {
	Vector strArray = new Vector();
	File ini = new File(fileName);
	boolean existed = ini.exists();
	int id = -1;
	if(existed) {
	    try {
		RandomAccessFile file = new RandomAccessFile(ini, "r");
		file.seek(0);
		String line = null;
		for(int i = 0; (line = file.readLine()) != null; i++) {
		    line = line.trim();
		    if(line.startsWith(keyword + "=")) {
			if(value == null) {
			     return line.substring(keyword.length() + 1);
			}
			id = i;
		    }
		    strArray.addElement(line);
		}
		file.close();
	    }
	    catch(Exception e) {
		System.out.println("Can not read the file " + fileName);
//		e.printStackTrace();
		return null;
	    }
	}
	if(value == null) {
	    return null;
	}
	ini.delete();
	
	PrintWriter file;
    	try {
    	    FileWriter fout = new java.io.FileWriter(fileName);
    	    file = new PrintWriter(fout);
    	} catch (Exception e) {
    	    System.err.println("Can not open file " + fileName);
//	    e.printStackTrace();
    	    return null;
    	}
	    
	try {
	    int size = strArray.size();
	    for(int i = 0; i < id; i++) {
		file.println((String)strArray.elementAt(i));
	    }
	    file.println(keyword + "=" + value);
	    for(int i = id + 1; i < size; i++) {
	        file.println((String)strArray.elementAt(i));
	    }
	    file.close();
	}
	catch(Exception e) {
	    System.out.println("Can not write to file " + fileName);
//	    e.printStackTrace();
	    return null;
	}
	return value;
    }
    
    static String _getJavaHome() {
	    String home = System.getProperty("java.home");
	    int type = getSysType();
	    if(type == WINDOWS || type == WINDOWS_NT || type == OS) {
		home = home.toLowerCase();
	    }
	    if(type == NOVELL) {
		home = "sys:" + File.separator + home;
	    }
	    String ending = File.separator + "bin" + File.separator + "..";
	    if(home.endsWith(ending)) {
		home = home.substring(0, home.length() - ending.length());
	    }
	    return home;
    }

    public static String getJavaHome() {
	return javaHome;
    }
    
    /**
     * className - full class name (e.g. java.lang.String)
     * return - full path to the directory or the archive containing this class
     *        - null if this class not found
     */
    public static String getPathByClassName(String className) {
		String tmpStr = className.replace('.', '/') + ".class";
		URL url = ClassLoader.getSystemResource(tmpStr);
		if(url == null) {
			return null;
		}
		tmpStr = url.getFile();
		
//		System.out.println(tmpStr);
		
		int pos=0;
		String newsub=null;
		String protocol = url.getProtocol();
		
//		System.out.println(protocol);
	
		if (protocol.equalsIgnoreCase("systemresource"))
		{
			if(tmpStr.indexOf("ZIP") !=-1 ) {
				pos = tmpStr.indexOf("ZIP") + 3;
			}
			else if(tmpStr.indexOf("FILE") != -1) {
				pos = tmpStr.indexOf("FILE") + 4;
			}
			newsub=tmpStr.substring(pos,tmpStr.indexOf("+")-1);
	
			if(newsub.indexOf(File.separator)==-1) {
				File checkfile=new File(System.getProperty("user.dir"));
				String checkstr=checkfile.getAbsolutePath()+File.separator+newsub;
				if(new File(checkstr).exists()==true) {
					newsub=checkstr;
				}
				else {
					int intstr = -1;
					try {
						intstr = Integer.parseInt(newsub);
					}
					catch(NumberFormatException e) {
						e.printStackTrace();
						return null;
					}
					newsub=getPathFromCLASSPATH(intstr);
					if (newsub.indexOf(File.separator)==-1) {
						newsub=System.getProperty("user.dir")+File.separator+newsub;
					}
				}
			}
	
		}
		else if(protocol.equalsIgnoreCase("file"))
				newsub = tmpStr.substring(1,tmpStr.length()-(className.length()+7));
		else if(protocol.equalsIgnoreCase("jar"))
				newsub = tmpStr.substring(6,tmpStr.length()-(className.length()+8));
	
		return newsub;
    }
    
    static String getPathFromCLASSPATH(int a){
		StringTokenizer Pathst=new StringTokenizer(System.getProperty("java.class.path"),File.pathSeparator);
		for(int i=0;i<a;i++)
				Pathst.nextToken();
		String toreturn=Pathst.nextToken();
		return toreturn;
    }

    public static String getIb4jApplethome() {
        String  ib4jhome = getPathByClassName("hc.util.SysInfo");
		if (ib4jhome == null) return null;

		int id = ib4jhome.lastIndexOf(File.separator);
		if(id == -1) {
		    id = ib4jhome.lastIndexOf('/');
		    if (id == -1) return null;
		}
		String tmpstr = ib4jhome.substring(id +1 ,ib4jhome.length());
		ib4jhome = ib4jhome.substring(0, id);

		if(tmpstr.equalsIgnoreCase("classes.zip")){
			id = ib4jhome.lastIndexOf(File.separator);
			if(id == -1) return null;
		}
		return ib4jhome.substring(0, id);
    }
	
   public static String getFileNameByPath(String path)
   {
          int index = path.lastIndexOf(File.separator);
          String fileName = null;
          if(index != -1)
              fileName = path.substring(index + 1);
          else
              fileName = path;
          return fileName;
   }
   
   public static String bye = "Your installation is corrupt. Program will exit.";
} 	
// end of SysInfo.java
    	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -