📄 utils.java.svn-base
字号:
/*
* Copyright(C) 2008, NTT AT Co., Ltd.
* Project: AWGView
*
* Notes:
* N/A
*
* Record of change:
* Date Version Name Content
* 2008/12/15 1.0 TriNT First create
*/
package jp.co.ntt.awgview.server.common;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import jp.co.ntt.awgview.server.constant.Constants;
/**
* Class name : Utils <BR>
*
* Package : jp.co.nttat.awgstar.server.common <BR>
*
* Description: This class provides function to give support <BR>
*
* @author : AI&T
* @version : 1.0
*/
public class Utils {
private static final String OS_NAME = "os.name";
private static final String OS_LINUX = "Linux";
private static final String LINUX_IFCONFIG_CMD = "ifconfig";
// see if line contains eth0 MAC address
private static final String ETH_0 = "eth0";
// see if line contains MAC address
private static final String HARD_WARE_ADDR = "HWaddr";
public static final String LINE_SEP = System.getProperty("line.separator");
/**
* Gent Directory path
* @param isFile
* @param directorys
* @return String
*/
public static String genPathDirectory(boolean isFile, String... directorys) {
String path = "";
try {
if (directorys != null) {
int len = directorys.length;
for (int i = 0; i < len; i++) {
if (directorys[i] == null) {
return null;
}
path += directorys[i];
if ((i < (len - 1)) || (!isFile)) {
path += Constants.FILE_SEPARATOR;
}
}
}
} catch (Exception e) {
return null;
}
return path;
}
/**
* Loads the properties from the source .properties file specified and
* over-rides them with those found in the merge file.
*/
public static Properties loadFile2Properties(String fileName) {
FileInputStream fis = null;
try {
Properties prop = new Properties();
fis = new FileInputStream(fileName);
prop.load(fis);
return prop;
} catch (Exception e) {
LogWriter.getDBLogger().error("Can not read file: " + fileName);
LogWriter.getSNMPLogger().error("Can not read file: " + fileName);
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ioe) {
LogWriter.getDBLogger().error("IOException:" + ioe.toString());
LogWriter.getSNMPLogger()
.error("IOException:" + ioe.toString());
ioe.printStackTrace();
}
}
return null;
}
/***
* Read a file using BufferedReader & FileReader to ArrayList<String>
*
* @param fileName
* @return ArrayList<String>
*/
public static ArrayList<String> readFile2ArrayList(String fileName) {
ArrayList<String> resultList = new ArrayList<String>();
FileReader fileReader = null;
BufferedReader buffReader = null;
try {
fileReader = new FileReader(fileName);
buffReader = new BufferedReader(fileReader);
String temp = "";
while ((temp = buffReader.readLine()) != null) {
if ((temp != null) && (temp.trim().length() > 0)) {
resultList.add(temp);
}
}
if ((resultList != null) && (resultList.isEmpty())) {
return null;
} else {
return resultList;
}
} catch (Exception e) {
LogWriter.getSNMPLogger().error(e.toString());
LogWriter.getSNMPLogger().error(e.toString());
return null;
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
if (buffReader != null) {
buffReader.close();
}
fileReader = null;
buffReader = null;
} catch (IOException e) {
LogWriter.getSNMPLogger().error(e.toString());
LogWriter.getSNMPLogger().error(e.toString());
}
}
}
/**
* This function reads the given filename into a string.
*
* @param filePath
* : filePath the name of the file to open.
* @return String
*/
public static String readFile2String(String filePath) {
FileInputStream fis = null;
InputStreamReader isReader = null;
BufferedReader buffReader = null;
StringBuffer strbuffer = new StringBuffer();
/* Init file input stream */
try {
fis = new FileInputStream(filePath);
// isReader = new InputStreamReader(fis, Constants.ENCODING_SJIS);
isReader = new InputStreamReader(fis);
if (isReader == null) {
return null;
}
/* Use buffer reader so that we can use read line by line */
buffReader = new BufferedReader(isReader);
String line = null;
while ((line = buffReader.readLine()) != null) {
line = line.trim();
strbuffer.append(line);
strbuffer.append(Constants.LINE_SEP);
}
// buffReader.close();
return strbuffer.toString();
} catch (IOException e) {
LogWriter.getSNMPLogger().error(e.toString());
LogWriter.getSNMPLogger().error(e.toString());
} finally {
try {
if (buffReader != null) {
buffReader.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
LogWriter.getSNMPLogger().error(e.toString());
LogWriter.getSNMPLogger().error(e.toString());
}
}
return null;
}
/**
* Replace all occurrences of a substring within a string with another
* string.
*
* @param inString
* String to examine
* @param oldPattern
* String to replace
* @param newPattern
* String to insert
* @return a String with the replacements
*/
public static String replace(String inString, String oldPattern,
String newPattern) {
if (inString == null) {
return null;
}
if (oldPattern == null || newPattern == null) {
return inString;
}
StringBuffer sbuf = new StringBuffer();
// output StringBuffer we'll build up
int pos = 0; // our position in the old string
int index = inString.indexOf(oldPattern);
// the index of an occurrence we've found, or -1
int patLen = oldPattern.length();
while (index >= 0) {
sbuf.append(inString.substring(pos, index));
sbuf.append(newPattern);
pos = index + patLen;
index = inString.indexOf(oldPattern, pos);
}
sbuf.append(inString.substring(pos));
// remember to append any characters to the right of a match
return sbuf.toString();
}
/**
* Get current date and/or time in Java using the following method. You may
* change the date format in the constructor of SimpleDateFormat to get the
* result in a different format.
*
* @param dateFormat
* : yyyy.MMMMM.dd or yyyy.MMMMM.dd GGG hh:mm aaa
* @return String
*/
public static String getNow(String dateFormat) {
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(cal.getTime());
}
public static long getTimeStampMilli() {
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
return cal.getTimeInMillis();
}
/**
*
* @param str
* String
* @param intDefault
* position
* @return int position
*/
public static int parseInt(String str, int intDefault) {
int intResult;
try {
str = str.trim();
intResult = Integer.parseInt(str);
} catch (Exception e) {
intResult = intDefault;
}
return intResult;
}
/**
* Check the input string is numeric format or not
*
* @param number
* - the specificed string
* @return - true if the input string has integer numeric format - false if
* the input string has not integer numeric format
*/
public static boolean isInteger(String number) {
if ((number == null) || ("".equals(number))) {
return false;
}
try {
Integer.parseInt(number);
} catch (NumberFormatException NEx) {
// log error
return false;
}
return true;
}
/**
* Get Address
*
* @param strIPAddress
* @return String IP Address
*/
public static String getAddress(String strIPAddress) {
String port = "/";
int colon = strIPAddress.indexOf(port);
if (colon < 0) {
return strIPAddress;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -