Util

 /**
* Write the context into the given file.
*
* @param context The context.
* @param theFileName The file name to write to.
* @throws IOException Exception when create the file.
*/
public static void writeToFile(String context, String theFileName)
throws IOException
{

if (theFileName != null && !"".equals(theFileName.trim()))
{
File aFile = new File(theFileName);
aFile = new File(aFile.getParent());
if (!aFile.exists()) {
aFile.mkdirs();
}
BufferedWriter aWriter = new BufferedWriter(new FileWriter(
theFileName));
aWriter.write(context);
aWriter.close();
} else
{
throw new IllegalArgumentException("Invalid file name!");
}
}




/**
* get locale
* @return
*/
public static Locale getLocale() {
return new Locale("en", "us");
}

/**
* to String, if object is null, return null, else call it's toString() method
* @param object
* @return
*/
public static String toString(Object object) {
if (object == null) {
return null;
} else {
return object.toString();
}
}

public static String decPlaceFormater(double value, int length)
{
DecimalFormat fmt = new DecimalFormat();
fmt.setMinimumFractionDigits(length);
fmt.setMaximumFractionDigits(length);
return fmt.format(value);
}


/**
* Returns true if this string is alpha-numeric; false, otherwise
* @author liliugen.
*/
static public boolean isAlphaNumeric(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isLetterOrDigit(str.charAt(i))) {
return false;
}
}
return true;
}



/**
* Returns true if this string is alpha-numeric; false, otherwise
* @author liliugen.
*/
static public boolean isDigit(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}



/**
* This is a wrapper API to the Base64Encoder
* @param input String
* @return Base64 encoded string
*/
static public String encodeString(String strInput) {
Base64Encoder b = new Base64Encoder(strInput);
return b.processString();
}

/**
* This is a wrapper API to the Base64Decoder
* @param input String
* @return String decoded from Base64 encoded input
*/
static public String decodeString(String strInput) {
try {
Base64Decoder b = new Base64Decoder(strInput);
return b.processString();
} catch (Base64FormatException ex) {
return "";
}
}



 /**
* Returns true if this string is letter only; false, otherwise
*
*/
static public boolean isLetters(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isLetter(str.charAt(i))) {
return false;
}
}
return true;
}

/**
* Returns true if this string is alpha-numeric; false, otherwise
* @author Benjamin Yip.
*/
static public boolean isNumeric(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}

/**
* trim string.
* using String.trim() to trim string.if string is null, return null
* @param string
*/
public static String trim(String string) {
if (string == null) {
return string;
} else {
return string.trim();
}
}

/**
* trim string, if input is null, return "";
* @param string
*/
public static String trimNoNull(String string) {
if (string == null) {
return "";
} else {
return string.trim();
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章