Java File Name Create toFilename(String name, String extension)

Here you can find the source of toFilename(String name, String extension)

Description

to Filename

License

Open Source License

Declaration

public static String toFilename(String name, String extension) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008, 2010 SAP AG.//from  w w w.jav a 2  s.  co  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    SAP AG - initial API and implementation
 *******************************************************************************/

public class Main {
    public static String toFilename(String name, String extension) {
        return toFilename(name, "", extension); //$NON-NLS-1$
    }

    /**
     * Build a file name.
     * Convert non-letters or digits to underscore.
     * @param prefix the prefix of the file
     * @param suffix the suffix
     * @param extension the file extension
     * @return the combined file name
     */
    public static String toFilename(String prefix, String suffix, String extension) {
        StringBuilder buf = new StringBuilder(prefix.length() + suffix.length() + extension.length() + 1);

        for (String s : new String[] { prefix, suffix }) {
            for (int ii = 0; ii < s.length() && ii < 20; ii++) {
                char c = s.charAt(ii);
                if (Character.isLetterOrDigit(c))
                    buf.append(c);
                else
                    buf.append("_"); //$NON-NLS-1$
            }
        }

        buf.append(".").append(extension); //$NON-NLS-1$

        return buf.toString();
    }
}

Related

  1. toFileName(String address)
  2. toFileName(String className)
  3. toFilename(String filenameOrURI)
  4. toFilename(String key, final String suffix)
  5. toFileName(String moduleName)
  6. toFilename(String s)
  7. toFileName(String s)
  8. toFilename(String str)
  9. toFileName(String str)