Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. */ package io.uengine.util; import io.uengine.common.exception.ServiceException; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.SystemUtils; import java.io.File; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; /** * File Utility. * * @author Byoung Gon, Kim * @since 0.1 */ public class FileUtils { public static long KB = 1024L; public static long MB = KB * KB; public static long GB = MB * KB; public static long TB = GB * KB; public static long PB = TB * KB; public static long ZB = PB * KB; /** * ?? ? ? . * * @param filename ? * @return ? . ? ?? ? ? 0L? . */ public static long lastModified(final String filename) { return new File(filename).lastModified(); } /** * ? ? . ) "mypath/myfile.txt" -> "myfile.txt". * * @param path ? (<tt>null</tt>? ? ? ?) * @return ? ? ? ?? <tt>null</tt> */ public static String getFilename(String path) { return org.springframework.util.StringUtils.getFilename(path); } /** * ? ?? ? . ) "mypath/myfile.txt" -> "txt". * * @param path ? (<tt>null</tt>? ? ? ?) * @return the extracted filename extension, or <tt>null</tt> if none */ public static String getFilenameExtension(String path) { return org.springframework.util.StringUtils.getFilenameExtension(path); } /** * ? ?? ? . ) "mypath/myfile.txt" -> "mypath/myfile". * * @param path ? (<tt>null</tt>? ? ? ?) * @return ? ? ?? ? ?? <tt>null</tt> */ public static String stripFilenameExtension(String path) { return org.springframework.util.StringUtils.stripFilenameExtension(path); } /** * Fully Qualified Path? Path Separator . * * @param fullyQualifiedPath Fully Qualified Path * @return Path Separator */ public static String getPath(String fullyQualifiedPath) { int sep = fullyQualifiedPath.lastIndexOf(SystemUtils.FILE_SEPARATOR); if (sep != 0) { return fullyQualifiedPath.substring(0, sep); } return SystemUtils.FILE_SEPARATOR; } /** * Fully Qualified Path? Path Separator . * * @param fullyQualifiedPath Fully Qualified Path * @return Path Separator */ public static String getDirectoryName(String fullyQualifiedPath) { int sep = fullyQualifiedPath.lastIndexOf(SystemUtils.FILE_SEPARATOR); int length = fullyQualifiedPath.getBytes().length; return fullyQualifiedPath.substring(sep + 1, length); } /** * Fully Qualified Path? Path Separator ? ? ? ?? ?. * * @param fullyQualifiedPath Fully Qualified Path * @param replacement ? ? ? * @return ? ? ? ?? fully qualified path */ public static String replaceLast(String fullyQualifiedPath, String replacement) { String path = getPath(fullyQualifiedPath); if (path.endsWith(SystemUtils.FILE_SEPARATOR)) { return path + replacement; } return path + SystemUtils.FILE_SEPARATOR + replacement; } /** * URL? Input Stream? . * * @param url URL * @return Input Stream */ public static InputStream openStream(String url) { String message = ExceptionUtils .getMessage("URL '{}' ? Input Stream? ? .", url); try { return new URL(url).openStream(); } catch (MalformedURLException ex) { if (ex.getMessage().contains("no protocol") && url.startsWith("/")) { try { return new URL("file:" + url).openStream(); } catch (Exception e) { throw new ServiceException(message, e); } } throw new ServiceException(message, ex); } catch (Exception ex) { throw new ServiceException(message, ex); } } /** * URL? ?. * * @param url URL * @return <tt>true</tt> */ public static boolean isExist(String url) { String message = ExceptionUtils .getMessage("URL '{}' ? Input Stream? ? .", url); InputStream inputStream = null; try { inputStream = new URL(url).openStream(); return true; } catch (MalformedURLException ex) { if (ex.getMessage().contains("no protocol") && url.startsWith("/")) { try { inputStream = new URL("file:" + url).openStream(); return true; } catch (Exception e) { return false; } } return false; } catch (Exception ex) { return false; } finally { IOUtils.closeQuietly(inputStream); } } }