Java Utililty Methods File Name Create

List of utility methods to do File Name Create

Description

The list of methods to do File Name Create are organized into topic(s).

Method

StringgenerateFileId(String userlogin)
generate File Id
String pattern = "yyyyMMddHHmmssSSS";
DateFormat df = new SimpleDateFormat(pattern);
String timestamp = df.format(new Date());
return userlogin + "_" + timestamp;
StringgenerateFileName()
Generates and returns a new file name by appending the current date in front of the file name

formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(Calendar.getInstance().getTime()) + "_Giant.xls";
StringgenerateFileName()
generate File Name
return generateFileName("", ".txt");
StringgenerateFileName(String fileName)
generate File Name
DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return formatDate + random + extension;
StringgenerateFileNameWithoutExtension(final String testClassName, final String testId, final boolean includeDate)
generate File Name Without Extension
StringBuilder sb = new StringBuilder(testClassName);
if (testId != null) {
    sb.append("_").append(testId);
if (includeDate) {
    sb.append("_");
    SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMddHHmmss");
    String formattedDate = dateFormat.format(new Date());
...
StringgetRandomFileName(@Nullable final String prefix)
Generate a random filename with a specified prefix.
final Date now = new Date(System.currentTimeMillis());
String dateStr = null;
synchronized (DATE_FORMAT) {
    dateStr = DATE_FORMAT.format(now);
if ((prefix == null) || (prefix.length() == 0)) {
    return dateStr;
} else {
...
StringtoFileName(final Object obj)
to File Name
return obj.toString().replaceAll("[^a-zA-Z0-9_.-]", "_");
StringtoFileName(String address)
to File Name
StringBuilder builder = new StringBuilder();
int i = 0;
while (i < address.length()) {
    char c = address.charAt(i);
    if (Character.isLetterOrDigit(c)) {
        builder.append(c);
    } else {
        builder.append('_');
...
StringtoFileName(String className)
Convert the Java-class-name (dot delimited package name)to the equivalent Java-class-file-name .
StringBuffer fileName;
if (className == null) {
    throw new IllegalArgumentException("null 'className' arg in method call.");
className = className.trim();
if (className.equals("")) {
    throw new IllegalArgumentException("empty 'className' arg in method call.");
fileName = new StringBuffer(className);
if (className.endsWith(".class") && className.length() > 6) {
    fileName.setLength(className.length() - 6);
for (int i = 0; i < fileName.length(); i++) {
    if (fileName.charAt(i) == '.') {
        fileName.setCharAt(i, '/');
fileName.append(".class");
return fileName.toString();
StringtoFilename(String filenameOrURI)
Turn a file: URL or file name into a plain file name
if (!isFile(filenameOrURI))
    return null;
String fn = filenameOrURI;
if (!fn.startsWith("file:"))
    return fn;
if (fn.startsWith("file:///"))
    fn = fn.substring("file://".length());
else if (fn.startsWith("file://localhost/"))
...