fileutils.java

来自「一个用struts tiles的在线影院web系统」· Java 代码 · 共 79 行

JAVA
79
字号
package com.blue.web.common.util;

import java.io.File;

/**
 * 
 * @author Lucifer
 *
 */
public final class FileUtils {
	// 
	public static final char DirectorySeparatorChar		= '\\';
	public static final char AltDirectorySeparatorChar	= '/';
	public static final char VolumeSeparatorChar			= ':';

	/**
	 * This class cannot be instantiated.
	 *
	 */
	private FileUtils() {
	}

	public static String getFileName(String path) {
		if (path != null) {
			int length = path.length();
			int index = length;
			while (--index >= 0) {
				char ch = path.charAt(index);
				if (((ch == FileUtils.DirectorySeparatorChar) || (ch == FileUtils.AltDirectorySeparatorChar))
						|| (ch == FileUtils.VolumeSeparatorChar)) {
					return path.substring(index + 1, length);
				}
			}
		}
		return path;
	}

	public static final String getFileExtension(File file) {
		return getFileExtension(file.getName());
	}

	public static final String getFileExtension(String path) {
		String extension = "";
		int iIndex = path.lastIndexOf(".");
		if(iIndex > 0)
			extension = path.substring(iIndex + 1);
		return extension;
	}

	/**
	 * Get tne name of a file without the extension.
	 * @param file
	 * @return
	 */
	public static final String getFileNameWithoutExtension(File file) {
		return getFileNameWithoutExtension(file.getName());
	}

	/**
	 * Get a file name without the extension.
	 * @param path The name of the file
	 * @return
	 */
	public static final String getFileNameWithoutExtension(String path) {
		String filename = getFileName(path);
		int index = filename.lastIndexOf(".");
		if(index > 0)
			filename = filename.substring(0, index);
		return filename;
	}
	
	public static final String getPathWithoutFileName(String path) {
		int index = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
		if (index < 0)
			return path;
		return path.substring(0, index);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?