javawaveblogs-20

Monday, March 9, 2009

Generate a unique identifier with java.util.UUID

Java SE 5 has introduced the java.util.UUID class to easily generate Universally Unique Identifier (UUID)


import java.util.UUID;

/**
* This class is used to generate UUID and return it as a String object
*
* @author dhanago
*
*/
public class GenerateUUID
{
/**
* method to return UUID as a String object
*
* @return
*/
public static String getUUID()
{
return UUID.randomUUID().toString();
}
}

Tuesday, March 3, 2009

Java utility to read from resource bundle or properties file



/**
* dataSource.properties is loaded to a resource bundle.
*/
public static ResourceBundle resourceBundle = ResourceBundle.getBundle(
"sample.prop.health");

/**
* This method will return the value of the property from the resource
* bundle.
*
* @param key
* property key
* @return property value
*/
public static String getProperty(String key)
{
String value = null;
try
{
value = resourceBundle.getString(key);
}
catch (MissingResourceException missingResourceException)
{
Logger.getLogger(Utility.class.getName()).
log(Level.SEVERE,
"Resource Bundle not found",
missingResourceException);
}
return value;
}

Java method to format the current date to yyyy-MM-dd using SimpleDateFormat.


/**
* This method will format the current date to yyyy-MM-dd.
* @return
* current date as String.
*/
public static String formatedCurrentDate()
{
String toDate = new Date().toString();
SimpleDateFormat formatter = new SimpleDateFormat(
"EEE MMM yyyy hh:mm:ss zzz");
Date date = formatter.parse(toDate,
new ParsePosition(0));

toDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
return toDate;
}

Java method to return a File object from the file path specified


/**
* This method will return a file from the path specified.
*
* @param path
* path of the file name.
* @param fileName
* Name of the file.
* @return File object
*/
public static File readFileFromPath(String path, String fileName)
{
String fileNameWithPath = null;
if (path.endsWith("/"))
{
fileNameWithPath = path + fileName;
}
else
{
fileNameWithPath = path + "/" + fileName;
}
File file = new File(fileNameWithPath);

return file;
}

Reading file as String in Java


/** This method will read a file as String
* @param filePath
* @return file as String
* @throws java.io.IOException
*/
public static String readFileAsString(String filePath)
throws java.io.IOException
{
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1)
{
String readData = String.valueOf(buf,
0,
numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString();
}

Java method to replace every occurences of a string within another string


/**
* This method will replace every occurences of a string within another
* string.
*
* @param target
* is the original string
* @param from
* is the string to be replaced
* @param to
* is the string which will used to replace
* @return
* changed new string
*/
public static String replace(String target, String from, String to)
{
int start = target.indexOf(from);
if (start == -1)
{
return target;
}
int fromLength = from.length();
char[] targetChars = target.toCharArray();
StringBuffer buffer = new StringBuffer();
int copyFrom = 0;
while (start != -1)
{
buffer.append(targetChars,
copyFrom,
start - copyFrom);
buffer.append(to);
copyFrom = start + fromLength;
start = target.indexOf(from,
copyFrom);
}
buffer.append(targetChars,
copyFrom,
targetChars.length - copyFrom);
return buffer.toString();
}

diggthis