Here you can find the source of getFileName(final CharSequence url)
Parameter | Description |
---|---|
url | The URL to the file name for |
public static String getFileName(final CharSequence url)
//package com.java2s; /*/*from w w w.j a va2 s . com*/ * Copyright 2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.net.URI; import java.net.URL; public class Main { /** * Returns the unqualified file name for the passed URL * @param url The URL to the file name for * @return The file name */ public static String getFileName(final CharSequence url) { return getFileName(toURL(url)); } /** * Returns the unqualified file name for the passed URL * @param url The URL to the file name for * @return The file name */ public static String getFileName(final URL url) { if (url == null) throw new IllegalArgumentException("The passed URL was null"); return new File(url.getFile()).getName(); } /** * Returns the URL for the passed file * @param file the file to get the URL for * @return a URL for the passed file */ public static URL toURL(File file) { try { return nvl(file, "Passed file was null").toURI().toURL(); } catch (Exception e) { throw new RuntimeException("Failed to get URL for file [" + file + "]", e); } } /** * Creates a URL from the passed string * @param urlStr A char sequence containing a URL representation * @return a URL */ public static URL toURL(final CharSequence urlStr) { if (urlStr == null || urlStr.toString().trim().isEmpty()) throw new IllegalArgumentException("The passed URL stringy was null or empty"); try { if (isFile(urlStr)) return toURL(new File(urlStr.toString()).getAbsoluteFile()); return new URL(nvl(urlStr, "Passed string was null").toString()); } catch (Exception e) { throw new RuntimeException("Failed to create URL from string [" + urlStr + "]", e); } } /** * Creates a URI from the passed string * @param uriStr A char sequence containing a URI representation * @return a URI */ public static URI toURI(CharSequence uriStr) { try { return new URI(nvl(uriStr, "Passed string was null").toString()); } catch (Exception e) { throw new RuntimeException("Failed to create URL from string [" + uriStr + "]", e); } } /** * Tests the passed object for nullness. Throws an {@link IllegalArgumentException} if the object is null * @param t The object to test * @param message The message to associate with the exception, Ignored if null * @return the object passed in if not null */ public static <T> T nvl(T t, String message) { if (t == null) throw new IllegalArgumentException(message != null ? message : "Null parameter"); return t; } /** * Determines if the passed stringy represents an existing file name * @param urlStr The stringy to test * @return true if the passed stringy represents an existing file name, false otherwise */ public static boolean isFile(final CharSequence urlStr) { if (urlStr == null || urlStr.toString().trim().isEmpty()) throw new IllegalArgumentException("The passed URL stringy was null or empty"); return new File(urlStr.toString().trim()).exists(); } }